I'm probably doing something stupid, but I can't get defaultProps to work.
export default class MyClass extends Component{
static propTypes = {
name: React.PropTypes.string.isRequired,
field: React.PropTypes.object.isRequired
}
static defaultProps = {
field: { value: '', errors: [] }
}
render() {
// blah blah
}
}
I have code that relies on this.props.field.value
and this.props.field.errors.length
and all my tests are blowing up with TypeError: 'undefined' is not an object (evaluating 'this.props.field.errors.length')
, shouldn't my default props give it a default value? Initially, my field
prop is an empty object.
Initially, my
field
prop is an empty object.
Default props are only used if no value is passed for the prop. It is is shallow merge, not a deep merge.
From the docs (emphasis mine):
The result of
getDefaultProps()
will be cached and used to ensure thatthis.props.value
will have a value if it was not specified by the parent component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With