Default props
suggest changedefaultProps allows you to set default, or fallback, values for your component props. defaultProps are useful when you call components from different views with fixed props, but in some views you need to pass different value.
Syntax
ES5
—var MyClass = React.createClass({ getDefaultProps: function() { return { randomObject: {}, ... }; } }
ES6
—``` class MyClass extends React.Component {…}
MyClass.defaultProps = { randomObject: {}, … }
<!-- end version if -->
<!-- if version [ES7] -->
**ES7**
---```
class MyClass extends React.Component {
static defaultProps = {
randomObject: {},
...
};
}
The result of getDefaultProps() or defaultProps will be cached and used to ensure that this.props.randomObject will have a value if it was not specified by the parent component.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents