-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated with explicit children in mind #510
Updated with explicit children in mind #510
Conversation
409e889
to
4db5b44
Compare
children1: JSX.Element; // bad, doesnt account for arrays | ||
children2: JSX.Element | JSX.Element[]; // meh, doesn't accept strings | ||
children3: React.ReactChildren; // despite the name, not at all an appropriate type; it is a utility | ||
children4: React.ReactChild[]; // better, accepts array children | ||
children: React.ReactNode; // best, accepts everything (see edge case below) | ||
functionChildren: (name: string) => React.ReactNode; // recommended function as a child render prop type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no sensible recommendation for render prop type since it depends heavily on what you want to do. The pattern itself is rarely used nowadays so let's not overwhelm people with React patterns at this point.
children1: JSX.Element; // bad, doesnt account for arrays | ||
children2: JSX.Element | JSX.Element[]; // meh, doesn't accept strings | ||
children3: React.ReactChildren; // despite the name, not at all an appropriate type; it is a utility | ||
children4: React.ReactChild[]; // better, accepts array children |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is deprecated.
@@ -59,12 +59,8 @@ Relevant for components that accept other React components as props. | |||
|
|||
```tsx | |||
export declare interface AppProps { | |||
children1: JSX.Element; // bad, doesnt account for arrays | |||
children2: JSX.Element | JSX.Element[]; // meh, doesn't accept strings | |||
children3: React.ReactChildren; // despite the name, not at all an appropriate type; it is a utility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is deprecated.
@@ -59,12 +59,8 @@ Relevant for components that accept other React components as props. | |||
|
|||
```tsx | |||
export declare interface AppProps { | |||
children1: JSX.Element; // bad, doesnt account for arrays |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't necessarily mean it's bad. This might be intended.
@@ -68,7 +68,6 @@ function CommentList({ data }: WithDataProps<typeof comments>) { | |||
} | |||
interface BlogPostProps extends WithDataProps<string> { | |||
id: number; | |||
// children: ReactNode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear why this was here. better remove it if it's unused.
@@ -346,79 +346,6 @@ class List<T> extends React.PureComponent<Props<T>, State<T>> { | |||
} | |||
``` | |||
|
|||
### Generic components with children | |||
|
|||
`children` is usually not defined as a part of the props type. Unless `children` are explicitly defined as a part of the `props` type, an attempt to use `props.children` in JSX or in the function body will fail: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
children
now have to be defined explicitly so this section is obsolete.
4db5b44
to
1a6f39f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Structurally looks good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Not sure I understand why newer TS versions would reject <Component />
but not <Component></Component>
though. Can you explain?
https://tkdodo.eu/blog/optional-vs-undefined explains this in more detail |
Also ensured that
ReactNode
children
are always optional. They already includeundefined
so newer TS versions would reject<Component />
but not<Component></Component>
which is quite annoying.Part of #495