Checkbox onChange is called when component is rendered first time with initialValue
See original GitHub issueDescribe the bug
Checkbox component’s onChange
handler is called when component is rendered for the first time with initialValue
In the Galio version before 0.6 it was not the case
To Reproduce Steps to reproduce the behavior:
- Add Galio Checkbox component in you screen
- E.g.
<Checkbox onChange={() => {alert('OnChangeCalled') this.setState({ termsAccepted: !this.state.termsAccepted })}} initialValue={this.state.termsAccepted} label="I agree with the Terms" />
, termsAccepted is boolean property - You can see alert message
OnChangeCalled
even when component is loaded for the first time
Expected behavior When initialValue is set first time onChange handler should not be called
Smartphone (please complete the following information):
- Samsung Galasy S8
- Type of device: simulator
- OS: Android 8
- React-Native version: 0.6
- Expo version: 34
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
How do I handle checkbox's onChange in React?
This components is getting a number (as props), how much checkboxes should it render. It looks like this. Why does handleCheckbox method returns ......
Read more >How To Use React Checkbox onChange Feature (with Code ...
Checkboxes are an example of an element that would traditionally be included as part of a form. In this tutorial, we'll learn how...
Read more >How to Control a Checkbox with React Hooks - Medium
An input form element whose value is controlled by React is called a controlled component. The controlled input has both the value and...
Read more ><input> - React Docs
The built-in browser <input> component lets you render different kinds of form ... Specifies the initial value for type="checkbox" and type="radio" inputs.
Read more >lightning-input - documentation - Salesforce Developers
You can define an action for input events like blur , focus , and change . For example, to handle a change event...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the best way of actually solving the problem would be by modifying the component back to a class based component. That way we can have more control over the lifecycle functions.
Having this problem as well. I see the PR fix. Looking forward to that. I’m just adding my workaround for anyone else experiencing this until the fix is implemented. This allows me to use the Checkbox without it calling onChange when first rendered.
const [testing, setTesting] = useState(0)
function someFunction() { if (testing != 0) { ... } setTesting(1); }
In function render:
<Checkbox initialValue={props.value} onChange={someFunction} />