0

I have made To-Do list with react, but when I submit new entry, it wont show on the list. I have no clue where the problem is.

class TodoList extends React.Component {
  constructor(){
    super();

This array doesn't get updated like it should:

    this.state ={
      items:["item1", "item2"]
    };
  }

Here I add new task:

  addTask = (task) => {
this.setState((prevState) => ({
  items: prevState.items.concat([task])
}));


}

  render(){
    return(
      <div>
        <AddTodo addNew ={this.addTask}/>
        <ShowEntries entries={this.state.items}/>
      </div>
    )
  }
}

class AddTodo extends React.Component {
  constructor(){
    super();
this.handeAddNew = this.handeAddNew.bind(this);
    this.state ={
      task: ""
    };
  }

Here I update the state, it works fine:

  update(e){
    this.setState({
      task: e.target.value
    });
  }

Here I have passed the addTask function as a prop. This might be where the problem is?

  handeAddNew(){
    this.props.addNew(this.state.task);
    this.setState({
      task: ""
    });
  }

  render(){
    return(
      <div>
        <input type="text"
        onChange={this.update.bind(this)}></input>
        <button onClick={this.handleAddNew}>Add</button>
      </div>
    )
  }
}

ShowEntries class works fine too:

class ShowEntries extends React.Component{
  render(){
    var list =this.props.entries.map((task) =>{
      return <li>{task}</li>
    });
    return(
      <div>
        <ul>
          {list}
        </ul>
      </div>
    )
  }
}

export default TodoList

Any ideas?

3 Answers 3

1

You should bind addTask function.

constructor(){
  super();
  this.addTask = this.addTask.bind(this);
}

Or change addTask to arrow function. like

addTask = (task) => {
  /* add task*/
}

handleAddNew also needs binding.

this.handleAddNew = this.handleAddNew.bind(this);
Sign up to request clarification or add additional context in comments.

1 Comment

I missed handleAddNew in AddTodo component... You should bind it too.
1

If you are relying on previous state to update the next state, you must use functional setState in React, like so:

  addTask = (task) => {
    this.setState((prevState) => ({
      items: prevState.items.concat([task])
    }));
  }

That, along with binding all functions used (preferrably inside the constructor itself), should solve your issue.

5 Comments

what is that prevState attribute?
thank you for letting me know from prevState, but unofortunately this didn't resolve my issue.
can you pleas share a working pen or fiddle so that we can debug your code ?
Ok why do you have items: prevState.items.concat([task]) instead of just items: prevState.items.concat(task) ? You are just passing a string ryt ? (although I think this is not the issue)
0

you misspelled handeAddNew.

make it handleAddNew also make sure you bind your methods handleAddNew = () => { //code }

1 Comment

if thats the issue we shoould close this as a typo

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.