【发布时间】:2017-12-30 12:09:33
【问题描述】:
我正在尝试通过单击一些调用方法来增加状态值的文本来进行分页。然后将状态值传递给 axios 调用,然后该调用应该调用下一页。然而,我注意到,虽然从渲染函数的 console.log 中状态开始增加,但 axios 调用并没有被新的状态值再次调用。有人知道我该如何解决这个问题吗?
constructor(props) {
super(props);
this.state = {
people: [],
planets: [],
page: 1
};
this.pageIncrementer = this.pageIncrementer.bind(this);
}
componentWillMount() {
let page = this.state.page;
axios({
method: 'GET',
url: `http://localhost:3008/people?_page=${page}&_limit=10`
}).then((response) => {
this.setState({
people: response
});
}).catch((error) => {
console.log('There is an error in the Card axios call for people: ', error);
})
axios({
method: 'GET',
url: `http://localhost:3008/planets?_page=${page}&_limit=10`
}).then((response) => {
this.setState({
planets: response
});
}).catch((error) => {
console.log('There is an error in the Card axios call for planets: ', error);
})
}
pageIncrementer() {
this.setState({
page: this.state.page + 1
});
}
【问题讨论】:
标签: javascript reactjs axios