React - Content projection
React - Content projectiontsx
import { ReactNode, FC } from 'react';
interface CardProps {
header: ReactNode;
children: ReactNode;
}
const CardComponent: FC<CardProps> = ({ header, children }) => {
return (
<>
<div className="header">{header}</div>
<div className="content">{children}</div>
</>
)
};
const AppComponent: FC = () => {
return (
<CardComponent
header={<h1>It's a title</h1>}
>
<img src="card-image.png"/>
<div>Content of a Card</div>
</CardComponent>
)
}
Use ReactNode type to mark properties that will receive HTML content or other React components
Define layout and styles for the Card. Allow to pass header and content from parent components. It will make it flexible and reusable.
In parent component pass the content you want to render in header and in the body of the Card Component.
You can also extract the content to dedicated smaller component.
Rate
Share
Language
Tags
Reserved place for future advertisments!