Default props

suggest change

defaultProps 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents