Effect cleanup potential issue in React 17 for mutable value inside effect
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la sezione del changelog di React 17 collegata nell’issue e confronta la relativa spiegazione con l’esempio useMountEffect segnalato. Chiarisci chi può impostare ref.current su null, quando viene eseguito il cleanup e perché il timing è cambiato in React 17. Il lavoro è completato quando la documentazione risponde alle domande elencate senza lasciare ambiguo il comportamento delle mutable refs.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100