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

132
Views
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 answers
Answer question

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 Report
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!