Open meetzaveri opened 6 years ago
I'm confused; defaultProps only applies to top-level prop values, by design. If you pass an empty object, then the default simply won't apply.
This is a small part of the reason why flat props (ie, not an object) are preferred.
This is a small part of the reason why flat props (ie, not an object) are preferred.
That's good to know. I'll keep in mind for better practices in this kind of structure
Do you want to request a feature or to report a bug
Both(Either of them). Report a Bug/Feature. Not sure if this would be a potential bug. This could be a feature when we can explicitly define a property via
.defaultProps
What is current behavior (Re-produce) ?
props.derivedState
is an object whose one of the property israndomString
. It cannot be initialized i.e. set to default value using.defaultProps
in case when I am not settingrandomString
indereivedState
object.(intentionally to test)Basically object/array(or any other primitive element) can be set to default as per
defaultProps
functionality. But when setting property of an object, I am facing this issue that I cannot initialize it.What is the expected behavior?
By doing this,
the
props.derivedState
's property(i.e.randomString
) can be set to default value to "Stranger".Use cases (Codesandbox links):
I experienced that when instead of not setting
randomString
I completely didn't passed attributederivedState
(which is prop) which resulted in printing "Stranger" (randomString
property ). This is reproduced in use case 2