Passing props to this.props.children in React

Passing props to this.props.children in React

In this post we will give you information about Passing props to this.props.children in React. Hear we will give you detail about Passing props to this.props.children in ReactAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to pass the props tothis.props.children in react.

In react this.props.children is used to pass the data(components,elements) between opening and closing jsx tags.

Consider we have the two Components Child and App where we need to pass the logMe method to all its child components.

import React,{Component} from 'react';const Child = (props)=>{    return (        <div>          <h1>Child {props.index}</h1>          <button onClick={()=>props.logMe()}>Log</button>        </div>    )}class App extends Component{    logMe =()=>{        console.log('Hii child')    }    render(){        return (            <div className="App">                 {this.props.children}            </div>        )}}ReactDOM.render(  <App>    <Child/>    <Child/>  </App>,document.getElementById('root'));

In general, we can pass the logMe method to Child components by using props but in our case, we are using this.props.children so that we didn’t have a direct way to pass the data.

React.Children.map and React.cloneElement

React offers us two methods React.Children.map and React.cloneElement by using these methods we can iterate over each children and clone the each react element with our props.

Let’s update our code by using these two methods.

import React, { Component } from "react";import ReactDOM from "react-dom";const Child = props =&gt; {  return (    &lt;div&gt;      &lt;h1&gt;Child {props.index}&lt;/h1&gt;      &lt;button onClick={() =&gt; props.logMe()}&gt;Log&lt;/button&gt;    &lt;/div&gt;  );};class App extends Component {  logMe = () =&gt; {    console.log("Hii child");  };  render() {    const updateChildrenWithProps = React.Children.map(      this.props.children,      (child<span>, i) =&gt; {        return React.cloneElement(child, {        //this properties are available as a props in child components          logMe: this.logMe,          index: i        });      }    );    return &lt;div className="App"&gt;{updateChildrenWithProps}&lt;/div&gt;;  }}ReactDOM.render(  &lt;App&gt;    &lt;Child /&gt;    &lt;Child /&gt;  &lt;/App&gt;,  document.getElementById("root"));

In the above code, we passed logMe method, index property to all child components by using the React.cloneElement.

Output :

Hope this code and post will helped you for implement Passing props to this.props.children in React. 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