-
-
Notifications
You must be signed in to change notification settings - Fork 454
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug
A ref should never be updated in render.
The corrected hook is:
export function useUnmount(func: () => void) {
const funcRef = useRef(func)
// must be a separate use effect
useEffect(() => {
funcRef.current = func
}, [func])
useEffect(
() => () => {
funcRef.current()
},
[],
)
}To Reproduce
Expected behavior
Do not mutate refs in render, because a render can be backed out before it's committed, and the mutated ref with possible incorrect behavior will stick around for the real cleanup
Additional context
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working