简体   繁体   English

ReactJS 中的按钮显示不正确

[英]Button is not displaying correctly in ReactJS

I'm trying to make pagination for my web-page,I've set limit to 10 items per page.我正在尝试为我的网页进行分页,我已将每页限制为 10 个项目。 Problem is whenever I'm adding 11th item New page button is not being displayed,It only appears after I add 12th item.问题是每当我添加第 11 项时,新页面按钮都不会显示,它仅在我添加第 12 项后出现。

Any suggestions?有什么建议么?

Add Function:添加Function:

    addItem = () => {
        const {addItems, setPageCount} = this.props.actions;
        let userName = localStorage.getItem('username')

        if (this.inpRef.current.value === '') {
            return alert('We dont do that here....')
        } else {
            axios
                .post(`http://localhost:8080/add`, {
                    username: userName,
                    todo: this.inpRef.current.value,
                    checked: false,
                })
                .then((res) => {
                    const {todo, checked, _id} = res.data;
                    addItems(todo, checked, _id);
                    console.log('res', res)
                })
                .catch((err) => {
                    console.log("err", err);
                });
            this.inpRef.current.value = "";
            setPageCount()

        }
    }

Slicing in render:在渲染中切片:

    const {todos, currentPage, itemsPerPage} = this.props;
    const indexOfLastItem = currentPage * itemsPerPage;
    const indexOfFirstItem = indexOfLastItem - itemsPerPage;
    const currentItems = todos.slice(indexOfFirstItem, indexOfLastItem);

setPageCount function: setPageCount function:

    case actionTypes.PAGE_COUNT:
    const setPageCount =  Math.ceil(todos.length / 10 )
    return {
        ...state,
        pageCount: setPageCount
    }

It will work asynchronously, so make it as synchronous with the use of promise or async/await.它将异步工作,因此使用promiseasync/await 使其同步。

This link below contains documentation on async.下面的此链接包含有关异步的文档。

Click this 点击这里

Hope this will helpful for your process.希望这对您的过程有所帮助。

Why are you adding extra 1 here const setPageCount = Math.ceil(todos.length / 10 ) + 1 .为什么要在此处添加额外的 1 const setPageCount = Math.ceil(todos.length / 10 ) + 1 I think you should use floor instead ceil .我认为你应该使用floor而不是ceil As you can see if you have 1 todo item.如您所见,您是否有 1 个待办事项。 Math.ceil(1/10) + 1 = 2 which means you have only 1 item but you're showing 2 pages. Math.ceil(1/10) + 1 = 2这意味着您只有 1 个项目,但您正在显示 2 页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM