1

In one of my .js file, I'm getting the following error from Webpack:

  4 | export default function(ComposedComponent) {
  5 |   class Authentication extends Component {
> 6 |     static contextTypes = {
    |                         ^
  7 |       router: React.PropTypes.object
  8 |     }
  9 | 

These are my devdependencies:

  "devDependencies": {
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-3": "^6.24.1",
    "css-loader": "^0.26.1",
    "file-loader": "^1.1.4",
    "html-webpack-plugin": "^2.30.1",
    "image-webpack-loader": "^3.4.2",
    "rimraf": "^2.6.2",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.9",
    "webpack": "2.2.0-rc.0",
    "webpack-dev-server": "^2.2.0-rc.0"
  }

And this is my Webpack configuration:

{
  "presets": ["env", "react", "stage-3"],
  "plugins": ["transform-object-rest-spread"]
}

Could anybody tell which plugin or package is missing? Thank you!

1
  • Please look at this and this. You just need to add correct babel presets to be able to use static class properties. Commented Oct 2, 2017 at 21:34

2 Answers 2

1

Nothing is missing, it's just incorrect syntax.

Static methods should be (as the name implies) functions.

Try:

class Authentication extends Component {
   ...
}

Authentication.contextTypes = {
    router: React.PropTypes.object
}
Sign up to request clarification or add additional context in comments.

4 Comments

Same issue... Now, it's pointing to the . between the Authentication.contextTypes. Interestingly, it was OK with the old setup when I downloaded the tutorial, it's just not working with my new Webpack transpiler.
Longshot but try babel-preset-es2015 and add es2015 to your presets
@Daniel is right. You just have to add correct presets. Look at my comment for links.
Thank you for your efforts, but I just keep getting error messages. This time is Uncaught Error: _registerComponent(...): Target container is not a DOM element - even thought the script is at the documents bottom. :(
1

Class properties is a stage 2 proposal at the moment, so it's not included in babel-preset-env. You can either:

  • include only this specific transform with .babelrc:

    { "plugins": ["transform-class-properties"] }

  • include all stage 2 proposals with :

    { "presets": ["env", "react", "stage-2"] }

Please consider the risks before adding all stage-2 proposals : https://babeljs.io/docs/plugins/preset-stage-2/

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.