0

In order to get to know React better, I have been working on a todo app for some time. The work is stacking up as I try to implement the handleStatus function and the change of the status immutable.

The array TodoList contains two further arrays: openTodo and doneTodo. In the Todo component, todos are stored either in one or the other array according to their status.

Currently, the status is changed, but this is not done immutably. Thanks for the help

export class TodoTable extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    enterTodo: "",
    todos: this.props.todos,
    status: 'open'
  };
  this.handleEnterTodo = this.handleEnterTodo.bind(this);
  this.handleStatus = this.handleStatus.bind(this);
  this.createTodo = this.createTodo.bind(this);

}

handleEnterTodo(event) {
  
  this.setState({
    enterTodo: event.target.value
  });
}

handleStatus(event) {
  let status;
  if(event.status == 'done'){
    event.status = 'open';
  }
  else{
    event.status = 'done';
  }
  let todo = {
    id: event.id,
    describtion: event.describtion,
    status: status
  };
  
  

  this.setState({
    
  });
  
}

export class Todo extends React.Component {
render() {
  const openTodo = [];
  const doneTodo = [];
  const lineBreak = <hr></hr>
  const todoList = 
  [ 
    openTodo,
    lineBreak,
    doneTodo
  ];

  this.props.todos.forEach((element, index) => {
    if(this.props.todos[index].status == 'open'){

      let todoOpen = (
        <div>
          
          {this.props.todos[index].describtion}
          {this.props.todos[index].status}
          <button
            type= 'button'
            onClick = {() => this.props.handleStatus(this.props.todos[index])}
          ></button>
          {this.props.todos[index].id}
            
          
        </div>
      );
      todoList[0].push(todoOpen); 
    } 
    else{
     
      let todoDone = (
        <div>
      
          {this.props.todos[index].describtion}
          {this.props.todos[index].status}
          <button
            type= 'button'
            onClick = {() => this.props.handleStatus(this.props.todos[index])}
          ></button>
          {this.props.todos[index].id}
          
        </div>
      );
      todoList[2].push(todoDone); 
    }
  
  });

  return todoList;
}

}

Pat Rick
  • 33
  • 4
  • It looks like you forgot to include an actual question. However, this type of problem has been asked thousands of times on SO, so I would recommend reading through as many existing questions/answers as you can first or this will likely be marked a duplicate. – Brian Thompson Mar 14 '22 at 20:26
  • Does this answer your question? [React: how to update state.item\[1\] in state using setState?](https://stackoverflow.com/questions/29537299/react-how-to-update-state-item1-in-state-using-setstate) – Brian Thompson Mar 14 '22 at 20:29
  • Other's worth looking at https://stackoverflow.com/questions/49477547/setstate-of-an-array-of-objects-in-react, https://stackoverflow.com/questions/62918710/how-to-update-state-with-usestate-in-an-array-of-objects, https://stackoverflow.com/questions/37662708/react-updating-state-when-state-is-an-array-of-objects. – Brian Thompson Mar 14 '22 at 20:29

0 Answers0