2

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

2
  • 1
    A cleaner way of writing components may be using ES6? i.imgur.com/7EHQRdl.png Commented Dec 10, 2016 at 3:58
  • 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 Commented Oct 25, 2018 at 23:33

1 Answer 1

5

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'
    };
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.