10.0.0-alpha.2 - <input onChange> normalization?
See original GitHub issueCalling 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:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This was indeed fixed together with #1324 🎉
Preact does not normalize
onChange
to useonInput
behaviour. If you includepreact/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