Effect cleanup potential issue in React 17 for mutable value inside effect
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
In React 17, the effect cleanup function always runs asynchronously — for example, if the component is unmounting, the cleanup runs after the screen has been updated.
In the changelog React team has highlighted a potential issue and solution for this:
Problematic Code:
useEffect(() => {
someRef.current.someSetupMethod();
return () => {
someRef.current.someCleanupMethod();
};
});
Solution suggested by the React team:
The problem is that
someRef.currentis mutable, so by the time the cleanup function runs, it may have been set to null. The solution is to capture any mutable values inside the effect.
useEffect(() => {
const instance = someRef.current;
instance.someSetupMethod();
return () => {
instance.someCleanupMethod();
};
});
While the above solution works in most cases, I have created a custom hook, where this solution is problematic:
function useMountEffect(funcForMount, funcForUnmount) {
const funcRef = useRef();
funcRef.current = { funcForMount, funcForUnmount };
useEffect(() => {
funcRef?.current?.funcForMount?.();
return () => funcRef?.current?.funcForUnmount?.();
}, []);
}
In the above example, I explicitly don't want to capture the mutable value, otherwise, I'll have to re-run the effect when funcForUnmount changes.
The reason to create the hook in this way is to have the freedom to call the function only on mount / unmount without worrying about stale closure and dependency array.
Update:
I have found one approach for useMountEffect
function useMountEffect(funcForMount, funcForUnmount) {
const isMounted = useRef();
useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);
useEffect(() => {
if (!isMounted.current) {
isMounted.current = true;
funcForMount?.();
}
return () => !isMounted.current && funcForUnmount?.();
});
}
But the major questions here are:
- How can the
ref.currentgo null between unmount and cleanup call in React 17? - Who is setting the value null? Is it react which can set
ref.currentto null? Or, is it the user who can change in between? - If a user can change in between how is this possible? Why was it not an issue prior to React 17?
The reason to raise this issue for docs is that the line The problem is that someRef.current is mutable, so by the time the cleanup function runs, it may have been set to null. is unclear and raises a lot of questions in the mind.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the React 17 changelog section linked in the issue and compare its explanation with the reported useMountEffect example. Clarify who can set ref.current to null, when cleanup runs, and why the timing changed in React 17. Done means the documentation answers the listed questions without leaving the mutable-ref behavior ambiguous.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100