1

I'm trying to make pagination for my web-page,I've set limit to 10 items per page. Problem is whenever I'm adding 11th item New page button is not being displayed,It only appears after I add 12th item.

Any suggestions?

Add 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 :

    case actionTypes.PAGE_COUNT:
    const setPageCount =  Math.ceil(todos.length / 10 )
    return {
        ...state,
        pageCount: setPageCount
    }
2
  • 2
    Look at the answer I've added. Thanks Commented Jun 18, 2020 at 10:07
  • It worked,Thank you Very much,If you want you can add this to the Answers so I can upvote it and accept the answer Commented Jun 18, 2020 at 10:09

2 Answers 2

1

Why are you adding extra 1 here const setPageCount = Math.ceil(todos.length / 10 ) + 1. I think you should use floor instead ceil. As you can see if you have 1 todo item. Math.ceil(1/10) + 1 = 2 which means you have only 1 item but you're showing 2 pages.

Sign up to request clarification or add additional context in comments.

Comments

1

It will work asynchronously, so make it as synchronous with the use of promise or async/await.

This link below contains documentation on async.

Click this

Hope this will helpful for your process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.