Skip to content

Instantly share code, notes, and snippets.

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