Created
February 26, 2021 11:25
-
-
Save KevinVR/ad94282aea01bf8a56a5b667d968545d 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, useEffect} from 'react'; | |
const UseEffectClickCounter = (props) => { | |
const [clicks, setClicks] = useState(initialClicks || 0); | |
const incrementClicks = () => { | |
setClicks(clicks + 1); | |
}; | |
// Introduce our useEffect hook | |
useEffect(() => { | |
// Every time `clicks` is updated, alert the user! | |
alert(`Clicks were updated to ${clicks}`); | |
}, [ clicks ]); | |
// The useEffect will run whenever the value of `clicks` or any other variable | |
// in this dependency list updates | |
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