-
Notifications
You must be signed in to change notification settings - Fork 46.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
Ability to use return value of React.Children.map with React.Children.only #13355
Comments
I agree the current situation is confusing. Thanks for digging up those links. See also my attempt to analyze the behavior in #12136 (comment). Let's keep this open for now I guess. It keeps coming up. If you have suggestions for how it should work, please file an RFC: https://github.com/reactjs/rfcs. We can then discuss it and see if the way forward makes sense. Thanks! |
According to Docs v16.4.2 React.Children.only, React.Children.only is a kind of test function but not a filtering function and expects a single As this is a function which throws error in case of unexpected arguments, it should be better to use with the conjunction of componentDidCatch as described in Error Handling in React 16. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
As stated in the docs and shown in #4410, the return value of
React.Children.map
is incompatible with theReact.Children.only
function, and will return with the error message:if used.
This incompatibility causes some fairly annoying restrictions, however. From what I understand, this means that you can't edit the properties of a component's children at runtime if they contain any component that requires a single child. (eg. you can't dynamically set the disabled prop of a child
TouchableNativeFeedback
component in the parent).I believe this is due to the type differences between the
this.props.children
parameter and theReact.Children.map
return value, the former being a valid element and the latter not (see #4424 and reactjs/react.dev#87).I'd like to make a request to have the return type of
React.Children.map
be the same as the type ofthis.props.children
, which will fix these quirks and be much more intuitive. A separate static function that converts the return type ofReact.Children.map
to the same type asthis.props.children
would also solve the issue. It feels like some sort of fix is in order, since it's a broad and strange restriction on the components you can use.The text was updated successfully, but these errors were encountered: