-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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.cloneElement handles undefined
props differently to React.createElement
#5929
Comments
Ooh, interesting! AFAICT Whether or not this is intended or |
I was thinking cloneElement probably shouldn't be aware of defaultProps as such, it could just make that same check as createElement for typeof 'undefined' to determine whether the new prop gets copied over. That way undefined and null would have the same semantics as createElement. |
I think this is a bug.
|
I made a PR for this :) . #5997 |
Why would it restore the default value? It's not the behavior I would expect intuitively. |
@mattzeunert Because otherwise you could create an element using |
cc @sebmarkbage - I don't recall if we talked about this before but what should our expected behavior here be? defaultProps resolution was explicitly only resolved during createElement but now we're adding a second pass for cloning. |
Yea, I think this is a bug. For the reason @jimfb mentioned above. The idea is that this should guarantee that you never get undefined: type Props = { foo: number };
function Foo(props : Props) {
}
Foo.defaultProps = {
foo: 123
}; Another example to explain this semantic is that <element.type {...element.props} key={element.key} ref={element.ref} /> |
Not sure if @truongduy134 has a PR that's going to get merged. I reused the same defaultProp process that is used in createElement in cloneElement to fix this bug. #6037 |
Fix #5929. Resolve to default props when config key is set to undefined in cloneElement
Uhh.. so what happens now if I use Will the property:
|
It does the same thing as React.createElement, which is to set the property to the value in defaultProps. So item # 2 in your list. If you want to look at the PR that fixed this, it was fixed in #5997. |
I'm not sure if this is expected but it caught me out earlier:
The text was updated successfully, but these errors were encountered: