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

Updating state with checkboxes #2485

Closed
CharlieCharlieCharlieCharlie opened this issue Feb 16, 2017 · 1 comment
Closed

Updating state with checkboxes #2485

CharlieCharlieCharlieCharlie opened this issue Feb 16, 2017 · 1 comment

Comments

@CharlieCharlieCharlieCharlie
Copy link

CharlieCharlieCharlieCharlie commented Feb 16, 2017

Looking over the docs, still not sure how to tie my input to my local state.

 constructor(props) {
    super(props);
    this.state = {
      checked: []
    }
  }

  checkBoxFormatter (cell, row, field) {
    const isChecked = () => {
      this.state.checked.includes(row.id);
    };
    const toggleCheck = () => {
      if (this.state.checked.includes(row.id)) {
        this.setState({checked: this.state.checked.filter((i)=> i !== row.id)});
      } else {
        this.setState({checked:this.state.checked.concat(row.id)});
      }
    };

    return (<Checkbox defaultChecked={isChecked} onChange={toggleCheck}/>);
  }

onChange is being called fined on click, and state is updating with new values, but the checkbox itself never visually shows as checked. Do I need to use inputRef?

@jquense
Copy link
Member

jquense commented Feb 16, 2017

You need to use checked not defaultChecked, I suggest reading the React docs on controlled and uncontrolled inputs.

@jquense jquense closed this as completed Feb 16, 2017
sersorrel added a commit to wsi-cogs/frontend that referenced this issue Jul 30, 2019
The problem: if the user clicked on a checkbox in the
MultiselectDropDown (rather than clicking on the text adjacent to the
checkbox), the state of the checkbox would appear visually to stay the
same, but everything else -- its props, the parent element's state, etc.
-- correctly believed that the checkbox has been toggled.

To fix this, we force an update of the entire component every time a
checkbox is clicked, which rerenders the checkbox and results in the
visual appearance matching the React/DOM state again. Both the call to
setTimeout and the two arrow functions are required; without the arrow
functions, various React code doesn't get the correct `this`, and
without the setTimeout, it Just Doesn't Work.

It's unclear exactly why this works. There are several issues in the
React repo and elsewhere describing similar behaviour:

facebook/react#2766
facebook/react#3005
facebook/react#3446
react-bootstrap/react-bootstrap#2485

Unfortunately, none of the suggestions made appear to be directly
applicable to this situation.

This workaround is bad for two reasons:
- it's ugly and nonobvious
- it forces a rerender of far too many components

Nevertheless, it fixes the problem.

Closes #1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants