You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a peculiar problem with using a html a-tag with input type=checkbox from React. The thing is that if I click directly on the checkbox and not the a-tag the checkbox will not re-render correctly but my state has updated.
Example: Given the code below I render the component and click directly on the checkbox. In this case showMap will be set to false since we set it to true in the constructor but the checkbox will still be checked in the html view. If I however click on the a-tag but not directly on the checkbox both the state showMap is updated correctly as well as the view.
I can make it work by not calling event.preventDefault(); in toggleCheckbox but if I do that the app will scroll to the top of the page if I click on the a-tag.
I'm not sure there's anything React can do here. Additionally, I'm curious about using an anchor tag in this way. What if you used a label?
<labelhtmlFor="showMap"><inputtype="checkbox" id="showMap" name="showMap" onChange={this.handleChange} /> Show map
</label>
This prevents the need to prevent the default action for the anchor tag. Using a label ensures that clicking "show map" will activate the checkbox. It also helps out screen readers.
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
I have a peculiar problem with using a html
a
-tag withinput type=checkbox
from React. The thing is that if I click directly on the checkbox and not thea
-tag the checkbox will not re-render correctly but my state has updated.Example: Given the code below I render the component and click directly on the checkbox. In this case showMap will be set to false since we set it to true in the constructor but the checkbox will still be checked in the html view. If I however click on the
a
-tag but not directly on the checkbox both the state showMap is updated correctly as well as the view.I can make it work by not calling
event.preventDefault();
intoggleCheckbox
but if I do that the app will scroll to the top of the page if I click on thea
-tag.Example:
https://codesandbox.io/s/w2x8vqq8xl
Code:
What is the expected behavior?
When I press the checkbox it should update visually.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16, Chrome, Windows.
The text was updated successfully, but these errors were encountered: