Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to use if in setstate

// 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);
Comment

PREVIOUS NEXT
Code Example
Javascript :: lodash remove element from list 
Javascript :: change the mouse pointer javascript 
Javascript :: alphabet string javascript 
Javascript :: axios set body 
Javascript :: javascript check if string contains substring 
Javascript :: convert cookies to json javascript 
Javascript :: access variable outside for loop javascript 
Javascript :: mongodb add new field 
Javascript :: discord.js button 
Javascript :: jquery check checkbox 
Javascript :: url regex javascript 
Javascript :: js conditional object 
Javascript :: react-native run-ios command 
Javascript :: delete a property of html by js 
Javascript :: js setinterval 
Javascript :: chartjs line color 
Javascript :: sort object dictionary javscript 
Javascript :: javascript pick random attribute from object 
Javascript :: javascript get parent element height javascript 
Javascript :: save form data jquery 
Javascript :: javascript sum array values 
Javascript :: window bind load jquery 
Javascript :: javascript remove duplicates from array 
Javascript :: how to disable keyboard calender input in material ui datepicker reactjs 
Javascript :: js array two dimensional 
Javascript :: js order string 
Javascript :: date time js 
Javascript :: how to get unique values from array in javascript without duplicate value 
Javascript :: laravel return validation errors ajax 
Javascript :: react native button top right 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =