Checkboxes state is not persisting
On the latest beta version.
My checkboxes state is not persisting. See here in my sandbox:
https://codesandbox.io/s/3r0jx6kqnq
I believe it has something to do with me setting state within a function called from within the onRowsSelect function.
However, I can't figure out why. The checkbox becomes true and then quickly becomes false, and I'm wondering if it is refreshing the render and returning the component to its original state. Definitely feels like a bug though, but if its an error by me please let me know.
Possibly related is when you check the select all box it throws an error:
Cannot read property '1' of undefined
| Tech | Version |
|---|---|
| Material-UI | Latest |
| MUI-datatables | 2.0.0-beta-21 |
| React | 16.4.0 |
| browser | Chrome OSX 68.0.3440.75 |
Ok thanks for sharing I’ll tru to take a look this weekend
If it's going to be a controlled component then you need to pass in the selected rows as well otherwise it'll appear blank after the re-render.
rowsSelected: this.state.rowsSelected,
onRowsSelect: (rowsSelected, allRows) => {
const selected = allRows.map(item => item.index);
this.setState({
rowsSelected: selected,
});
},
Ok the problem I had was that I want to access a column and apply that as a selected record value in state.
const data = [
['THC123789', '123456/1', '1', true, true, '2018-01-01', 0],
['THC123789', '159351/3', '1', true, false, null, 1],
['BVR9845', '789456/1', '1', true, true, '2018-02-05', 2],
['MBN2291', '693825/1', '1', false, false, '', 0],
];
So I I select a row I want to put in the 2nd value of the array, your code snippet just brings back the row id.
I’m not seeing that in your code example? At minimum I think your first big issue was not passing the rowsSelected. Without that the table will never know which rows should be selected if it is a controlled component
Overall I’m not even sure I’m understanding what you’re trying to accomplish considering this is just something to identify selected rows and the callback returns the correct selected row.
Ok, I've updated my example, sorry about that.
See in my example I have Name and Ref for example. When I check the rows, I want the reference numbers to go into state so I can pass them to other components.
Maybe if I explain the full scenario that I am going for here.
My data in the actual app will be server-side, and the function of the app is to be able to remove a booking date, and my employers want to do this as a bulk, so having the ability to check which ever rows they want to bulk edit and this will enable me to pass the state to a custom component that will update an API, and then return it on confirmation.
See in my example I have Name and Ref for example. When I check the rows, I want the reference numbers to go into state so I can pass them to other components.
I'm still not seeing an issue? In the SS below I click on the first row and the last row the result gives me [0,3].

I also see the proper values in the local state for that component.
Codesandbox: https://codesandbox.io/s/r44z758vy4
Yeah I think we are good. I managed to get the value I was looking at from those rows with a bit of persevering.
I have same issue when using setState (react-hook) inside onRowsSelect checkbox not working or state did not persistent
optionTable have properties serverSide true.