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: input won't trigger onChange after a chain of setStates

See original GitHub issue

React-dom version: 16.9.0 to 16.12.0 (latest)

This bug was not present in 16.8.6

Steps To Reproduce

  1. Create a controlled input element
  2. Add an onKeyDown event handler that will change a state.
  3. Add a useEffect hook that depends on the state from (2.), and changes another state
  4. Try to type in something in the input

Link to code example: https://codesandbox.io/s/nostalgic-lake-zzynt

The current behavior

The input updates with the key the user has pressed

The expected behavior

The input doesn’t update. The handler for onKeyDown was called, but the handler for onChange wasn’t.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
gaearoncommented, Jun 23, 2021

This is fixed in 18 alpha. https://codesandbox.io/s/tender-glade-80ks5?file=/src/App.js

Unfortunately we won’t backport the fix to 17 because it was a complex refactor which relies on other things we’re putting in 18.

0reactions
gaearoncommented, Aug 26, 2020

We haven’t touched that part but there are some post-17 changes planned that should simplify this. We’ll revisit at that point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React - setting input value with JavaScript does not trigger ' ...
Basically, the text input is a string (from an array) of user ID's - "2,46,3", etc. addMember is triggered when the app user...
Read more >
react input onchange doesn't work
React app, after getting data from initial get request, onChange doesnt work. React JS : grandparent component's setState method doesn't update state of...
Read more >
How to Use the setState Callback in React
To perform an action in a React component after calling setState, such as making an AJAX request or throwing an error, we use...
Read more >
React setState does not immediately update the state
Reading state right after calling setState() a potential pitfall. useState React hook. Returns a stateful value, and a function to update it.
Read more >
Simulate React On-Change On Controlled Components
For example, in many situations, we want to auto-update inputs. We do that either by using ... But triggering onChange without using setState...
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