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 a different value.
Syntax
ES5
var MyClass = React.createClass({
getDefaultProps: function() {
return {
randomObject: {}
// ...
};
}
});
ES6
class MyClass extends React.Component {}
MyClass.defaultProps = {
randomObject: {}
// ...
};
ES7
class MyClass extends React.Component {
static defaultProps = {
randomObject: {}
// ...
};
}
The result of getDefaultProps() or defaultProps is cached and used to ensure that this.props.randomObject has a value if the parent component did not specify one. Class fields such as static defaultProps need a transpiler; they were not part of ES6.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents