Skip to content

Instantly share code, notes, and snippets.

@KevinVR
Created February 26, 2021 11:25
Show Gist options
  • Save KevinVR/ad94282aea01bf8a56a5b667d968545d to your computer and use it in GitHub Desktop.
Save KevinVR/ad94282aea01bf8a56a5b667d968545d to your computer and use it in GitHub Desktop.
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