React Props Tutorial: Children Example

React Props Tutorial: Children Example

In this post we will give you information about React Props Tutorial: Children Example. Hear we will give you detail about React Props Tutorial: Children ExampleAnd how to use it also give you demo for it if it is necessary.

React is a popular user interface library for building UIs with components and render them using a virtual DOM.

Basically, your build your React application as a tree of components with a parent root component (Usually called App) and child components.

What’s React Props

More often that not, components need to communicate between each other by passing data from a component to another or from a parent component down the tree.

You can pass data down to a child component via its props which is short for properties.

Example of Using the React Props Object

Each component owns a props object that holds any properties that are passed from a parent component.

Let’s take a simple example.

In this example we assume that you already have a React project ready.

Creating a Child React Component: The props children property

Let’s start by creating a child React component that will be used in the parent App component.

In the src/ folder, create a Header.js file and the following code:

importReactfrom'react';classHeaderextendsReact.Component{render(){return(<headerrole="banner">{this.props.children}</header>);}}exportdefaultHeader;

We use the children property of this.props to display the content that exists between the opening and closing tag when invoking a component. In this case, our Header component.

According to the React docs, you can use this.props.children in components that can be used as generic boxes which don’t know their children ahead of time.

Next, open the src/App.js file, import the Header component and update the render() method as follows:

importReactfrom'react';importHeaderfrom'Header.js';classAppextendsReact.component{render(){return(<Header><h1>Thisistheheader</h1></Header>);}}exportdefaultApp;

Creating a Second React Child Component: Passing Data via Props

Next, let’s create a second React component called Main that will be also invoked from the App component.

Create a Main.js file and add the following code:

importReactfrom'react';classMainextendsReact.Component{render(){return(<div><h2>{this.props.title}</h2><p>{this.props.bodyText}</p></div>);}}exportdefaultMain;

The Main component expects two props – title and bodyText which need to be passed from the parent component.

Next, let’s invoke the Main component from the App component

Open the src/App.js file and update it accordingly:

importReactfrom'react';importHeaderfrom'Header.js';importMainfrom'Main.js';classAppextendsReact.component{render(){return(<Header><h1>Thisistheheader</h1></Header><Maintitle='This is a the main section'bodyText='Hello World!'></Main>);}}exportdefaultApp;

We import and invoke the Main component and we pass values to the title and bodyText props.

As we’ve seen we can access the passed data from the child component using the props object.

Props and the Functional React Components

In a functional component, you can access the props object as follows:

constHeader=(props)=>{return(<headerrole="banner">{props.children}</header>);}

The props object is passed as a parameter to the component so you don’t need to use this.

Wrap-up of React Props

In this tutorial we’ve learned about React props.

We have created two child components of the App component – Header and Main.

The Header component renders whetever exists between the opening and closing tag by using the children property of the props object.

The Main component displays a title and a text as body using the title and bodyText props.


Hope this code and post will helped you for implement React Props Tutorial: Children Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US