Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a React Component as argument in between functions of a Container Component

Tags:

reactjs

Passing Child Components as arguments to functions of Parent Component and trying to render does not work

//React Container Component //Import Both Views and Render based on preference import PosterView from "./PosterView" import ListView from "./ListViewCard"  ...  renderCardsBasedOnType(cardType){   if(cardType === "poster"){      return this.renderCards(PosterView)   }else{      return this.renderCards(ListViewCard)   } } renderCards(component){   let cards =  this.props.list.map(function(cardData){      return <component data={cardData}/>    })   return cards } render(){   let cards = this.renderCardsBasedOnType("poster")   return <div>{cards}</div> } ...... 
like image 261
guru107 Avatar asked Jun 24 '16 13:06

guru107


People also ask

How do you pass a component to a function in React?

First, you'll need to create two components, one parent and one child. Next, you'll import the child component in the parent component and return it. Then you'll create a function and a button to trigger that function. Also, you'll create a state using the useState Hook to manage the data.

Is there any other way of passing data between components in React?

Props. Props allows us to pass data between React components. We can pass callback functions and other pieces of data. We can attach multiple props to each component.

Can you pass a React component as 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!

Can I pass ref as prop?

Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.


2 Answers

Try Component instead of component. React requires an upperCase for jsx tag:

renderCards(Component){   let cards =  this.props.list.map(function(cardData){      return <Component data={cardData}/>    })   return cards } 
like image 90
Damien Leroux Avatar answered Sep 19 '22 14:09

Damien Leroux


Thanks to Damien's answer, here's the same answer using TypeScript with React.

Instead of this, i.e. a simple component without a wrapper ...

export const SiteMapContent: FunctionComponent = () => {   return (     <React.Fragment>       <h1>Site Map</h1>       <p>This will display the site map.</p>     </React.Fragment>   ); } 

... you can pass a component into a wrapper to be rendered there, like this ...

const renderContent: FunctionComponent<FunctionComponent> = (Foo: FunctionComponent) => {   return (     <div className="foo">       <Foo />     </div>   ) }  export const SiteMap: FunctionComponent = () => {   return renderContentOne(SiteMapContent); }  const SiteMapContent: FunctionComponent = () => {   return (     <React.Fragment>       <h1>Site Map</h1>       <p>This will display the site map.</p>     </React.Fragment>   ); } 

Again the name of the parameter must be upper-case e.g. Foo not foo.

A different way of course, instead of passing a component as asked in the OP is to pass an element instead of a component (in which case the parameter name needn't be upper-case):

const renderContent: FunctionComponent<ReactElement> = (foo: ReactElement) => {   return (     <div className="foo">       {foo}     </div>   ) }  export const SiteMap: FunctionComponent = () => {   const foo: ReactElement = <SiteMapContent />;   return renderContentOne(foo); }  const SiteMapContent: FunctionComponent = () => {   return (     <React.Fragment>       <h1>Site Map</h1>       <p>This will display the site map.</p>     </React.Fragment>   ); } 
like image 27
ChrisW Avatar answered Sep 18 '22 14:09

ChrisW