Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Cant access state inside reducer

I have createslice and reducer like this. I want to print my state inside reducer cause my dispatch is not working correctly but when i am trying to printing the state.event i am getting this.

[Proxy]
0: Proxy {}
length: 1
__proto__: Array(0)

export const eventSlice = createSlice({
      name: 'event',
      initialState: {
        event: [],
        favouriteEvents: [],
        isFetching: false,
        isSuccess: false,
        isError: false,
        errorMessage: '',
      },
    
      reducers: {
        alterFavourite: (state, action) => {
       // item = state.event.filter((item) => {
       //   item.pk == parseInt(pk)
       // item.is_liked = !is_liked;
       // })
    
        console.log(state.event)
      },
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use current function:

Immer exposes a named export current that creates a copy of the current state of the draft. This can be very useful for debugging purposes (as those objects won't be Proxy objects and not be logged as such). Also, references to current can be safely leaked from a produce function. Put differently, current provides a snapshot of the current state of a draft.

RTK uses immer underly and re-exports these functions. The state in the reducer is a draft object, it is some kind of wrapper or proxy of the JavaScript plain object.

You can do this:

import { configureStore, createSlice, current } from '@reduxjs/toolkit';

export const eventSlice = createSlice({
  name: 'event',
  initialState: {
    event: [],
    favouriteEvents: [],
    isFetching: false,
    isSuccess: false,
    isError: false,
    errorMessage: '',
  },

  reducers: {
    alterFavourite: (state, action) => {
      // console.log(state.event);
      console.log('current state.event: ', current(state.event));
    },
  },
});

const store = configureStore({
  reducer: {
    event: eventSlice.reducer,
  },
});
store.dispatch(eventSlice.actions.alterFavourite(null));

Output:

current state.event:  []

package version: "@reduxjs/toolkit": "^1.6.1"

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda