我的组件渲染了4次,这似乎是由于获取和多个setState函数引起的。如何仅使用一个setState()包括所有提取的数据?
我已经尝试创建一个函数而不是一个函数,创建三个变量来存储获取结果并将它们传递给setState,但这对我没有帮助。 预先感谢!
class DuelList extends React.Component {
state = {
duels: [],
users: [],
datasets: []
};
fetchDuels = () => {
axios.get("http://127.0.0.1:8000/api/duel/user/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
.then(res => {
this.setState({
duels: res.data
});
});
};
fetchUsers = () => {
axios.get("http://127.0.0.1:8000/api/user/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
.then(res => {
this.setState({
users: res.data
});
});
};
fetchDatasets = () => {
axios.get("http://127.0.0.1:8000/api/dataset/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
.then(res => {
this.setState({
datasets: res.data
});
});
};
componentDidMount() {
this.fetchDuels();
this.fetchUsers();
this.fetchDatasets();
}
render() {
return (
<div>
<Duels data={this.state.duels}/> <br/>
<h2> Add duel </h2>
<AddDuelForm users={this.state.users} datasets={this.state.datasets} requestType="post" articleID={null}
btnText="Challenge"/>
</div>
);
}
}
export default DuelList;```
答案 0 :(得分:3)
您需要将所有的承诺归为一组并加以解决:
fetchDuels = () => {
return axios.get("http://127.0.0.1:8000/api/duel/user/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
};
fetchUsers = () => {
return axios.get("http://127.0.0.1:8000/api/user/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
};
fetchDatasets = () => {
return axios.get("http://127.0.0.1:8000/api/dataset/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
};
componentDidMount() {
const promises = [this.fetchDuels(), this.fetchUsers(), this.fetchDatasets()];
Promise.all(promises).then(([ duelsReponse, usersResponse, datasetsReponse ]) => {
this.setState({ duels: duelsReponse.data, users: usersReponse.data, datasets: datasetsResponse.data });
});
}
这样,您减少了setState
个呼叫。
答案 1 :(得分:2)
一旦所有api都成功响应后,请使用Promise.all
和setState
class DuelList extends React.Component {
state = {
duels: [],
users: [],
datasets: []
};
fetchDuels = () => {
return axios.get("http://127.0.0.1:8000/api/duel/user/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
};
fetchUsers = () => {
return axios.get("http://127.0.0.1:8000/api/user/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
};
fetchDatasets = () => {
return axios.get("http://127.0.0.1:8000/api/dataset/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
};
componentDidMount() {
Promise.all([this.fetchDuels(), this.fetchUsers(), this.fetchDatasets()]).then(([duels, users, datasets]) => {
this.setState({
duels,
users,
datasets
})
});
}
render() {
return (
<div>
<Duels data={this.state.duels}/> <br/>
<h2> Add duel </h2>
<AddDuelForm users={this.state.users} datasets={this.state.datasets} requestType="post" articleID={null}
btnText="Challenge"/>
</div>
);
}
}
export default DuelList;