-
Notifications
You must be signed in to change notification settings - Fork 49.6k
Treat [undefined]
as valid PropTypes.renderable
#1668
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
Conversation
CHANGELOG.md
Outdated
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.
No need to update the changelog here; we'll do it when we do a release (and organize it in a way that hopefully makes more sense than the individual commit bullets points).
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.
👍 Removed CHANGELOG
entry.
Removed |
If you start passing undefined values in to a renderable slot you can no longer have default values for that same slot. This is a potential refactoring hazard. It's also easy to accidentally return undefined by missing a return case in a function. function render() {
if (x) return <Foo />;
if (y) return <Bar />;
// undefined
} Failing this type check helps prevent such errors. I can see that it's a pain to check for the existence of a value. :( <Foo component={this.props.component || null} /> This does forces you to make the choice if you want to use the default prop or explicitly null. You can always make a renderable prop optional which is suppose to be the same as undefined. |
What about: If |
@sebmarkbage We should at least make |
Yes, I agree with @spicyj to allow react/src/core/__tests__/ReactPropTypes-test.js Lines 387 to 398 in 927eb57
|
Ideally it would be the same behavior for top-level and nested. You can implement default props as ES6 destructuring which could introduce similar issues at the array level. There's also a problem if you don't have defaults and don't have explicit prop types in intermediate components. This could warn you about undefined values even without explicit propTypes: <Wrapper>{this.props.left}{this.props.right}</Wrapper> It's disturbing that it's relaxing the type check when it's nested. Reopening for proper discussion. @mathieumg An optional boolean can have an explicit default prop. Makes it a little bit easier to reason about the actual value if it's only true/false. |
@sebmarkbage Isn't this a reasonable piece of code to write in render?
I guess that's not 100% the same as what we're talking about here, but it's close. |
@spicyj Not really, no. It looks like an accidental missing But even then, that's a mutable structure. I'd probably prefer the |
Thanks for reopening for discussion, @sebmarkbage. I definitely want to find a solution that allows for more use cases of
Can you give an example?
If you believe that, shouldn’t In the original example I gave in #1647, what style would you recommend to address that |
Any update on this? |
I think the most convincing use case for me is the case where you have an optional property.
This might be reason enough to allow this. However the main issue is that this resolves differently for var { childOrUndefined = <Foo /> } = this.props; Therefore passing undefined may actually change behavior of underlying classes in confusing ways. Seems like a little bit of boilerplate to ensure which one you mean seems like a decent way to resolve this issue and ensure that refactoring doesn't cause unforeseen bugs. |
Closing this out again because I think that making it optional is enough. Will follow the lead from Flow here and align with Flow once it settles. I still think undefined needs to be treated differently from null here. |
null
vsundefined
#1647: React no longer issues a warning when passing in[undefined]
asReact.PropTypes.renderable
, hence treating it the same as[null]
.Notes