-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>
</>
))}
Vlw bro, it worked!
– Paulo Robson
If useful @Paulorobson vote to answer your question
– novic
Do you have any reason to use spread twice in the function
handleRemoveItem
?– Luiz Felipe
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 ... !!!– novic
@Luizfelipe in the state I always do so.!
– novic
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 insetTodos([...newTodos])
), since internally the whole array is swept during the spread.– Luiz Felipe
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.– novic
@Luizfelipe what would change in practice? I think nothing hyena!
– novic
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? :)
– Luiz Felipe
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.
– novic
I know that. But in that case no need to do it twice.
– Luiz Felipe
Have some more score to make @Luizfelipe
– novic