The course is part of this learning path
This module looks at how to identify state in a React application.
The objectives of this module are to provide you with an understanding of:
- How to identify state
- Where state lives
This Learning Path is aimed at all who wish to learn how to use the ReactJS framework.
Prerequisites of the Course
We welcome all feedback and suggestions - please contact us at email@example.com to let us know what you think.
The first part of thinking in React is identifying where the state should live. Once you've identified data, it should be stated in the application. We need to work out which components rely on the state, which can potentially mutate it and which should own it. This might not be blatantly obvious when we look at our component hierarchy. Remember, React data flow is unidirectional and data flows from the top of a component tree to the bottom. There's a three-step process to identifying which component should own state.
Firstly, identify every component that render something based on state. That means the component receives all, or part of the state as props. Then, find a component higher up the tree than all the components that use the state, and put state in this component. This could be an immediate owner component or a component even higher up the tree.
Lastly, if it doesn't make sense for state to live in a common owner component or one doesn't exist, create a new component to hold state, and add it to the hierarchy and appropriate place. It may be the two or more separate trees converge at this new component. And it may only serve to provide a sensible place for state to live. There are other alternatives where state lives in an application, but therefor late in your React journey.
So, continuing to follow our example, the Filterable Courses Table will receive the set of original courses as static data. That won't change, and it passes these down to the Course Table as props. This of course is not state but it's worth noting where the data comes from. Course Table needs to calculate the courses to pass this props to the Course Category Row and the Course Row based on the state values of search text and the chat box. Again, the course is passed down and not state but they are calculated using two items from state.
The Search Bar needs to display the search text and the checkbox value, as well as potentially mutate this data. The common owner component of course, table and search bar is Filterable Courses Table. And it conceptually makes sense for search text and the chatbox value to live in the Filterable Courses Table. We'll deal with how the search text and the chatbox value can mutate in the fifth and final part of the thinking in React process, but before that, we need to know how to add state to a component. So make sure you check out our content on that.
Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.
Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.