I want to create a reusable components in my website. I have created a following components
const Header: React.FC = () => …
const Content: React.FC = () => …
const MyCustomComponent = {Header, Content};
export default MyCustomComponent;
From other components I can able to consume like below
import MyCustomComponent …
…
<MyCustomComponent.Header />
<MyCustomComponent.Content />
But my expectation is, those component should be wrapped like below
import MyCustomComponent …
…
<MyCustomComponent>
<MyCustomComponent.Header />
<MyCustomComponent.Content />
</MyCustomComponent>
My Goal is I can able to pass more children props to the wrapper as well. At the same time children component should be call under parent component.
Thanks in advance.
2
Answers
In React(>18) the following code works. Thanks for the support
in MyCustomComponent.tsx
There’s nothing that stops you from doing this:
except that such usage of components is not documented anywhere and might have inconsistent behaviour.