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

136
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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