Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state in constructor or not #1

Open
yuminatwca opened this issue Aug 9, 2019 · 1 comment
Open

state in constructor or not #1

yuminatwca opened this issue Aug 9, 2019 · 1 comment

Comments

@yuminatwca
Copy link

yuminatwca commented Aug 9, 2019

Hi, Ninja,

I have watched the 2 series of your React toturials, and I have noticed that you like to use state directly in the class, instead of to put it in the constructor like the React documentation does.

class Foo extends Component{
   state: { 
        name: "Shaun"
   }
}


class Bar extends Component {
    constructor(props){
      super(props);
      this.state= { 
        name: "Shaun"
      }
    }

}

So, would you please explain it a little bit about why you prefer to use state like the above class Foo ? Thanks.

@cyril-nonato
Copy link

The above method is more of a shorthand method / modern way of using the state without the using the constructor. You can read this if you want https://www.robinwieruch.de/react-state-without-constructor/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants