Javascript 我需要在哪里添加键属性?
我有两个react组件,一个外部组件称为UserManagement,另一个组件称为UserList,它曾经用作子组件。我在我的Chrome控制台中收到了一条熟悉的错误消息:Javascript 我需要在哪里添加键属性?,javascript,reactjs,mobx-react,Javascript,Reactjs,Mobx React,我有两个react组件,一个外部组件称为UserManagement,另一个组件称为UserList,它曾经用作子组件。我在我的Chrome控制台中收到了一条熟悉的错误消息: Each child in an array or iterator should have a unique "key" prop. Check the render method of `UserList`. 下面是我的UserList的渲染方法: render() { const { users, filter
Each child in an array or iterator should have a unique "key" prop. Check the render method of `UserList`.
下面是我的UserList
的渲染方法:
render() {
const { users, filteredUsers, filter } = this.props.userStore;
return (
<div>
{ filteredUsers.map(user=><div key={user.id}>{user.username}</div>) }
</div>
);
}
确保您的user.id是唯一的,这是一个唯一的MongoDb GUID,而且该列表中只有一个用户a.t.m。在UserList中,您使用的props是
const{users,filteredUsers,filter}=this.props.userStore
但是您通过名称store
传递它,很抱歉,我是从旧版本复制代码的,userStore传递正确,我已经编辑了问题中的代码部分。我会再次检查{user.id}
是否确实有值,您还可以尝试将map函数的增量索引作为键传递,以便对其进行测试
render() {
const { users, filteredUsers, filter } = this.props.userStore;
return (
<div>
<h4>User Management:</h4>
<UserList userStore={this.props.userStore} />
</div>
);
}