Remove All from a list

Asked

Viewed 71 times

-1

My function of removing All is only removing the first Element that has been added to all.

const handleRemoveItem = useCallback(() => {
    setTodos(todos.slice(todos.indexOf(+1)));
}, [todos]);


{todos.map((todo, index) => (
<>
    <Icon src={IconClose} onClick={handleRemoveItem} />
    <Card key={index}>
    <p>{todo.regiao}</p>
    <div>
        <p>Código do Representante:</p>
        <span>{todo.codRepresentante}</span>
    </div>
    </Card>
</>
))}

1 answer

1


Missed what object crave rule out of the list and the correct method is splice that removes the item from the list, and Slice returns you parts of a array and it’s not what you need. There are other ways to do this, but I will follow your current example:

function App() {
  const [todos, setTodos] = React.useState([
    {codRepresentante: 1, regiao: 'Região 1'},
    {codRepresentante: 2, regiao: 'Região 2'},
    {codRepresentante: 3, regiao: 'Região 3'},
  ]);
  const handleRemoveItem = React.useCallback((todo) => {
    let newTodos = [...todos];
    newTodos.splice(todos.indexOf(todo), 1)
    setTodos(newTodos);
  }, [todos]);
  return (
    <div>
      {todos && todos.map(todo => 
        (
          <div>
            {todo.codRepresentante} - {todo.regiao} { ' ' }
            <button onClick={e => handleRemoveItem(todo)}>Remover</button>
          </div>
        )
      )}
    </div>
  )
}
ReactDOM.render(<App />, document.querySelector('#root'))
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<div id="root"></div>

  • Vlw bro, it worked!

  • If useful @Paulorobson vote to answer your question

  • Do you have any reason to use spread twice in the function handleRemoveItem?

  • is recommended @Luizfelipe has seen videos saying that for some reason he passes the reference and gives problem, so it is better to always pass the copy, once it happened to me also in a real case, I could not understand the reason, at the time I did the spread worked out ... !!!

  • @Luizfelipe in the state I always do so.!

  • Yes, but in let newTodos = [...todos] you’ve already created a new reference, so I don’t think you need to do it twice (the second in setTodos([...newTodos])), since internally the whole array is swept during the spread.

  • Well I made the change @Luizfelipe, but, I remember I’ve had trouble, if I’m not mistaken is in the part to pick up copies of several arrays, I don’t really remember, if I put.

  • @Luizfelipe what would change in practice? I think nothing hyena!

  • 1

    In practice nothing changes, the "visual" and "functional" effect is the same. The potential problem is in relation to performance, depending on the amount of elements, since it is a complete "scan" in the extra unnecessary array (excluding non-standard optimizations). Of course, in practice nothing changes, but if it is possible to avoid an extra iteration, why not, right? :)

  • If you use state pass mode as a function catching the old by the new is required to do this @Luizfelipe then I guess it depends I ended up checking on an example.

  • I know that. But in that case no need to do it twice.

  • Have some more score to make @Luizfelipe

Show 7 more comments

Browser other questions tagged

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