...

/

Keeping Value Constant

Keeping Value Constant

Using useRef to Ensure Value Remains Constant

The solution to this problem is simple. We can use the useRef hook to ensure that a value stays the same throughout component’s entire lifetime.

Here’s how it works:

Press + to interact
//correct implementation
const componentJustMounted = useRef(true)
useEffect(
() => {
if (!componentJustMounted.current) {
onExpand(expanded)
}
componentJustMounted.current = false
},
[expanded]
)

...