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

135
Visualizações
use empty object as initialState in createSlice

I'm trying write reducer for ticket object in my app and I use createSlice for it like this:

    export const ticketSlice = createSlice({
    name: 'ticket',
    initialState: { value: null },
    reducers: {
        ticketSet: (state, action) => {
            state.value = action.payload;
        },
        ticketSetCategory: (state, action) => {
            state.value.category = action.payload;
        }
    }
});

Everything works fine but it's not so convenient to use state.ticket.value in useSelector. I prefer state.ticket instead it. So I changed my reducer like it:

    export const ticketSlice = createSlice({
    name: 'ticket',
    initialState: {},
    reducers: {
        ticketSet: (state, action) => {
            state = action.payload;
        },
        ticketSetCategory: (state, action) => {
            state.category = action.payload;
        }
    }
});

After that, the storage stopped working. The ticket value is always an empty object. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

state = anything is never a valid statement in an Immer-powered reducer, because it is neither mutating the state, nor returning a new value.

The short answer is that you want return action.payload here to replace the existing state.

For more details, see the extended writeup on this topic in our docs:

https://redux-toolkit.js.org/usage/immer-reducers#resetting-and-replacing-state

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