First I should say the only reason timeline is in the 2nd argument array for useEffect is because codesandbox's linter suggests it, but it's not required. I've actually changed it to an empty array like the React docs suggest.
If there is no array for that 2nd argument, the useEffect method runs on every re-render. If there is an empty array, it runs on the first render only, the same behavior as componentDidMount. If there is a prop in that array, it will only run on a re-render if the prop's value changed, which is the same behavior as componentDidUpdate. And lastly, if you return a function it acts as componentWillUnMount. Check the console.log lines below.
// componentDidMount
React.useEffect(() => {
console.log("useEffect 1: this executes only once.")
TweenLite.set([top.current, bot.current], { transformOrigin: "50% 50%" })
setTimeline(
timeline
.to(top.current, 0.1, { y: 6 }, 0)
.to(bot.current, 0.1, { y: -6 }, 0)
.to(top.current, 0.1, { rotation: 45 }, 0.2)
.to(bot.current, 0.1, { rotation: -45 }, 0.2)
.reverse()
)
return () => {
// componentWillUnmount
}
}, [])
// componentDidUpdate
React.useEffect(() => {
console.log(
"useEffect 2: this executes on every click because isMenuOpen changes on every click"
)
timeline.reversed(!isMenuOpen)
}, [isMenuOpen])
Thanks for your help! ❤️