Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

What is the proper way to set an initial state?

I am learning React from two different sources. These videos are showing a completely different way from setting an initial state in a component. The other source I am learning from is using a constructor method (much like a child class) to set an initial state.

Example: constructor(props) { super(props); this.state = { mood: 'decent' }; }

I've also noticed that creating a class is different in these videos.

Example: class App extends React.Component.

Which is the proper way to set initial state and creating a class?

2 Answers

Like most frameworks, different versions will have some differences, it all depends on what version you are using to learn, I would suggest you use the official React documentation as reference: https://reactjs.org/docs/hello-world.html the videos and tutorials on the web may be outdated. Cheers.

Seth Kroger
Seth Kroger
56,413 Points

You'll often find in programming there are more than one way to do the same thing. And as a language like JavaScript grows and evolves, people will find more new tricks to do the same thing. Sometimes it might make a slight difference in which way you choose to do it and others, like here, come down to stylistic differences only and there is no "One Right Way" to do it.