![]() |
Passing props from parent to child in React JS is an important concept. Props are read-only properties that are sent from the parent components to the child components. We can pass props in from parent to child using React JS functional as well as class components Table of ContentSteps to create the application: Step 1: The below command will help you to start a new React app. npx create-react-app testapp
Step 2: Next, you have to move to the ‘testapp‘ project folder from the terminal. cd testapp
Step 3: Create a new components folder inside the src folder and create one component named child.js inside the component folder. Project Structure: Approach 1: Using Class ComponentsWhen users pass the data from parent to child using the class components, they can access inside the child component using ‘this.props.property_name‘. Steps:
Example: Passing props from parent to child component in react usign class components App.js and child.js Javascript
Javascript
npm start Output: This output will be visible on http://localhost:3000/ in the Web browser. Approach 2: Using Functional ComponentsTo access properties from parent to child using a functional component, users don’t need to use ‘this.props‘ like class components. Users can access props value by writing variable names only. Steps:
Example: Javascript
Javascript
Step to run the application: Step 1: Open the terminal and type the following command. npm start
Output: This output will be visible on http://localhost:3000/ in the Web browser. |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |