Created
February 26, 2021 11:33
-
-
Save KevinVR/11a86db9581eb1835491e666302d8d05 to your computer and use it in GitHub Desktop.
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'; | |
const UseEffectClickCounterMount = (props) => { | |
const [clicks, setClicks] = useState(initialClicks || 0); | |
const incrementClicks = () => { | |
setClicks(clicks + 1); | |
}; | |
// Our 'componentDidMount' with hooks, give an empty dependency list! | |
useEffect(() => { | |
alert('The click counter has mounted!'); | |
}, []); // Do not forget to send an empty list! Undefined won't work! | |
useEffect(() => { | |
alert(`Clicks were updated to ${clicks}`); | |
}, [ clicks ]); | |
return <> | |
<h1>Click Counter</h1> | |
<p>Amount of clicks: {clicks}</p> | |
<button onClick={incrementClicks}>Click me!</button> | |
</>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment