The following code example works well with text input type, but allows inputs like 012 or 0012 to be entered when using the number input type.
The console.log line always runs and shows the right value, and the App state is also correct when checked with React Developer Tools. It's only that the controlled input is not being "controlled" somehow.
Why is this happening, and what is the recommended way to use number inputs with React?
class App extends Component {
constructor() {
super()
this.state = {
value: '',
}
}
handleChange = e => {
const value = e.target.value
const num = parseInt(value, 10)
console.log(num)
this.setState({ value: isNaN(num) ? '' : num })
}
render() {
return (
<div className="App">
<input type="number" value={this.state.value} onChange={this.handleChange} />
</div>
)
}
}
0012is a number.