【发布时间】:2017-05-24 01:29:57
【问题描述】:
当调用删除按钮上的 onClick 时,我试图从数组中删除一个元素,当单击该按钮时,我收到 Uncaught TypeError: Cannot read property 'name' of null
为什么会出现这个错误?
removeData(key) {
console.log(key);
const data = this.state.data;
data[key] = null;
this.setState({ data });
}
renderData(key){
const user = this.props.data[key];
console.log(user.name);
console.log(user.id);
return(
<div key={key}>
<li> <strong> Name: </strong> {user.name},
<strong> ID: </strong> {user.id} </li>
<button onClick={() => this.props.removeData(key)}> Remove </button>
</div>
)
}
【问题讨论】:
-
这是什么?...
-
现在你的代码在你的
console.log('user.name);上失败了。所以你的this.props.data[key]出了点问题,我们都无法真正分辨出它是什么。 -
暗示
user在{user.name}中为空 -
@cpugourou - 看起来像
react
标签: javascript