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

261
Vistas
React (Javascript) - callbacks overlapping

I have implemented my custom hook "useReducerWithCallback"

export default function useReducerWithCallback(
  reducer,
  initialState,
  initializer
) {
  const [state, dispatch] = useReducer(reducer, initialState, initializer);

  const callbackRef = useRef(undefined);

  const customDispatch = (action, callback) => {
    callbackRef.current = callback;
    console.log("Callback updated"); // <-------
    dispatch(action);
  };

  useEffect(() => {
    console.log("Executing callback"); // <--------
    callbackRef.current?.(state);
  }, [state]);

  return [state, customDispatch];
}

which basically works like the typical useStateWithCallback but with a useReducer.

I am using this hook in a Context Provider, which is consumed in a screen that is inside a stack navigator.

This screen has a listener to my db, and when it is triggered, the screen communicates with my context provider reducer, passing a custom callback.

The problem I am having is when I push an other instance of the same screen in my stack navigator, as both screen listen to the same part of the DB, they communicate with the context provider at the same time, and their callbacks are overlapped.

Callbacks Overlapping

This is my context provider:

function contentsReducer(contents, action) {
   ...
}

export function ContentsProvider({ children }) {
  const [contents, dispatch] = useReducerWithCallback(
    contentsReducer,
    new Map([])
  );

  ...

  const addContents = (contents, callback = undefined) => {
     console.log("Adding contents"); <------
     dispatch(
       {
         type: "add-contents",
         contents,
       },
       callback
     );
   };

  ...

}

The stack screen 1 and 2 (the last pushed one) are the same. Both communicate with the context provider using its method "addContents", which you see in the previous code, but for some reason, the callbacks are overlapped.

This is the result of the useReducerWithCallback:

Adding contents
Callback updated
Adding contents
Callback updated
Executing callback
Executing callback

When it should be:

Adding contents
Callback updated
Executing callback
Adding contents
Callback updated
Executing callback

Any ideas? I think that the problem is because of the centralization of the reducer in one unique point, the context provider, but I don't know how to fix this.

Thank you.

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