Javascript reactJS对象的状态数组-如何引用setState()中的状态数组对象
我有以下声明:Javascript reactJS对象的状态数组-如何引用setState()中的状态数组对象,javascript,reactjs,Javascript,Reactjs,我有以下声明: constructor(props) { super(props); this.state = { productArray: [{ barcode: '', name: '' }], numberOfRecords: '', return_code: '', return_string: '' }; }
constructor(props) {
super(props);
this.state = {
productArray: [{
barcode: '',
name: ''
}],
numberOfRecords: '',
return_code: '',
return_string: ''
};
}
我希望引用如下状态字段:
for (counter=0;counter<numberOfRecords;counter++) {
currentComponent.setState({productArray[counter].barcode: tempData.barcode[counter]});
currentComponent.setState({productArray[counter].name: tempData.name[counter]});
}
currentComponent.setState((state) => {
for(let i = 0; i<tempData.length; ++i){
let from = tempData[i], to = state.productArray[i];
to.name = from.name;
to.barcode = from.barcode;
}
return state;
});
this.state.productArray[0]。条形码和this.state.productArray[1]
我还有一段代码试图更新状态。代码如下所示:
for (counter=0;counter<numberOfRecords;counter++) {
currentComponent.setState({productArray[counter].barcode: tempData.barcode[counter]});
currentComponent.setState({productArray[counter].name: tempData.name[counter]});
}
currentComponent.setState((state) => {
for(let i = 0; i<tempData.length; ++i){
let from = tempData[i], to = state.productArray[i];
to.name = from.name;
to.barcode = from.barcode;
}
return state;
});
for(counter=0;counter您不能直接免疫该状态,请按如下操作
let tempvar = this.state.productArray;
for (counter=0;counter<numberOfRecords;counter++) {
tempvar[counter].barcode= newValue
}
this.setState({productArray:tempvar})
让tempvar=this.state.productArray;
对于(counter=0;counter理想情况下,您只需要对setState
进行一次调用,您可以创建一个临时变量来存储计算,例如
const { productArray, numberOfRecords } = this.state;
const newProducts = [ ... productArray ];
for( let counter = 0; counter < numberOfRecords; counter ) {
const item = newProducts[ counter];
newProducts[ counter] = { ...item, barcode: 'value' },
}
this.setState({ productArray: newProducts });
const{productArray,numberOfRecords}=this.state;
const newProducts=[…productArray];
for(让计数器=0;计数器
通过使用扩展运算符…
可以创建对象和数组的数组。更新对象嵌套数组的推荐方法是利用对象.assign的功能。请记住,该Object.assign不会执行深度克隆,因为它只复制属性值
this.setState({
productArray: Object.assign({}, this.state.productArray[0], {
barcode: 'any value',
})}, ()=>{
console.log(this.state)
}
);
在您的案例中,这就是如何使用速记符号实现这一点:
for (var counter = 0; counter < 1; counter++) {
this.setState({
productArray: Object.assign({}, this.state.productArray[counter], {
barcode: tempData.barcode[counter],
name: tempData.name[counter]
})}, ()=>{
console.log(this.state)
}
);
}
for(变量计数器=0;计数器<1;计数器++){
这是我的国家({
productArray:Object.assign({},this.state.productArray[计数器]{
条形码:tempData.barcode[计数器],
名称:tempData.name[计数器]
})}, ()=>{
console.log(this.state)
}
);
}
至于数据,我正在挑选tempData数组的特定部分
及
我可以从状态中删除numberOfRecords并从tempData.length中获取它
好的,productArray
包含来自tempData
的对象属性的子集,而不是项的子集
然后,我甚至不会费心去复制这些值,而只是复制新的值
currentComponent.setState({
productArray: tempData.map(item => {
const {barcode, name} = item;
return {barcode, name};
})
});
//or in short
currentComponent.setState({
productArray: tempData.map(({barcode, name}) => ({barcode, name}))
});
要回答您的问题,如果您想访问和更新某些嵌套属性,您应该使用setState()
中的更新函数,以便在代码尝试修改它之前,所有以前的更新都已应用于此.state
大概是这样的:
for (counter=0;counter<numberOfRecords;counter++) {
currentComponent.setState({productArray[counter].barcode: tempData.barcode[counter]});
currentComponent.setState({productArray[counter].name: tempData.name[counter]});
}
currentComponent.setState((state) => {
for(let i = 0; i<tempData.length; ++i){
let from = tempData[i], to = state.productArray[i];
to.name = from.name;
to.barcode = from.barcode;
}
return state;
});
currentComponent.setState((状态)=>{
for(设i=0;iwhy是numberOfRecords
一个空字符串而不是一个数字?只设置currentComponent.setState({productArray:tempData})怎么样
?我的错误在于numberOfRecords的默认值。我应该将其初始化为零。至于数据,我正在提取tempData数组的特定部分,因此我觉得没有必要将所有tempData数据保存在状态中。感谢您的澄清,这对我来说并不明显。numberOfRecords
真的是状态吗n它很容易计算?比如tempData.length
?很好的一点。我可以从state中删除numberOfRecords并从tempData.length中获取它。到目前为止,这是一个很重要的问题,请继续学习。我自己已经给出了,而不必进行肤浅的复制,您可以利用JS的功能。如果是这样的话,我将如何处理是箭头函数/map中的state.productArray,因此我可以动态呈现state.productArray中的数据?您可以正常呈现它,因为您不修改状态。您可以这样做。state.productArray.map(index=>index.barcode),因为我将此代码放置在componentWillMount()中在这个过程中,我不会在其他任何地方更新状态,我认为我不需要担心那个实例。