Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CheckBox doesnt check when i use REACT HOOK #2411

Closed
aliburhankeskin opened this issue Sep 3, 2020 · 1 comment
Closed

CheckBox doesnt check when i use REACT HOOK #2411

aliburhankeskin opened this issue Sep 3, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@aliburhankeskin
Copy link

aliburhankeskin commented Sep 3, 2020

I want to update checkBox status in 'onRowClick' event .

1  const handleRowClick = (evt, rowData) => {
2    const rslts = data;
3    const index = rslts.indexOf(rowData);
4    rslts[index].tableData.checked = !rslts[index].tableData.checked;
5    setData(rslts); 
6
7   // Random state update
8    setRandom(!random);
9   };

When i write code above it works but the application locked when i click repeatedly.

Note: Recommended solution for class component is code above except eighth line but i use function component.

Also if i remove eighth line, the following bug is happening;

2020-09-03-2145-55

Thanks for helping...

@aliburhankeskin aliburhankeskin added the bug Something isn't working label Sep 3, 2020
@Lalee10
Copy link

Lalee10 commented Sep 8, 2020

This is not a bug with the table, this issue is with how you are setting state. Mutating the data and not assigning it a new reference before setting the state will result in React's reference check resulting in true and component not re-rendering.

Changing Line 5 to setData([...rslts]) will update the state of your component and re-render the table as well since data has changed

Assuming random is a boolean, since booleans are primitives and cannot be mutated is the reason your second setRandom on line 8 works.

But for arrays and objects in state always make sure to change the reference to the state before setting it. Your code will also work if you change line 2 as const rslts = [...data];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants