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

78
Vistas
How to handle useContext properly with frequent changes?

I am using useContext for global state management with useReducer.I am using WebSocket to retrieve bitcoin data every second.Because of the data changing frequently, I am getting my app re-rendered every second and I have heavy data calculations, complex UI with tables and etc.Even though not any component is a consumer for bitcoin data, anyway every component is being re-rendered every time it's data is changed.

this is the context provider.

const AppContext = createContext<{
  state: InitialStateType;
  dispatch: Dispatch<ActionType>;
}>({
  state: initialState,
  dispatch: () => null,
});

const mainReducer: MainReducer = ({btc, common, user }, action) => ({
  btc: btcReducer<BTCPayload>(btc, action as BTCReducerActions),
  user: userReducer<UserPayload>(user, action as UserReducerActions),
  common: commonReducer<CommonPayload>(common, action as CommonReducerActions),
});

const AppProvider: React.FC = ({ children }) => {
  const [state, dispatch] = useReducer(mainReducer, initialState);

  return (
    <AppContext.Provider value={{ state, dispatch }}>
      {children}
    </AppContext.Provider>
  );
};

export { AppProvider, AppContext };

And here is the wrapper for the app.

<AppProvider>
  <ThemeProvider theme={theme}>
    <GlobalStyles />
    <BrowserRouter>
      <div className="App">
        <AppRoot isBusy={!ready || !isConnected} />
      </div>
    </BrowserRouter>
  </ThemeProvider>
</AppProvider>

I have tried to create separate providers one for the btc data, and the another one for the rest of the data, but it's working the same, because I guess the reason is that the whole app is inside that context provider.

const AppProvider: React.FC = ({ children }) => {
  const [state, dispatch] = useReducer(mainReducer, initialState);

  const appValue = useMemo(() => {
    return { state, dispatch };
  }, [state]);

  const btcValue = useMemo(() => {
    return { btc };
  }, [btc]);

  return (
    <AppContext.Provider value={appValue}>
      <BtcContext.Provider value={btcValue}>
        {children}
      </BtcContext.Provider>
    </AppContext.Provider>
  );
};

I am struggling almost 2 days.I need to wrap the whole app with the provider because there are more than 10 components using btc data in different routes.

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