"setState" problem using React

Asked

Viewed 165 times

0

I have a code in React that uses the hook useState in that way:

const [tarefas, setTarefas] = useState([]);

It is a task list code, every time I remove a project I have to update the project of each task, I was trying to do so:

setTarefas(tarefas.map(tarefa => {
    if (tarefa.projeto > id) {
      tarefa.projeto--;
    }
    
    return tarefa;
}

Whereas the id is the id of projeto that is being deleted. The problem is, it does not update the property projeto of tarefa, continues as if that part of the code did not exist. Does anyone have any idea what it might be?

Just before I try to do that, I do the filter in the array by means of that line:

setTarefas(tarefas.filter(tarefa => tarefa.projeto !== id));

What’s really happening is that it doesn’t seem to filter, the tasks don’t fade. It filters only if I take out the method that changes the id.

  • what you want to do I was in doubt?

  • you please put the layout of each task item!

  • i want in case change the id of the task object if it is larger than the id I passed

  • project: current project, name: task.name, date: task.data, priority: task.priority, done: task.done

2 answers

1

From the comments, I understood that your doubt is to remove an item from a list, if yes, the example below can facilitate your understanding, example:

function App() {
  const [tarefas, setTarefas] = React.useState([
    {'id': 1, 'desc': 'Tarefa 1'},
    {'id': 2, 'desc': 'Tarefa 2'},
    {'id': 3, 'desc': 'Tarefa 3'},
    {'id': 4, 'desc': 'Tarefa 4'},
    {'id': 5, 'desc': 'Tarefa 5'},
  ])
  const styleListType = {
    listStyleType: "none",
    marginTop: 10
  };
  const onRemove = (id) => {
    setTarefas(tarefas.filter(item => item.id !== id));
  }
  return (
    <div>
      <ul>
      {tarefas && tarefas.map((tarefa, index) => (
        <li style={styleListType}>
          <button onClick={() => onRemove(tarefa.id)}>Excluir</button> 
          {' ' + tarefa.desc}
        </li>)
      )}
      </ul>
    </div>
  );
}
ReactDOM.render( <App/> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

and the method onRemove removes the item from the list with a particular filter.

A few more examples and related issues:

1

The problem is that you need to return a new object with the property changed. In React you should not directly manipulate the state you should calculate a new state based on the current one. Try this way.

setTarefas(tarefas.map(tarefa => {
    let novaTarefa = {...tarefa}
    if (novaTarefa.projeto > id) {
      novaTarefa.projeto--;
    }
    
    return novaTarefa;
})
  • I forgot to mention that just before running this line, I make a filter() in my task array and then make this change. What happens is that it is not filtering, not removing the tasks

  • But of course it will not remove just decrease the variable.

  • but I have my array "tasks" filtered according to the id that arrives as function parameter, the project id in the case. If the task has the "project" property equal to id, it is removed from the array, otherwise it is.

  • setTarefas(tasks.filter(task => task.project !== id)); <=== like this, and soon after I do what I asked the question, I change the project to -1 if it is larger than id. Only when I see it, the task is not removed

  • Can you send the code by Pastebin or repl.it to understand better what you’re doing?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.