我正在尝试更新一个数字onclick,以便在单击";加载更多按钮";,然而,当实例数量>数组中返回的数字。我已经尝试让这两个更新在这里发生:
<a className="LoadMoreButton" onClick={() => this.setState({ VisibleNo: this.state.VisibleNo+10})} style={{display: this.state.LoadMoreVis}}>Load More</a>
调用的函数为:
allloaded = () => {
if (this.state.data.length < this.state.VisibleNo)
this.setState({LoadMoreVis: 'none'})
}
有没有一种简单的方法可以让这两个函数都成功地运行onclick?
您应该使用条件呈现来解决这个问题,同时在onClick事件上执行这两个函数。
checkAllLoaded = () => {
if (this.state.data.length < this.state.VisibleNo)
this.setState(prevState => ({...prevState, LoadMoreVis: 'none'}));
}
handleClick = () => {
this.setState(prevState => ({...prevState, VisibleNo: this.state.VisibleNo+10}));
checkAllLoaded();
}
{this.state.LoadMoreVis !== "none" && <a className="LoadMoreButton" onClick={handleClick} style={{display: this.state.LoadMoreVis}}>Load More</a>}