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.

bug: changing value of component in ionChange results in an infinite loop

See original GitHub issue

Bug Report

Ionic version: [x] 4.7.11

Current behavior: When the value of an Ionic input element is changed from OUTSIDE of the element, the element’s onIonChange is triggered, thereby duplicating the change event.

Expected behavior: As with every other React input, the onIonChange should only be triggered when the value was change from the element itself.

Steps to reproduce: See console in included stackblitz.

  1. Both the IonInput and regular input are controlled via the same value.
  2. When changing it from the IonInput, only the onIonChange is triggered.
  3. When changing it from the regular input, the input’s onChange is called AS WELL AS the onIonChange from the IonInput.

Related code: https://stackblitz.com/edit/ionic-v4-11-7-controlled-inputs

Other information: This same bug is happening with other Ionic inputs as well. I haven’t tested all of them, but it is happening with at least IonToggle and IonDateTime.

This is bad because unless each input has its own separate controlled state, doing something like clearing a state object could trigger 10+ onIonChange events, leading to race conditions and other annoyances.

Ionic info:

Ionic:

   Ionic CLI       : 5.4.13 (/Users/evan/.config/yarn/global/node_modules/ionic)
   Ionic Framework : @ionic/react 4.11.7

Capacitor:

   Capacitor CLI   : 1.4.0
   @capacitor/core : 1.4.0

Utility:

   cordova-res : not installed
   native-run  : not installed

System:

   NodeJS : v13.1.0 (/usr/local/Cellar/node/13.1.0/bin/node)
   npm    : 6.12.1
   OS     : macOS Catalina

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:22
  • Comments:28 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
ebk46commented, Dec 20, 2019

And as a followup, it seems that other inputs don’t have onIonInput or anything like it that only triggers for inputs via that element. For example, IonDateTime and IonToggle only have onIonChange. What are we supposed to do in those cases?

8reactions
masimplocommented, Dec 19, 2019

ionChange is triggered with every change of the input value, either through user input or programmatically. If you want your handler to be triggered only when the user changes the input, you should use ionInput event instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React - Infinite loop when changing state from onChange
When the onChange method is called, the error "Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside ...
Read more >
OnChange event on controls not consistent when default ...
It seems that some controls don't fire the OnChange action when the default value property is set/bound to an output from a component:....
Read more >
Fix the "Maximum Update Depth Exceeded" Error in React
One of which is when you accidentally cause an infinite render loop, often resulting in the cryptic “maximum update depth exceeded” error.
Read more >
8 common React error messages and how to address them
Can't perform a React state update on an unmounted component; Too many re-renders. React limits the number of renders to prevent an infinite...
Read more >
How to Solve the Infinite Loop of React.useEffect()
useEffect() hook because it can generate infinite loops. ... value={value} onChange={onChange} /> is a controlled component. value state ...
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