KLove
KLove

Reputation: 1

How do I properly set up my Babel plugins and presets to make es6 decorators work in my React app?

I have a React app going, and I'm trying to get the hang of decorators. They're causing unexpected token errors.

I'm using the transform-decorators-legacy and transform-class-properties babel plugins.

I've tried both the stage-0 and stage-2 presets. All the related posts on SO and elsewhere seem to think that the plugins should make it work. Some say to use the stage-0 preset, while others recommend stage-2.

Here's the file with the decorator:

import React, {Component} from 'react';
import Actions from '../firebase/actions';
import RecipeList from '../Recipe/recipe-list';
import connectToStores from 'alt-utils/lib/connectToStores';
import RecipeStore from '../../store/recipe-store';

@connectToStores
class Main extends Component {
    constructor() {
        super();
        Actions.getRecipes();
    }

    static getStores() {
        return [RecipeStore];
    }

    static getPropsFromStores() {
        return RecipeStore.getState();
    }

    render() {
        return (
                <section>
                    <section className="container">
                        {
                            this.props.recipes
                            ?
                            <RecipeList recipeList={this.props.recipes}/>
                            :
                            null
                        }
                    </section>
                </section>

        );
    }
}

export default Main;

Here's the package.json file:

{
  "name": "glutenfreehacker",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "firebase": "^4.12.1",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.1.4",
    "recompose": "^0.26.0",
    "redux": "^4.0.0"
  },
  "babel": {
    "presets": [
      "env",
      "stage-2",
      "react"
    ],
    "plugins": [
      "transform-decorators-legacy",
      "transform-class-properties"
    ]
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

I understand this has been discussed here and elsewhere. However, the solutions I've seen suggest using the above plugins and presets, and I still can't get it to work.

Thanks for reading, and any help is greatly appreciated.

Upvotes: 0

Views: 562

Answers (1)

Patrick Hund
Patrick Hund

Reputation: 20236

For decorators, use the babel-plugin-transform-decorators.

Installation

npm install --save-dev babel-plugin-transform-decorators

Usage

Via .babelrc:

{
  "plugins": ["transform-decorators"]
} 

Upvotes: 0

Related Questions