I have the following declaration:
constructor(props) {
super(props);
this.state = {
productArray: [{
barcode: '',
name: ''
}],
numberOfRecords: '',
return_code: '',
return_string: ''
};
}
My hope is to reference the state fields like this:
this.state.productArray[0].barcode and this.state.productArray[1]
I also have a piece of code where I try to update the state. The code looks like this:
for (counter=0;counter<numberOfRecords;counter++) {
currentComponent.setState({productArray[counter].barcode: tempData.barcode[counter]});
currentComponent.setState({productArray[counter].name: tempData.name[counter]});
}
This does not compile and the error is pointing to the first setState command. The compiler is pointing at the [ in the reference to productArray[counter].barcode saying it expected a ','.
Am I defining the state correctly? If not, what would the proper syntax be? If yes, what is proper syntax to reference the individual state field pieces?
numberOfRecordsan empty string and not a number? and how about just settingcurrentComponent.setState({ productArray: tempData })?numberOfRecordsreally state, or can it be easily computed? LiketempData.lengthfor example?