Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ES7 static propTypes with React-Native

When I'm launching my project using React-Native default packager, I have this error: Unexpected token on this line:

static propTypes = {
   /// ...
};

I took a look on React-Native issues on GitHub, but I didn't find a solution.

Any suggestion?

like image 905
Jean Lebrument Avatar asked Nov 09 '15 18:11

Jean Lebrument


People also ask

Should I use proptypes in my React Native components?

I highly recommend using PropTypes in your react native components, to avoid unexpected glitches to the user. Let’s take a look at the example below. The component ‘ MyComponent ’ has a set of defined PropTypes. The props here are defined with their respective PropTypes for type-checking.

How to declare defaultprops as static in React component class?

If you are using a Babel transform like plugin-proposal-class-properties (previously plugin-transform-class-properties ), you can also declare defaultProps as static property within a React component class. This syntax has not yet been finalized though and will require a compilation step to work within a browser.

Is it possible to use typescript with react prop types?

In vanilla React, defining the prop types (via the prop-types) package is optional. But with TypeScript, everything must be typed, either implicitly or explicitly. Below are mappings from PropTypes to TypeScript types that you can use as a resource.

How to check if a prop is a specific type?

For performance reasons, propTypes is only checked in development mode. Here is an example documenting the different validators provided: import PropTypes from 'prop-types'; MyComponent.propTypes = { // You can declare that a prop is a specific JS type.


4 Answers

React-Native packager use Babel for transfer ES6 and ES7, but NOT ALL features. The enable list is here. In your case, class-props is not enabled by default in RN packager. You can use Babel to compiler your code before packager, or just enable it in the packager setting. See this official doc for more information.

like image 127
Fomahaut Avatar answered Oct 17 '22 21:10

Fomahaut


Try appending your propTypes to your class:

var MyClass extends React.Component {
....
}

MyClass.propTypes = {
.... /* enter proptypes here */
}
like image 41
Nader Dabit Avatar answered Oct 17 '22 19:10

Nader Dabit


After @Fomahaut answer, I keep looking on Facebook's GitHub repository and found this issue: https://github.com/facebook/react-native/issues/2182

  • Create a .babelrc file at the project's root directory
  • Add more rules to Babel

Example:

    {
      "retainLines": true,
      "compact": true,
      "comments": false,
      "whitelist": [
        "es6.arrowFunctions",
        "es6.blockScoping",
        "es6.classes",
        "es6.constants",
        "es6.destructuring",
        "es6.forOf",
        "es6.modules",
        "es6.parameters",
        "es6.properties.computed",
        "es6.properties.shorthand",
        "es6.spread",
        "es6.tailCall",
        "es6.templateLiterals",
        "es6.regex.unicode",
        "es6.regex.sticky",
        "es7.asyncFunctions",
        "es7.classProperties",
        "es7.comprehensions",
        "es7.decorators",
        "es7.exponentiationOperator",
        "es7.exportExtensions",
        "es7.functionBind",
        "es7.objectRestSpread",
        "es7.trailingFunctionCommas",
        "regenerator",
        "flow",
        "react",
        "react.displayName"
        ],
      "sourceMaps": false
    }
like image 3
Jean Lebrument Avatar answered Oct 17 '22 20:10

Jean Lebrument


According to this answer, you need to install a plugin for class properties as of Babel 6.

As of Babel 6, you now need the transform-class-properties plugin to support class properties.

Steps:

  1. Run this: npm install babel-plugin-transform-class-properties
  2. Add this to your .babelrc: "plugins": ["transform-class-properties"] (Note, it's a plugin, not a transform; so don't put it in the "presets" list.)

Worked for me.

like image 3
stone Avatar answered Oct 17 '22 21:10

stone