0

I've been trying to use setState to store multiple values inside an object. Each candidate has all four fields stored inside a solidity array and retrieved using an instance:

   this.state = {
  candidates: {
    Fname: "",
    Sname: "",
    RegNo: "",
    Dept: ""
  }
}

// retrieving the various candidates
this.myInstance.candiNumber().then((candiNum)=> {
    for(let i = 0; i < candiNum; i++){
        this.myInstance.candiArr(i).then((candidate)=>{
            let newCandidate = Object.assign({}, this.state.candidates)
            newCandidate.Fname = candidate[0],
            newCandidate.Sname = candidate[1],
            newCandidate.RegNo = candidate[2],
            newCandidate.Dept = candidate[3]
            this.setState({ newCandidate })
        })
    }
})

My question is; how do I add each candidate to state without replacing the other candidate values?

Kimzy
  • 11
  • 1
  • https://stackoverflow.com/questions/26253351/correct-modification-of-state-arrays-in-reactjs – Kais Jan 14 '20 at 13:53
  • Does this answer your question? [Correct modification of state arrays in ReactJS](https://stackoverflow.com/questions/26253351/correct-modification-of-state-arrays-in-reactjs) – Nosk Jan 14 '20 at 15:42

0 Answers0