With the introduction of React Hooks, the documentation recommends using function components instead of class components in the future.
However, I wonder how to define and type the component's properties when I want to access children components. I have come up with this solution (I am using Typescript):
import * as React from "react";
import "./styles.css";
type MyWrapperCompProps = {
color: string;
children?: React.ReactNode;
};
function MyWrapperComp(props: MyWrapperCompProps) {
return <div style={{ backgroundColor: props.color }}>{props.children}</div>;
}
export default function App() {
return (
<div className="App">
<MyWrapperComp color="tan">
<h1>Hello World!</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Porro, fuga.
</p>
</MyWrapperComp>
</div>
);
}

This seems to work, but I have a couple of questions:
- Is this the best practice to model wrapper components with children?
- Is
ReactNode the correct, most general type for children?
- How does React automagically know that the children are assigned to
props.children? Is this hardwired to the name children or is there anything I should be aware of?
- Is the above documented anywhere at all on the React website?
Thanks a lot in advance for your help.
With the introduction of React Hooks, the documentation recommends using function components instead of class components in the future.
However, I wonder how to define and type the component's properties when I want to access children components. I have come up with this solution (I am using Typescript):
This seems to work, but I have a couple of questions:
ReactNodethe correct, most general type forchildren?props.children? Is this hardwired to the namechildrenor is there anything I should be aware of?Thanks a lot in advance for your help.