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

213
Vistas
Can I setState inside function passed to useCallback?

This question occurred to me because as we know from the docs:

useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).

The docs of useMemo say:

Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo.

I assume that also implies I can't do setState.

Now, if we have this code:

const memoizedValue = useMemo(() => computeExpensiveValue(), []);

Does above quote mean we can't do a side effect inside computeExpensiveValue or the inline function which was the argument to useMemo?

Because if it means we can't do side effect inside computeExpensiveValue it means we can't do a side effect also in a function passed to useCallback because that line is same as:

const memoizedValue = useCallback(computeExpensiveValue, []);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I see how you got there, but it's a slight misreading of the docs. (Fairly easy mistake to make.)

The function that the useMemo docs are talking about isn't your function (fn), it's this function in the useCallback "equivalent useMemo" example:

useMemo(() => fn, deps).
//      ^^^^^^^^

That function doesn't call any state setters, so it's fine.

It's absolutely fine and completely normal to call state setters in callback functions you memoize via useCallack. (As long as you don't call your callback during rendering.) When you do, be sure to either:

  • Not use any state variables in when calling the state setter:
    const fn = useCallback(() => {
         setSomething(someValueNotFromState);
    }, []);
    
    or
  • Use the callback form of the state setter:
    const fn = useCallback(() => {
         setSomething(something => something + 1);
    }, []);
    
    or
  • Declare any state members you're going to use in the callback as dependencies:
    const fn = useCallback(() => {
         setSomething(something + 1); // I usually avoid this
    }, [something]);
    //  ^^^^^^^^^
    

A more verbose example/comparison:

const [value, setValue] = useState(0);

const changeHandler1 = useCallback((event) => {
    // Absolutely fine to call `setValue` here
    setValue(event.currentTarget.value);
}, []);

// equivalent to:

const changeHandler2 = useMemo(() => {
    // NOT okay to call `setValue` here (the function we pass to `useMemo`)

    return (event) => {
        // Absolutely fine to call `setValue` here
        setValue(event.currentTarget.value);
    };
}, []);
about 4 years ago · Juan Pablo Isaza Denunciar
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