Last active
May 5, 2019 14:53
-
-
Save ak1103dev/098388db16c33e5384267c280f070b4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import Hook from './Hook' | |
import './App.css'; | |
function App() { | |
const [isLifecycle, setIsLifecycle] = useState(false) | |
return ( | |
<div className="App"> | |
<button onClick={() => setIsLifecycle(!isLifecycle)}> | |
Switch | |
</button> | |
{ | |
isLifecycle ? null : <Hook /> | |
} | |
</div> | |
); | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react' | |
function Hook() { | |
const [count, setCount] = useState(0) | |
useEffect(() => { | |
console.log('Hook: use effect') | |
document.title = `${count} times`; | |
return function cleanup() { | |
console.log('Hook: cleanup') | |
} | |
}); | |
console.log('Hook: before render') | |
return ( | |
<div> | |
<h1>Hook</h1> | |
<h2>{count}</h2> | |
<button onClick={() => setCount(count + 1)}>Add</button> | |
</div> | |
) | |
} | |
export default Hook |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment