The Best Approach To Design React Component Hierarchy
React allows us to break the user interface into logically connected components. Those components when combined make a complete application structure.
But, if you are a beginner in the React world, you will find yourself stuck at how to approach a layout design and break it into react components. Breaking the application into components is the first and the most crucial step in building a react application. A good structure makes your app scalable, maintainable and testable.
In this article, we are going to learn how to —
- Break a layout into react components
- Convert those components into a component hierarchy
What are we going to build?
I know this layout doesn’t look impressive. I have made it simple and after creating this in react, you will learn how to structure your application like a pro! So, hold your excitement and let’s get started.
STEP 1 — Wireframing
Before creating an app, you should create a rough wireframe of it. A wireframe is nothing but a visual representation of the skeletal framework of your app. This will give you a clear view of what your app is going to look like.
STEP 2 — Breaking it into components
Once your wireframe is ready, you can start breaking it into individual components. Now, this can be tricky for beginners. What can be considered as a component?
A component is anything that has its standalone meaning in the layout.
For example, a calculator has a screen and many buttons. The screen is a component as it has its separate use of just displaying the calculations. An individual button is also a component. And finally, the whole calculator can be considered as a root component as it integrates the screen and the button components together.
So, after breaking my layout into components, I come up with the structure below.
- App : The outer (root/parent) component that integrates(renders) other(child) components
- Intro: The landing page section of the webpage, renders heading, subheading and another component Button.
- Button: Displays a button.
- Skills: The skill section of the webpage.
- SkillItem: Individual skill item inside the Skills component.
STEP 3— Creating a component hierarchy
Good work there. After you know your app’s components, let’s define some relationships between them.
We create a tree-like diagram called component hierarchy. This gives us information about the parent-child relationship between components, tells us about which component renders other components and helps us to organize the overall state of the application.
Steps to create a component hierarchy —
- Start from the root component (App). It will be the root node of the tree.
- Find it’s direct children components (Intro and Skills).
- Make a tree and represent App as the root node and Intro and Skills its children.
- Now, repeat the same process with other component nodes. Find direct children of the components from Figure 3.
Congratulations! You learned how to create a component hierarchy.
Here’s, the component hierarchy —
Things we get from the above hierarchy —
- App is the parent component of the application.
- App renders two children components — Intro and Skills
- Intro renders a Button component
- Skills component renders SkillList (I have created this component as a wrapper component to wrap all the skill items. Not confusion right?)
- Finally, SkillList renders three SkillItem components
So, these are the initial steps you should follow to make a react project.
Thank you for reading!