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
Allow children to be a ReactNode instead of ReactElement #88
Conversation
Since children is just returned by this component, there's no practical reason that children has to be a single element. We've used multiple children in our project, and this type definition made an upgrade incompatible.
With v4, we switched to rendering a function component in order to use hooks. Unlike a class Component, a function component needs to return a JSX Element. To switch to a ReactNode, we'd need to wrap the render return with an element or a React Fragment. There are more details here: DefinitelyTyped/DefinitelyTyped#18051 While I would rather not break the children type, I don't necessarily like wrapping what a consumer passed to children. I would rather let the consumer control exactly what is rendered by wrapping their children in whatever markup or Fragment they'd like. After looking into it more, it seems that the difference in types is solely a TypeScript thing, but there for historical reasons, so I'm not sure what's best. There's a good explanation here: https://stackoverflow.com/a/59840095/3928053 If you come up with a solution that passes type checking and the tests pass without changing them, I'd be happy to merge. |
Ahh, that makes sense :) I'll do some more investigating... |
Long story short - based on Flow's docs and React's behavior, DefinitelyTyped's typings for React.FunctionComponent's return ( |
There's been an issue open about that for a few years DefinitelyTyped/DefinitelyTyped#18051 |
OK, that was more complicated than I thought, so I've updated this PR to use the casting children as ReactElement workaround, seems better than introducing another Fragment layer to the component tree, and it's a workaround that's been done before. Also updated README.md like I should have before. |
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.
Nice investigation! Casting seems like a good solution to this.
released as |
Since children is just returned by this component, there's no practical reason
that children has to be a single element. We've used multiple children in our
project, and this type definition made an upgrade incompatible.