react-bootstrap-table2: [Bug] Can't click radio button if using setState inside handleOnSelect
Hi, I have this options
const selectRow = {
mode: "radio",
clickToSelect: true,
onSelect: this.handleOnSelect
};
handleOnSelect = (row) => {
//if using setState the radio button options will not show up, if remove setState everything ok
this.setState({
selectedId: row.userId
});
};
Below is error

About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 35 (12 by maintainers)
Commits related to this issue
- fix #297 — committed to react-bootstrap-table/react-bootstrap-table2 by AllenFang 6 years ago
For those looking for a solution to the radio/checkbox toggle disabling when calling setState:
What works for me is setting the
selectedproperty in theselectRowobject you pass to the table.selected: [this.state.selectedUserId],as mentioned by @AllenFang (16 April comment above)I understand the legacy
react-bootstrap-tableallow you to callsetStatewithout manage/control the selection byselectRow.selected. So it can be improved inreact-bootstrap-table2, but with much lower priority@AllenFang Same problem occurs with multiple selection. Here is the codesandbox https://codesandbox.io/s/93jj53kkpp
remove the setState from the App part and try:
@AllenFang I am also having the same problem but I am using remote and redux. My redux state does not change but when an onClick event happens but it causes the table to re-render for some reason. This causes the selected row radio to not show and also makes my current page on the pagination shows 1 even though I am on page 4 for example.
It seems your intention for the
onSelectwas for the user of the component to manage the state of the selections.However, it seems to be the common use case is not to manage the selection - but to simply be notified and take some other action on the selection.
Perhaps you could simply add a property of
customSelectStateManagementor something that when set would behave as it does now - assuming user will manage state. If it isfalse(the default) the grid manages the state but calls the event handlers as simply call backs.Just my thoughts
I thought this was still an issue, but checking the storybook above, it does work if you send the onSelect function to a method somewhere inside your component, and do it there. Make sure the keyField on your table matches the things you are saving in your array of selected items - I made that mistake and as soon as I fixed it, behold it worked!
@ngohungphuc I my opinion,
setStatewill trigger the component render again, so in your case, it will make thereact-bootstrap-tablebroken for selection row. If you really need to callsetState, I will recommand you to also manage the selection by yourself:How do you think?