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

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

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