// How to use if-else in react js / setState of react js
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
title: "",
time: "",
enabled: false
};
}
onChange(e) {
this.setState({[e.target.id]: e.target.value},
() => {
if (this.state.title && this.state.time) {
this.setState({ enabled: true });
} else {
this.setState({ enabled: false });
}
}
);
}
render() {
return (
<React.Fragment>
<input
type="text"
id="time"
name="time"
onChange={this.onChange.bind(this)}
value={this.state.time}
/>
<input
type="text"
id="title"
name="title"
onChange={this.onChange.bind(this)}
value={this.state.title}
/>
<button disabled={!this.state.enabled}>Button</button>
</React.Fragment>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);