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

229
Views
Using spread operator to add new value to key value pair in object

Struggling to figure out how to add a new item to an object from my reducer. This is the payload I am receiving GH

This is the relevant code in my reducer

const initialForm = { 
    nationality: '',
}
function addName(state= initialForm, action) {
    switch(action.type){
        case ADD_NEW_NATIONALITY:
            console.log('nationality',action.payload.name)
            return {
                ...state,
                [action.payload.nationality]: action.payload.value
            }
        default:
            return state
    }   
}

and my action creators

export const addFullName = newName => dispatch => {
    console.log(newName)
    axios.get(`https://api.nationalize.io?name=${newName.name}`)
        .then(res => {
            dispatch({ type: ADD_NEW_NATIONALITY, payload: res.data.country[0].country_id})
        })

Please help me figure this out

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

0

Your payload is just a plain value (not an object), so you cannot find action.payload.name and action.payload.value

And similarly, you cannot find action.payload.nationality either.

Here is how your action looks like

{ type: ADD_NEW_NATIONALITY, payload: 1} //`1` is country_id

To set nationality correctly, you can check the below implementation

const initialForm = { 
    nationality: '',
}
function addName(state= initialForm, action) {
    switch(action.type){
        case ADD_NEW_NATIONALITY:
            return {
                ...state,
                nationality: action.payload //update your `nationality` property with a plain value from `payload`
            }
        default:
            return 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!