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 behavior inexplicable when preventDefault() is used #3446
Comments
this is because under the hood react listens for Click events instead of Change to normalize browser differences. The accepted solution is: don't use preventDefault (use stopPropagation instead) or make use of a timeout. more reading here #3005 |
@jquense Can you maybe detail exactly what is going on? The effect is still observed when you use onClick directly. Can you maybe give me a flow of how this is happening? |
@tigergrid you can read the issue I linked, zpao covers it quite nicely. #3005 (comment) essentially you are running into a limitation of how react implements the |
This is unfortunate but the way it is for now. If you don't have a good reason for using preventDefault(), don't. |
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.
Man, 8 years later and I'm still seeing the same issue. Messed with my head for a solid hour or two, seems like |
Notice how clicking the checkbox does not make React render it according to the checked state, but clicking the other checkbox causes the checkbox to re-render correctly.
In theory the preventDefault() happened when the component was dispatching and shouldn't affect the rendering? In any case the component shouldn't be rendered incorrectly?
The text was updated successfully, but these errors were encountered: