Javascript 按钮在ReactJS中未正确显示
我正在尝试对我的网页进行分页,我已将每页限制为10项。 问题是,每当我添加第11项“新建页面”按钮不显示时,它只在我添加第12项后出现 有什么建议吗 添加功能:Javascript 按钮在ReactJS中未正确显示,javascript,reactjs,button,Javascript,Reactjs,Button,我正在尝试对我的网页进行分页,我已将每页限制为10项。 问题是,每当我添加第11项“新建页面”按钮不显示时,它只在我添加第12项后出现 有什么建议吗 添加功能: addItem=()=>{ const{addItems,setPageCount}=this.props.actions; 让userName=localStorage.getItem('userName') 如果(this.inpRef.current.value==''){ 返回警报(“我们在这里不这样做…”) }否则{ axio
addItem=()=>{
const{addItems,setPageCount}=this.props.actions;
让userName=localStorage.getItem('userName')
如果(this.inpRef.current.value==''){
返回警报(“我们在这里不这样做…”)
}否则{
axios
.邮政(`http://localhost:8080/add`, {
用户名:用户名,
todo:this.inpRef.current.value,
勾选:假,
})
。然后((res)=>{
const{todo,checked,_id}=res.data;
附加项(todo、选中、id);
console.log('res',res)
})
.catch((错误)=>{
console.log(“err”,err);
});
this.inpRef.current.value=“”;
setPageCount()
}
}
渲染中的切片:
const{todos,currentPage,itemsPerPage}=this.props;
const indexOfLastItem=currentPage*itemsPerPage;
常量indexOfFirstItem=indexOfLastItem-itemsPerPage;
const currentItems=todos.slice(indexOfFirstItem,indexOfLastItem);
setPageCount函数:
case actionTypes.PAGE_COUNT:
const setPageCount = Math.ceil(todos.length / 10 )
return {
...state,
pageCount: setPageCount
}
它将异步工作,因此使用promise或async/await使其同步。 下面的链接包含有关异步的文档
希望这对您的流程有所帮助。为什么要在此处添加额外的1
const setPageCount=Math.ceil(todos.length/10)+1
。我认为你应该用floor
来代替ceil
。如您所见,如果您有1个待办事项Math.ceil(1/10)+1=2
这意味着您只有一项,但显示了两页。看看我添加的答案。谢谢你,非常感谢,如果你愿意,你可以把这个添加到答案中,这样我就可以投票并接受答案