I'm curious if it is possible to achieve this when using functional components?
Inside of my App.js, found in the first code snippet below, I want to send a Component (here: PageMain) with some props to the HOC.
When trying to do this by sending a JSX with the resp. prop, I receive the ff. error:
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: . Did you accidentally export a JSX literal instead of a component?
Which makes sense according to the description, because I am trying to send this instead of an actual Component:
<PageMain childContent={<p>Hey I'm the child component content!</p>
But is there a clean way to achieve this? My idea is to forward the prop to the ultimate receiver, so PageMain can work with
{props.childContent && <>props.childContent</>}
My App.js:
import { PageMain } from "./MyHOCStuff/PageMain";
import { Header } from "./MyHOCStuff/Header";
import { Footer } from "./MyHOCStuff/Footer";
import { withLayout } from "./MyHOCStuff/withLayout";
const PageWithLayout = withLayout(<PageMain childContent={<p>Hey I'm the child component content!</p>} />, Header, Footer)
function App() {
return (
<>
<PageWithLayout message={"Hello from within a HOC."} />
</>
);
}
export default App;
My HOC:
import React from "react";
export const withLayout = (PageComponent, HeaderComponent, FooterComponent) => {
return function WithPage({ ...props }) {
console.log(PageComponent.props);
return (
<div>
<HeaderComponent />
{props.message && <h1>{props.message}</h1>}
<PageComponent />
<FooterComponent />
</div>
);
};
};
The Component, which should ultimately receive the prop:
export const PageMain = ({...props}) =>{
return <>
<h2>Page Content Stuff</h2>
<p>Lorem ipsum dolor sit amet, consectetur adip bla bla bla...</p>
{props.childContent && <>props.childContent</>}
</>;
}