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 React Basics (retired) Stateful Components Understanding State

getInitialState vs defaultProps

Why use getIntialState when you can also set default values to defaultProps ? I am confused.

1 Answer

Lucas Santos
Lucas Santos
19,315 Points

I think your confusion lies in understanding the difference between States and Props.

Props are just the properties of the component that do not change and are immutable. Something like a title for the app or a certain value for a variable.

States are the data points in your application that do change. Like scores, lists, timers and so on.

So setting getIntialState gives the component a default State and defaultProps gives the component a default Prop. The reason anyone would use state over a props would be because they want the data point to change at some point in time in the app.

That or they decide to not use a "Stateless Functional Component". (a component that has no state only props)

Also here is a great topic in StackOverflow that explains this a lot more detailed than I did.

Thanks for explaining. This makes sense.