React-rails: Using PropTypes in ES6

Created on 18 Aug 2016  路  3Comments  路  Source: reactjs/react-rails

Help us help you! Please choose one:

  • [x] My app crashes with react-rails, so I've included the stack trace and the exact steps which make it crash.
  • [ ] My app doesn't crash, but I'm getting unexpected behavior. So, I've described the unexpected behavior and suggested a new behavior.
  • [ ] I'm trying to use react-rails with another library, but I'm having trouble. I've described my JavaScript management setup (eg, Sprockets, Webpack...), how I'm trying to use this other library, and why it's not working.
  • [ ] I have another issue to discuss.

In a pure JS-app I would import PropTypes like so: import React, {PropTypes} from 'react';

As react-rails don't follow that pattern, how do I implement PropTypes in Rails (ES6 style)?

I'm receiving the following error when I try to define proptypes:

Uncaught ReferenceError: PropTypes is not defined

Code:

class MyComponent extends React.Component {
    constructor(props) {
      super(props);
    }

    render () {
      return (
        <div>
          Hello World.
        </div>
      );
    }
}

MyComponent.propTypes = {
  thingOne: PropTypes.string.isRequired,
  thingTwo: PropTypes.string.isRequired
};

Most helpful comment

For anyone still arriving here from Google:
In React-Rails 2.4+ we are using React 16 which does not package PropTypes anymore, instead import PropTypes from 'prop-types' and use like so:

MyComponent.propTypes = {
  property: PropTypes.string
};

All 3 comments

This is how I eventually got it to work:

class MyComponent extends React.Component {
}

MyComponent.propTypes = {
  someProperty: React.PropTypes.string.isRequired,
  someOtherProperty: React.PropTypes.array.isRequired
};

For anyone still arriving here from Google:
In React-Rails 2.4+ we are using React 16 which does not package PropTypes anymore, instead import PropTypes from 'prop-types' and use like so:

MyComponent.propTypes = {
  property: PropTypes.string
};

Just so others know, in order to use import or require you'll need to use webpacker instead of the Asset Pipeline, as per the README.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scottbarrow picture scottbarrow  路  5Comments

prasanthrubyist picture prasanthrubyist  路  3Comments

dongtong picture dongtong  路  3Comments

okolomoets picture okolomoets  路  5Comments

dylanitorium picture dylanitorium  路  4Comments