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: [Ionic-React]. <IonInput/> Controlled component doesn't work.

See original GitHub issue

Bug Report

Ionic version:

[x] 4.4.2

Ionic-React version

[x] 0.0.5

Current behavior:

<IonInput />'s onChange is not fired.

<IonInput />'s value is set to state from onInput, but the state is not set to value={}. The <IonInput />-component’s state is not really being controlled.

Expected behavior:

Input’s value is same as state.

  1. Input is handled by onChange method
  2. Input length is checked.
  • If length <= 5 then update state
  • else don’t
  1. <IonInput />'s value is same as state

Steps to reproduce:

I created two state variables, value1 and value2, and setters using the useState()-hook. I created a controlled <IonInput /> and a controlled <input />. The regular <input /> is controlled as expected. No more than 5 characters can be placed there. The <IonInput />'s input is correctly set to state and restricted to 5 characters, however the <IonInput /> continues to take in characters.

I ran in to the issue by following the guide from: https://ionicframework.com/blog/using-react-hooks-in-an-ionic-react-app/

Related code:

https://ionicframework.com/blog/using-react-hooks-in-an-ionic-react-app/

image

image

Other information:

Ionic info:

➜  ionic-react-test git:(valtsuDev) ✗ ionic info

Ionic:

   ionic (Ionic CLI) : 4.12.0 (/home/[redacted]/.nvm/versions/node/v12.2.0/lib/node_modules/ionic)

Capacitor:

   capacitor (Capacitor CLI) : 1.0.0-beta.24
   @capacitor/core           : 1.0.0

System:

   NodeJS : v12.2.0 (/home/[redacted]/.nvm/versions/node/v12.2.0/bin/node)
   npm    : 6.9.0
   OS     : Linux 4.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
calendeecommented, Sep 8, 2019

After looking at the docs ☺️, maybe we shouldn’t be using these React events?

Recognized Events : ionBlur, ionChange, ionFocus, ionInput.

So you can do this:

<IonInput
  id="firstName"
  name="firstName"
  onIonChange={setFirstName}
  onSubmit={() => {
    login(null);
  }}
  placeholder="Enter your first name"
  value={firstName}
></IonInput>
0reactions
ionitron-bot[bot]commented, Feb 22, 2020

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug: [Ionic-React]. <IonInput/> Controlled component doesn't ...
The <IonInput /> -component's state is not really being controlled. Expected behavior: Input's value is same as state. Input is handled by ...
Read more >
IonInput not allowing to conditionally prevent the onChange ...
I have a similar control: IonInputMagic1 which is very similar, but it uses the HTML built-in element: <input /> instead of the Ionic...
Read more >
Forms And Validation In Ionic React - Smashing Magazine
It provides support for controlled or uncontrolled components and input validation, and the API is hooks-based so it only works with functional ...
Read more >
Setting Focus to an Input in Ionic
SOLVED : · 1) import “Input”, “ViewChild” and “NavController”. import {Component, Input, ViewChild} from '@angular/core'; import {NavController} ...
Read more >
Using React Hook Form with Ionic React Components
I created a simple error handler function to display the error message from the react-hook-form hook. The library creates an object as part...
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