Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!