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

193
Views
How to change just one parameter using Redux?

I try to update my global state using Redux, but I try to update just one paramter of five, not all.

My store code looks like:

const initialState = {
    loggedIn: false,
    thisUser: {}
}

export function usersReducer(state = initialState, action) {
    switch (action.type) {
        case 'users/loggedIn':
            return { ...state, loggedIn: action.payload }
        case 'users/addUser':
            return { ...state, thisUser: action.payload }
        default:
            return state
    }
}

I tried to write a new case like, but doesn't work:

case 'users/setActivated':
            return { ...state, thisUser.activated: action.payload }

VS Code doesn't let me write that ".activated"

My dispatch look like:

dispatch({ type: 'users/setActivated', payload: 1 })

What is wrong?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Well, your syntax is wrong, that's what. :)

case 'users/setActivated':
    return { ...state, thisUser: {...state.thisUser, activated: action.payload} }

is what you need to shallow-merge state with a shallow-merged nested object.

Note that it quickly gets very old to dig into objects like that, and you might want to look at e.g. immer.

about 4 years ago · Juan Pablo Isaza Report

0

I understand why you did this, it seemed logical to you, but it will not work.

 const state = {
    ...state,
    [thisUser.activated]: action.payload
  };

So your goal is to update the state to be like this:

{
    loggedIn: true,
    thisUser: {
      //other properties ,
      activated: 1,
    }
  }

Firstly, this is the output of what you did:

{
    loggedIn: true,
    thisUser: {
      //other properties ,
      activated: 0,
    },
    activated: 1,
  };

Secondly, JavaScript doesn't accept this thisUser.activated as a key.

The solution:

 {
    ...state,
    thisUser: { 
      ...state.thisUser, 
      activated: action.payload
    },
  };
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!