I'm trying to have the button disabled whenever the input is equals to 0. In the beginning, the button is disabled but when I try to increase the input > 0 and decrease the input back to 0, the button won't be disabled.
constructor() {
super();
this.state = {
number: 0,
};
}
onInputChange = (e) => {
this.setState({ number: e.target.value });
};
render() {
const { number} = this.state;
return (
<Fragment>
<input
value={number}
type="number"
max="100"
min="0"
onChange={this.onInputChange}
></input>
<Button
style={{ display: "block" }}
disabled={number === 0 ? true : false}
>
Print
</Button>
Could someone explain to my why the state is changed but the disable property won't work? If I set the condition to:
<Button
style={{ display: "block" }}
disabled={number < 1 ? true : false}
>
then the code works fine. But I really want to know why my first approach doesn't work. Thank you.