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.

10.0.0-alpha.2 - <input onChange> normalization?

See original GitHub issue

Calling setState in a onChange callback of <input type="text"> doesn’t update that input as in React’s controlled component pattern.

I use this to give feedback to the user when an invalid value is entered by resetting it to the last valid value.

Example: Typing something into the form and pressing Enter correctly updates the state. Entering XYZ and pressing Enter doesn’t update the state but also doesn’t reset the input element.

import { Component, render } from "preact";

class App extends Component {
  state = { text: "X" };

  render() {
    return (
      <div>
        {this.state.text}
        <br />
        <input
          type="text"
          value={this.state.text}
          onChange={e => {
            if (e.target.value !== "XYZ") {
              this.setState({
                text: e.target.value
              });
            } else {
              this.setState({
                text: this.state.text
              });
            }
          }}
        />
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

Works with "preact": "^8.0.0": Codesandbox: https://codesandbox.io/s/kmn5400y2v

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mischniccommented, Mar 23, 2019

This was indeed fixed together with #1324 🎉

1reaction
developitcommented, Mar 15, 2019

Preact does not normalize onChange to use onInput behaviour. If you include preact/compat in your project it will do so, but the default is to treat events as they exist in the DOM. The reason it worked in some Preact 8 apps was because compat patched it for all VNodes rather than only patching React.createElement.

In general, it’s recommended to use onInput for this reason: https://codesandbox.io/s/ll0n7j27nz

Read more comments on GitHub >

github_iconTop Results From Across the Web

NPM Install Error:Unexpected end of JSON input while ...
This solved it for me: Open Windows Powershell as admin npm cache clean --force npm install -g @angular/cli.
Read more >
material-ui/core/CHANGELOG.md - UNPKG
The change makes it easier for developers to upgrade React independently from Material-UI. The best support for React 17 will be found in...
Read more >
components/CHANGELOG.md at main · angular ... - GitHub
Component infrastructure and Material Design components for Angular - components/CHANGELOG.md at main · angular/components.
Read more >
Recipe - Releases
BREAKING CHANGE: removed deprecated EzLabelledItem from Recipe and docs ... fix: missing textValue warning from Multiple choice inputs [sha] ...
Read more >
Changelog - Cypress Documentation
Fixed a regression in 12.0.0 where .contains() received multiple elements as a ... This change aligns Cypress' cookie rules with the browser cookie...
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