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> } ......
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.
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.
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!
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.
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 }
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> ); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With