Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created June 17, 2019 06:22
Show Gist options
  • Save imparvez/2c0b9db3ae711b47da60e8f452158368 to your computer and use it in GitHub Desktop.
Save imparvez/2c0b9db3ae711b47da60e8f452158368 to your computer and use it in GitHub Desktop.
useEffect in React Hooks
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