Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
Effect cleanup potential issue in React 17 for mutable value inside effect

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.current is 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 for 'The problem is that someRef.current is mutable, so by the time the cleanup function runs, it may have been set to null.':

  • How can the ref.current go null between unmount and cleanup call in React 17?
  • Who is setting the value null? Is it react which can set ref.current to 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?
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda