prop-types
prop-types copied to clipboard
Default Proptypes : Not able to set default prop value for object's property(s) if property is not defined
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 is randomString. It cannot be initialized i.e. set to default value using .defaultProps in case when I am not setting randomString in dereivedState 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.
// in app.js
<div>
<h1>{this.props.derivedState.randomString}</h1>
{/* PRINTS NOTHING (In case : if randomString was not defined/set as property in object derivedState) */}
</div>
...
App.defaultProps = {
derivedState: {
randomString: "Stranger"
}
};
What is the expected behavior?
By doing this,
// in app.js
<div>
<h1>{this.props.derivedState.randomString}</h1>
{/* PRINTS STRANGER (In case : if randomString was not defined/set as property in object derivedState) */}
</div>
...
App.defaultProps = {
derivedState: {
randomString: "Stranger"
}
};
the props.derivedState's property(i.e. randomString) can be set to default value to "Stranger".
Use cases (Codesandbox links):
- Use case 1 (where nothing gets printed) - https://codesandbox.io/embed/j229563jw
I experienced that when instead of not setting randomString I completely didn't passed attribute derivedState(which is prop) which resulted in printing "Stranger" (randomString property ). This is reproduced in use case 2
- Use case 2 (where stranger gets printed when no) - https://codesandbox.io/s/4x7vm2z39w
Please feel free to submit a PR for this!
Any update on this? With regenerator-runtime auto pollutes the global-scope, there's no way to achieve a 100% pure polyfilling without scope pollution as described in https://github.com/babel/babel-polyfills/issues/59