4

I'm new to the reactJS and I'm making simple To-do App,I already implemented Edit and Delete and now I'm stuck on Pagination,I'm trying to display new button on certain circumstances but It's not working and I'm confused. The problem is It is not displaying a new button

class App extends Component {
state = {
    inputValue: '',
    todos: [],
    currentPage:1,
    pageCount:1,
};

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

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

paginationDisplay = () => {
    console.log('Hello world')

addItem = () => {
        let {inputValue, todos} = this.state
        this.setState({todos: [...todos, {inputValue, id: uuidv4()}]})
        this.setPageCount()
        this.renderPagination()

    }

render() {
        return <div className={"App"}>
            <div className="App-header">
                <h2>Welcome to To-Do List App</h2>
            </div>
            <input onChange={this.handleInputValue} name={''} type='text'/>
            <button onClick={() => this.addItem()} className={'btn btn-primary'}>Add</button>
            <ul>
                {
                    this.state.todos.map(todoItem => <ListItem
                        key={todoItem.id}
                        todoItem={todoItem}
                        deleteItem={this.deleteItem}
                        editItem={this.editItem}
                        submitEdit={this.submitEdit}
                    />)
                }
            </ul>
        </div>
    };
}
4
  • What is the error? Commented Jun 11, 2020 at 7:01
  • It is not Displaying the new button Commented Jun 11, 2020 at 7:01
  • can you show your render method? Commented Jun 11, 2020 at 7:05
  • render() { return <div className={"App"}> <div className="App-header"> <h2>Welcome to To-Do List App</h2> </div> <input onChange={this.handleInputValue} name={''} type='text'/> <button onClick={() => this.addItem()} className={'btn btn-primary'}>Add</button> <ul> { this.state.todos.map(todoItem => <ListItem key={todoItem.id} todoItem={todoItem} deleteItem={this.deleteItem} editItem={this.editItem} submitEdit={this.submitEdit} />) } </ul> </div> }; } Commented Jun 11, 2020 at 7:08

1 Answer 1

2

You need to move renderPaginator function to render like this.

addItem = () => {
    let { inputValue, todos } = this.state;
    this.setState({ todos: [...todos, { inputValue, id: uuid() }] });
    this.setPageCount();
  };
  render() {
    return (
      <div className={"App"}>
        <div className="App-header">
          <h2>Welcome to To-Do List App</h2>{" "}
        </div>
        <input onChange={this.handleInputValue} name={""} type="text" />{" "}
        <button onClick={() => this.addItem()} className={"btn btn-primary"}>
          Add
        </button>
        <ul>
          {" "}
          {this.state.todos.map(todoItem => (
            <div
              key={todoItem.id}
              todoItem={todoItem}
              deleteItem={this.deleteItem}
              editItem={this.editItem}
              submitEdit={this.submitEdit}
            />
          ))}
        </ul>
        {this.renderPagination()}
      </div>
    );
  }

Every time you update the state using setState function render function is called.

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.