javascript - 参考错误“'状态中的数组名称'未定义”
问题描述
我似乎无法在我的数组中设置属性的状态,因为 react 认为它是未定义的,或者我猜它显然是未定义的,谁能告诉我我的代码有什么问题!
构造函数中的状态
constructor(props){
super(props);
this.state = {
listOfItems: [],
listOfEquiptment: [],
listOfSpells: [],
choosableEquiptment:[],
choosableSpells: [],
currentEquiptment: undefined,
currentSpell: undefined
}
this.addNewEquiptment =this.addNewEquiptment.bind(this);
this.addNewSpell =this.addNewSpell.bind(this);
this.handleEquiptmentChange = this.handleEquiptmentChange.bind(this);
this.handleSpellChange = this.handleSpellChange.bind(this);
}
**我正在尝试重新分配的财产**
if(prevProps.chosenCharacter != this.props.chosenCharacter){
axios.get(`/api/${this.props.chosenUser.name}/characters/${this.props.chosenCharacter.name}`)
.then(results =>{
let newArr = results.data;
// newArr.sort((a, b) => {
// return (a.item > b.item ? 1 : -1)
// })
console.log("arr after sort is", newArr)
this.setState({
listOfItems: newArr
});
console.log('list of items after set state ', listOfItems)
.then(results =>{
const tempEquipArr = [];
const tempSpellArr = [];
for(const item of listOfItems){
(item.type == "spell")? tempSpellArr.push(item.name) : tempEquipArr.push(item.name);
}
})
this.setState({
listOfEquiptment: tempEquipArr,
listOfSpells:tempSpellArr
})
})
.catch(err =>{
console.error(err);
})
}
解决方案
推荐阅读
- html - HTML/CSS - 让警报在几秒钟后消失
- python - 计算图像中两点之间的白色像素
- xamarin - Xamarin android在启动时挂起
- c++ - std::vector 的前地址是否移动不变?
- c# - 数字到单词程序
- objective-c - 将dismissViewController 与UIAlertController 一起使用
- php - 在 PHP 代码中插入图像
- linux - 将大括号扩展与过程替换相结合
- java - 如果xml文件中包含超过4000个字符,如何从DB中获取java中clob数据类型的完整xml
- armadillo - dgemm nvblas gpu 卸载