-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
React.Children.isValidChildren similar to React.isValidElement #9791
Comments
What types of invalid children are you looking to test for? Have you tried using |
Yep. Sorry I should I have been more clear in the initial issue.
Agreed. This issue is specifically for React's
I'm writing a library that provides some validation logic on props / state, but would like to skip the validation if given the 'opaque` children data structure. Currently I do this by checking if the key of the prop is 'children', but it is possible that the developer reassigns the value of the 'children' prop to a different prop key or to a key on the state. Instead it would be nice to be able to simply check if the value itself is React's 'opaque' children data structure. I think it might be possible to use to use some of the existing children helper functions to do this via some sort of try/catch, but that seems brittle and hacky. |
I'm not entirely sure what you're trying to build, but tl;dr there is no such thing as invalid children, any value is a valid child and probably always will be. |
Is that true? I think objects are invalid children (React 15.5.4) (#3473)
Right, currently 'children' could be any arbitrary data structure or primitive. However, as devs we're not supposed to care, and we need to treat it as a black box value. I think the suggested name of After thinking about my specific use case more, I'm not sure if having this function would cover all the cases, so I think I might need to think a bit more on this. However, having this function would cover a majority of the cases that I need to cover, and I do still think it would be nice to have this type of utility function (for example, |
Yeah, but partly because they used to be treated as |
So the question here isn't really how to verify valid children, but how to validate that a value can be returned from I think it's reasonable to defer this validation to React since it can provide much better error messages in most cases, especially with the inclusion of the render stack in 16. I feel that providing an API for testing this would encourage library authors to implement their own error messaging, and I'm not sure that's a good thing in the general case. I mentioned |
That makes sense.
This is probably the approach that I will take :) |
Do you want to request a feature or report a bug?
feature
What is the current behavior?
No
isValidChildren
method availableWhat is the expected behavior?
Returns true if passed in the 'opaque' children object
The text was updated successfully, but these errors were encountered: