Skip to content

Instantly share code, notes, and snippets.

@ohansemmanuel
Created April 13, 2019 08:49
Show Gist options
  • Save ohansemmanuel/d050d65e0069a0ecaa7fdac247c1ef29 to your computer and use it in GitHub Desktop.
Save ohansemmanuel/d050d65e0069a0ecaa7fdac247c1ef29 to your computer and use it in GitHub Desktop.
const ArrayDep = () => {
const [randomNumber, setRandomNumber] = useState(0)
const [effectLogs, setEffectLogs] = useState([])
useLayoutEffect(
() => {
setEffectLogs(prevEffectLogs => [...prevEffectLogs, 'effect fn has been invoked'])
},
[randomNumber]
)
return (
<div>
<h1>{randomNumber}</h1>
<button
onClick={() => {
setRandomNumber(Math.random())
}}
>
Generate random number!
</button>
<div>
{effectLogs.map((effect, index) => (
<div key={index}>{'🍔'.repeat(index) + effect}</div>
))}
</div>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment