React Pass Component As Prop with Props | ReactJs

We can pass component as props in React JS, it is very simple and easy. We can also pass props with components in React with the specified components. Here we are going to create one simple example to pass one component as props into another component. 

React Pass Component As Prop with Props | ReactJs Example

Let’s create two components and pass one component as props in another component.

<code>const ChildComponent = (props) => {
  return (
  <h1>{props.title}</h1>
);
}

const ParentComponent = () => {
render() {

  const MyComponent = this.props.myComponent; 
    return (
      <div> 
         <MyComponent title = {this.props.title} /> 
      </div>
    );
  }

}

const TestComponent = () {

return (
<ParentComponent  myComponent={ChildComponent} />
)

}</code>

In the same way, you can pass any component as props in react. 


Advertisements

Add Comment

📖 Read More