Skip to content
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

use defaultProps when an attribute of an object is undefined? #16331

Closed
kud opened this issue Aug 9, 2019 · 4 comments

Comments

@kud
Copy link

commented Aug 9, 2019

Do you want to request a feature or report a bug?

Maybe a bug?

What is the current behavior?

https://codesandbox.io/s/green-snow-79z65

When you give undefined to a direct attribute, it will use the defaultProp.

When you give undefined to an attribute of an object, it'll use this undefined

What is the expected behavior?

I'm not sure but I'm waiting for using the defaultProp even for an attribute of an object?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

react 16.9.0

@kud kud changed the title defaultProps and object? use defaultProps when an attribute of an object is undefined? Aug 9, 2019

@Dergash

This comment has been minimized.

Copy link

commented Aug 9, 2019

Hello @kud! I don't think it's a bug. You do provide an object { attribute: undefined } for your Components object property:

 <Checker
    object={{
        attribute: undefined
    }}
/>

so technically it isn't undefined anymore, and in that case defaultProps aren't applied.

You probably could just write your own getDefaultObject(props) function with custom logic merging your default object and props:

<Checker
    object={getDefaultObject(props.object)}
/>

const defaultObject = { attribute: "attribute should be displayed too" }

function getDefaultObject(propsObject ) {
    return { ...defaultObject, ...propsObject }
}

Of course if you want automatically merge objects of any shape you'll have to use some library or write your own method for traversing both object trees and merging, which could hit perfomance and I think that is the reason why defaultProps can't do it by its own.

@nortonwong

This comment has been minimized.

Copy link

commented Aug 9, 2019

Deep merging objects isn't really appropriate for a library to decide. If object had multiple mutually exclusive schemas, e.g. object={{ foo: true }} or object={{ bar: 30 }}, it would be wrong for the library to force a deep merging strategy.

@aweary

This comment has been minimized.

Copy link
Member

commented Aug 9, 2019

Correct, React does not traverse objects when calculating props with defaultProps, so this is expected behavior.

@aweary aweary closed this Aug 9, 2019

@kud

This comment has been minimized.

Copy link
Author

commented Aug 9, 2019

Understood! Thanks for your kind explanation! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.