Javascript 如何在ReactJS中处理道具?
一般来说,我对ReactJS和JavaScript框架非常陌生。我正在使用Django后端和使用限制和偏移量的API 我创建了一个可以很好地处理这些分页的函数:Javascript 如何在ReactJS中处理道具?,javascript,reactjs,Javascript,Reactjs,一般来说,我对ReactJS和JavaScript框架非常陌生。我正在使用Django后端和使用限制和偏移量的API 我创建了一个可以很好地处理这些分页的函数: handlePageClick(data) { this.setState({ offset: Math.ceil((data-1) * this.state.limit), page: data}, function(){
handlePageClick(data) {
this.setState({
offset: Math.ceil((data-1) * this.state.limit),
page: data},
function(){
console.log('/api/results?limit='+ this.state.limit +'&offset=' + this.state.offset)
}
)
}
还可以使用,使网页非常容易
<Pagination
activePage={this.props.page}
itemsCountPerPage={this.props.limit}
totalItemsCount={this.props.count}
pageRangeDisplayed={5}
onChange={this.handlePageClick}
/>
然后我使用推荐的组件WillReceiveProps
componentWillReceiveProps(newProps){
if (newProps.offset !== this.props.offset) {
const pagination = {
limit: this.props.limit,
offset: this.props.offset,
};
this.props.getBy(pagination)
}
}
但是现在我找不到更新变量以更改页面的方法
我能做什么?您需要编码到componentWillReceiveProps中。 现在,在页面更改中,若要调用api以获取更多数据,则可以这样做,也可以通过setState更改状态
componentWillReceiveProps(newProps){
if (newProps.offset !== this.props.offset) {
const pagination = {
limit: this.props.limit,
offset: this.props.offset,
};
this.setState({limit:this.props.limit,offset: this.props.offset},
() => {
this.props.getBy(limit, offset);
});
}
}
我不知道你想做什么,tbh。如果要在状态更改后执行操作,请将其作为
this.setState()
什么是this.props.getBy
?我最初的猜测是,您需要调用一个API来获取分页数据,因此(a)使用API数据调用setState
的回调应该传递给API调用,或者(b)您可以使用wait/async
在线获取此数据。
componentWillReceiveProps(newProps){
if (newProps.offset !== this.props.offset) {
const pagination = {
limit: this.props.limit,
offset: this.props.offset,
};
this.setState({limit:this.props.limit,offset: this.props.offset},
() => {
this.props.getBy(limit, offset);
});
}
}