Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - How to pass props to a component passed as prop

I have a React component (React v15.5.4) that you can pass other components to:

class CustomForm extends React.Component {   ...   render() {     return (       <div>           {this.props.component}       </div>     );   } } 

And I have a different component that uses it:

class SomeContainer extends React.Component {   ...   render() {     let someObjectVariable = {someProperty: 'someValue'};     return (       <CustomForm           component={<SomeInnerComponent someProp={'someInnerComponentOwnProp'}/>}          object={someObjectVariable}       />     );   } } 

Everything renders fine, but I want to pass someObjectVariable prop to the child component inside CustomForm (in this case that'll be SomeInnerComponent), since in the actual code you can pass several components to it instead of just one like the example.

Mind you, I also need to pass SomeInnerComponent its own props.

Is there a way to do that?

like image 617
Daniel Calderon Mori Avatar asked Feb 22 '18 03:02

Daniel Calderon Mori


People also ask

How do you pass a component with props as a prop?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop. Copied!

How do you pass an object as a prop in React?

Use the spread syntax (...) to pass an object as props to a React component, e.g. <Person {... obj} /> . The spread syntax will unpack all of the properties of the object and pass them as props to the specified component. Copied!

How do you pass props from one component to another in React hooks?

Just call an alert method in the childToParent function and pass that function as a prop to the child component. And in the child component, accept the childToParent function as a prop. Then assign it to an onClick event on a button. That's it!


2 Answers

You can achieve that by using React.cloneElement.

Like this:

class CustomForm extends React.Component {   ...   render() {     return (       <div>           {React.cloneElement(this.props.component,{ customProps: this.props.object })}       </div>     );   } } 

Working Code:

class Parent extends React.Component{    render() {      return(        <Child a={1} comp={<GChild/>} />      )    }  }    class Child extends React.Component{    constructor(){      super();      this.state = {b: 1};      this.updateB = this.updateB.bind(this);    }        updateB(){      this.setState(prevState => ({b: prevState.b+1}))    }        render(){      var Comp = this.props.comp;      return (        <div>          {React.cloneElement(Comp, {b: this.state.b})}          <button onClick={this.updateB}>Click to update b</button>        </div>      );    }  }    const GChild = props => <div>{JSON.stringify(props)}</div>;     ReactDOM.render(    <Parent />,    document.getElementById('container')  );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>    <div id='container' />
like image 189
Mayank Shukla Avatar answered Sep 25 '22 23:09

Mayank Shukla


You can do in the same as you did for SomeInnerComponent.

Just pass named props.

Inside CustomForm,

render() {    const MyComponent = this.props.component; //stored it in some variable      return (       <div>           <MyComponent customProps = {this.props.object} /> //access object here and passed it or passed individual props       </div>     );   } 

EDIT :

Please find the working demo here.

like image 22
RIYAJ KHAN Avatar answered Sep 26 '22 23:09

RIYAJ KHAN