Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda