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 trialSunny Chu
4,677 PointsReact & state - Is my mindset correct?
I am trying to wrap my head around the whole React & it's state concept, I have a few ideas in mind after studying the basic react course, I am wondering if this is the right way to think about react?
State is basically like value
It is preferred to pass state from higher level DOM to lower level DOM if the said state is used for the whole application (e.g. like raw data set)
State are passed from parent to child using "props"
If #2 & #3 are set up correctly, the general benefit is that React will automatically detect state change and make DOM change according / automatically, saving tons of development time.
To update state from a child to it's parent, the parent needs to first pass a callback function (that changes its own state) to the child using "props", then the child will be able to use it to change state.
Base on #5, for example : For a 10 layer component structure, to make changes to level 1 parent from level 10 child, I will have to pass the call back function 9 times through every layer of component from level 1 parent to level 10 child in order for the child to use the function.
Base on everything above, the benefit is that:
- My code are now more easy to reuse and debug because everything is within a controlled component class.
- React is smart to help automatically apply state changes to DOM change, saving me time.