I have a question regarding the element which is inside custom parent component, how to catch it and render it.
For example:
Parent
interface ComponentProps{
message: string;
}
export default MyComponent: FunctionComponent = <ComponentProps>(props: ComponentProps) => {
render (
<div>{props.message}</div>
)
}
Now, imagine i call this component with inside elements, like this
<MyComponent message='Test'>
<div>How to render this div inside MyComponent, how to catch it?</div>
</MyComponent>
So, the question is how to accept this inside MyComponent and render it as wel?
Thanks in advance
2
Answers
this should be pretty straight forward,
your can have a property called
children
with the type ofReact.ReactNode
and use this children on theMyComponent
the place where u are using this
with the above changes your custom div with text "How to render this div inside MyComponent, how to catch it?" is going to rendered within MyComponent
Here is a more practical example demonstrating the
children
property of a functional component in React.The wrapper component will return a wrapped version of the children passed to it and display a message alert box at the top.
Here is a working JSX snippet of the TSX code above above.