How to embed two components in one component ?
In the ReactJS Components, it is easy to build a complex UI of any application. We can divide the UI of the application into small components and render every component individually on the web page.
React allows us to render one component inside another component. It means, we can create the parent-child relationship between the 2 or more components.
Prerequisites: The pre-requisites for this project are:
Creating Application: The below command will help you to start a new react app.
npx create-react-app testapp
Next, you have to move to the testapp project folder from the terminal.
Create a new components folder inside the src folder and create two components named child1.jsx and child2.jsx files inside it.
Project directory: It should look like this.
Example: Write down the following codes in respective files.
Now, we will change the default code of the App.js component file. Also, we will embed the child1 and child2 components inside the App component.
Steps to Run: To start the react app, run the below command on your terminal and verify that react app is working fine.
Output: You will see react app is started on localhost:3000 without any difficulty. Here, the App component is a parent component of the child1 and child2 components.