Skip to content
Advertisement

How to add a class name in every row without effect the rest of the rows?

How to add a class name in every row without effect the rest of the rows

JavaScript

Advertisement

Answer

The problem here is that when you say the state is false; it is assuming the state is false for the whole component. It doesn’t update the row but the whole component. So, at first, you need to add a deleted property that will take a different value for each row.

So,

JavaScript

Now, when you render, you don’t need to use that arrayTest. But you need to use the state. We won’t touch arrayTest ever again. So we use,

JavaScript

Notice we use state.map. We also send x.id to handleClick function. Why? Because we will use that id to change the deleted value of the object. So our handleClick becomes,

JavaScript

This is just updating the state in an immutable way.

Here is the full codesandbox for your convenience.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement