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

140
Vistas
How to update just one object from a list of objects in Redux by id?

I build an app in React with Redux and in my state I have a list of objects and I want to update one object from that list by unique id.

My object looks like:

{
id: '',
title: '',
description: '',
label: '',
}

My state:

const initialState = {
    compare: dayjs().month(),
    savedEvents: [],
}

When I push a new event in that list I use:

case 'events/setNewEvent':
            return { ...state, savedEvents: [...state.savedEvents, action.payload] };

My problem is that I don't know to write the right code to update just one object by id sent from my form.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use combination of Array method map and spread operator

function updateOne(array, obj) {
  return array.map((item) => {
    if (obj.id === item.id) {
    // update whatever you want
    return {...item, title: obj.title };
   } else {
    return item;
   }
 })
}

reducer:

case 'events/setNewEvent':
   return { 
    ...state, 
    savedEvents: updateOne(state.savedEvents, action.payload) 
  };

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