Tuesday, February 21, 2017

In ReactJS, here are some quick fixes for common problems

To prevent common errors,
in your constructur , you should init the state variable like this:


  constructor(){

      super();

      this.state={};

   

  }


This will prevent some "undefinded variable" errors in case you provide the state variables as props of the child components. 

The initial state should be set in call to componentDidMount. Something like this would do:

componentDidMount() {
    
       this.setState(this.getInitialState());

}


getInitialState(){
     return {
          currentOrderMode:"new"

      }

}

This way, you will be able to propagate state variables to child components without errors:

......

<


                <OrderEditor mode={this.state.currentOrderMode} />


.....

Hope this prevents some of setState() cannot be called before component mounted errors, as well as state variables undefined errors. :)

No comments:

Post a Comment

Ubuntu 12.04, 14.04, 16.04 - auto start an app or script before login

To run a command or application at startup, even before the user has logged in, you can use this file: /etc/rc.local The commands entered...