2

I'm new to the react and I'm making simple To-Do app application,I already implemented Add,Delete,Edit and now I'm working on Pagination. The problem is I'm trying to create Loop which will create several Buttons for Pagination,For example:If there is 5 list items There shall be 1 button,if there is 10 list items then there shall be 2...etc...

I tried to do:

state = {
        inputValue: '',
        todos: [],
        currentPage:1,
        pageCount:1,
    };

    inpRef = createRef();

    setPageCount = () => {
            let {todos} = this.state
          this.setState({pageCount: Math.ceil(todos.length / 5)})
            console.log('--------this.state.pageCount', this.state.pageCount );
    }

paginationDisplay = () => {
        console.log('helo')
    }

        renderPagination = () => {
            let {pageCount,currentPage} = this.state
            for (let i= 1;i<pageCount; i++){
                <button onClick={() => {
                this.paginationDisplay()
                currentPage = i}
                }>
                    {pageCount}
                </button>
            }



        }

            render() {
            const { todos } = this.state;

            return <div className={"App"}>
                <div className="App-header">
                    <h2>Welcome to To-Do List App</h2>
                </div>
                <input ref={this.inpRef} onKeyPress={this.handleKeyPress} onChange={this.handleInputValue} name={''} type='text'/>
                <button onClick={() => this.addItem()} className={'btn btn-primary'}>Add</button>
                <ul>
                    {
                        todos.map(todoItem => <ListItem
                            key={todoItem.id}
                            todoItem={todoItem}
                            deleteItem={this.deleteItem}
                            editItem={this.editItem}
                            submitEdit={this.submitEdit}
                        />)
                    }
                    {this.renderPagination()}
                </ul>
            </div>
        };
    }

It's not working and I don't know how fix my Loop.Please help

3
  • Could you provide your whole component code? Commented Jun 11, 2020 at 10:08
  • 1
    you never return the buttons elements in renderPagination try return pageCount.map((page, index) => {return <button />}) Commented Jun 11, 2020 at 10:09
  • I posted whole component code,also thank you for the answer Hai alaluf Commented Jun 11, 2020 at 10:11

1 Answer 1

3

The renderPagination() does not return component to be rendered, try like this

renderPagination = () => {
    let {pageCount,currentPage} = this.state

    const paging = []

    for (let i= 1;i<pageCount; i++){
      paging.push(
        <button 
           onClick={() => {
            this.paginationDisplay()
            currentPage = i
           }
         }>
            {pageCount}
         </button>
      )
    }

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

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.