IC_
IC_

Reputation: 1839

Default values of props in ReactJS

How can I define default values in ReactJS?

var Header = React.createClass({
    render: function() {
      return (
         <h1>{this.props.title} if not defined insert "Title"</h1>
      )
    }
});

I extremely increased my React skill and as of 2018 this example (I wrote at the question body) is outdated. You should do it like @VladyVeselinov shown at the picture using ES6 classes and babel transforms like babel-class-properties that allow you to make static fields for your components

Upvotes: 2

Views: 6872

Answers (1)

Marc Bernstein
Marc Bernstein

Reputation: 110

Since your example uses the createClass version of creating a component, you can use the getDefaultProps function.

https://facebook.github.io/react/docs/react-without-es6.html#declaring-prop-types-and-default-props

 getDefaultProps: function() {
    return {
      title: 'Title'
    };
  }

Upvotes: 5

Related Questions