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

357
Vistas
React Hook - setData with pure function to use prevData but with a deep comparison inside

I have the following code:

export function CurrentUserProvider({ children }) {
  const [data, setData] = useState(undefined);
  
  return (
    <CurrentUserContext.Provider
      value={{
        data,
        setData,
      }}
    >
      {children}
    </CurrentUserContext.Provider>
  );
}

and I am doing the following to update the data:

const currentUser = useContext(CurrentUserContext);

currentUser.setData((prevData) => {
   ...prevData,
   totalFollowers: prevData.totalFollowers + 1
});

Now I need to avoid unnecessary state updates. But I also need to be able to access the setData pure function.

My idea was to deep compare the new data object, updating my CurrentUserProvider:

export function CurrentUserProvider({ children }) {
  const [data, _setData] = useState(undefined);

  const setData = (newData) => {
     if(!lodash.isEqual(newData, data)) {
        setData(newData);
     }
  }
  
  return (
    <CurrentUserContext.Provider
      value={{
        data,
        setData,
      }}
    >
      {children}
    </CurrentUserContext.Provider>
  );
}

But... with this, I am not able to access prevData. Any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Make the equality check inside the setState callback, and if they are equal, return the previous state. According to the docs:

If your update function returns the exact same value as the current state, the subsequent rerender will be skipped completely.

export function CurrentUserProvider({ children }) {
  const [data, _setData] = useState(undefined);

  const setData = useCallback(newData => {
    _setData(prevData => lodash.isEqual(newData, prevData)
      ? prevData
      : { ...prevData, ...newData }
    );
  }, []);

  return (
    <CurrentUserContext.Provider
      value={{
        data,
        setData,
      }}
    >
      {children}
    </CurrentUserContext.Provider>
  );
}
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