question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Checkbox onChange is called when component is rendered first time with initialValue

See original GitHub issue

Describe 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:

  1. Add Galio Checkbox component in you screen
  2. 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
  3. 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:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
palingheorghecommented, Sep 30, 2019

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.

1reaction
matthew-bigginscommented, May 14, 2020

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} />

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found