Created
June 17, 2019 06:22
-
-
Save imparvez/2c0b9db3ae711b47da60e8f452158368 to your computer and use it in GitHub Desktop.
useEffect in React Hooks
This file contains hidden or 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 App() { | |
const [count, setCount] = useState(0) | |
useEffect(() => { | |
document.title = `You clicked ${count} times` | |
console.log('component did mount') | |
}, []); // componentDidMount, only when the component has been rendered at start | |
useEffect(() => { | |
document.title = `You clicked ${count} times` | |
console.log('component did update') | |
}, [count]) // Now this function is dependent on the value being passed | |
return ( | |
<div className="App"> | |
<button onClick={() => setCount(count + 1)}>Increase Count</button> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment