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

188
Vistas
how to mutate state in react-redux?

i am using createSlice way to implement redux for the first time and i am trying to mutate state in the store which is not working and the object that i am updating is getting undefined. please help me fix it

[save.fulfilled]:(state,action)=>{
      console.log("inside save it");
      console.log(action.payload,"test");
      console.log(current(state));
      current(state).list.map((C,i)=>{
        console.log(C._id,"ii");
        if(C._id==action.payload._id){
          const temp=Object.assign({},C);
          temp=action.payload.obj
          return temp;
        }
        return C;
      })
      console.log(current(state).list);
    }

if id matches i need to mutate a key

Object { form: {…}, _id: "617ce521028ba1920422fb6b" }
​
_id: "617ce521028ba1920422fb6b"
​
form: Object { name: "adarsh hh", email: "adarsh@gmail.com", phone: "8340484685", … }
​

above is the action object,below is the state

Object { list: (2) […], status: "succeeded" }
​
list: Array [ {…}, {…} ]
​​
0: Object { _id: "617ce521028ba1920422fb6b", userid: "616548965d47a2b6ec0a1592", key: 0, … }
​​​
__v: 0
​​​
_id: "617ce521028ba1920422fb6b"
​​​
created: "2021-10-30T06:24:33.922Z"
​​​
form: Object { name: "adarsh raii", email: "adarsh@gmail.com", phone: "8340484685", … }
​​​
key: 0
​​​
updated: "2021-10-30T06:24:33.922Z"
​​​
userid: "616548965d47a2b6ec0a1592"
​​​
<prototype>: Object { … }
​​
1: Object { _id: "617ce5a2028ba1920422fb72", userid: "616548965d47a2b6ec0a1592", key: 0, … }
​​​
__v: 0
​​​
_id: "617ce5a2028ba1920422fb72"
​​​
created: "2021-10-30T06:26:42.600Z"
​​​
form: Object { name: "adarsh raj", email: "adarsh@gmail.com", phone: "8340484685", … }
​​​
key: 0
​​​
updated: "2021-10-30T06:26:42.600Z"
​​​
userid: "616548965d47a2b6ec0a1592"
​​​
<prototype>: Object { … }
​​
length: 2
​​
<prototype>: Array []
​
status: "succeeded"
​
<prototype>: Object { … }

i just want to mutate the form object of a particular field object by matching the _id so as to update it in dom

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

0

if cant mutate the redux state, you've to copy the state you need to work on your logic. i think below code would suffice your requirement let me know.

[save.fulfilled]:(state,action)=>{
      console.log("inside save it");
      console.log(action.payload,"test");
      console.log(current(state));
      let temp= Object.assign({},current(state));
      temp.list.forEach((C,i)=>{
        console.log(C._id,"ii");
        if(C._id==action.payload._id){
          temp.list[i] = action.payload.obj
        }
       
      })
    return temp; 
      console.log(current(state).list);
    }
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