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

122
Vistas
why is state not being updated?

Slice

import { createSlice } from '@reduxjs/toolkit';

const initialState: { value: number } = { value: 0 };

export const testSlice = createSlice({
  name: 'test',
  initialState,
  reducers: {
    test: (state) => {
      state.value += 1;
    },
  },
});

export const { test } = testSlice.actions;

Combine

import { combineReducers } from '@reduxjs/toolkit';

import { authSlice } from './auth';
import { productsSlice } from './products';
import { testSlice } from './test';
import { userSlice } from './user';

export const rootReducer = combineReducers({
  user: userSlice.reducer,
  auth: authSlice.reducer,
  products: productsSlice.reducer,
  test: testSlice.reducer,
});

export type RootState = ReturnType<typeof rootReducer>;

Store

import { configureStore } from '@reduxjs/toolkit';
import storage from 'utils/storage';

import { rootReducer } from './rootReducer';

declare global {
  interface Window {
    store: any;
  }
}

const reHydrateStore = () => {
  if (storage.getToken() !== null) {
    return storage.getToken();
  }
  return false;
};

// !TODO add types
// @ts-ignore
const localStorageMiddleware = (store) => (next) => (action) => {
  if (action.type.match('auth')) {
    const result = next(action);
    storage.setToken(store.getState().auth.token);
    return result;
  }
  return next(action);
};

export const store = configureStore({
  reducer: rootReducer,
  preloadedState: {
    auth: {
      token: reHydrateStore() || '',
      loading: false,
    },
  },
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localStorageMiddleware),
});

window.store = store.getState();

When I output store.test to the console, the value remains old, but auth reducer works correctly. Also I see that the state after the update in the reducers is 0.

Why is state not being updated?

Above are examples of my code:

  1. Сreate Slice
  2. Combine reducers
  3. Store settings (and output store in console)
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