我正在调用一个 API 端点,将它的数据保存到一个状态,然后呈现它。它显示在浏览器中,但控制台上有一个警告:Warning: Each child in a list should have a unique "key" prop.
。
我的app.js
:
class App extends Component {
render () {
return (
<div>
<Profile profiles={this.state.profile} />
</div>
)
}
state = {
profile: []
};
componentDidMount() {
fetch('http://127.0.0.1:8000/profiles')
.then(res => res.json())
.then((data) => {
this.setState({ profile : data })
})
.catch(console.log)
}
}
export default App;
我不明白将关键props放在 render() 中的什么位置。这是我的片段profile.js
:
const Profile = ({ profiles }) => {
return (
<div>
<center><h1>Profiles List</h1></center>
{profiles.map((profile) => (
<div className="card">
<div className="card-body">
<h5 className="card-title">{profile.first_name} {profile.last_name}</h5>
<h6 className="card-subtitle mb-2 text-muted">{profile.dob}</h6>
<p className="card-text">{profile.sex}</p>
</div>
</div>
))};
</div>
)
};
export default Profile;
关键props不使用它会带来什么改进?我对这些<div>...</div>
标签不知所措。