I'm trying to code "blockchain" visual implementation in React. I want to hold somehow inside array of objects(that are my blocks) index of each object. I can't figure out how to do it.
this.state = {
value: '',
blocks: [{
hash: calculateHash(1),
timestamp: timeStamp(),
dataOfBlock: 'Genesis Block',
nounce: 607,
index: 0
}]
}
};
addBlock = (event) => {
event.preventDefault();
this.setState({
blocks: [...this.state.blocks, {
hash: 'cos',
timestamp: timeStamp(),
dataOfBlock: this.state.value,
nounce: '',
index: 1 // here's the problem
}]
})
}
My code mimics what i want to accomplish where the comment is. I want to constantly add +1 to my index with each block.
index: this.state.blocks.length+1, suggestion: useprevStateinstead ofthis.stateinsidesetState.