首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在我的react-es6项目中使用"context“时出错

在我的react-es6项目中使用"context“时出错
EN

Stack Overflow用户
提问于 2018-03-14 15:15:56
回答 1查看 213关注 0票数 0

我试图在我的es6 react代码中使用"context“,但是在捆绑的时候,我得到了这个错误

export default class Layout extends React.Component {
    static contextTypes = {
        LayoutURL : React.PropTypes.string 
    }; 
    constructor(props, context) {
        super(props, context);
        this.state = {

        }
    }
}

“模块构建失败: TypeError:_jsTokens2.default.matchToToken不是函数”

PFB我的webpack代码(我在resolve中添加了es6,但错误仍然存在)我的webpack版本是"^1.13.0“

 module.exports = {
  entry: './main.js',
  output: { path: __dirname, filename: 'bundle.js' },
    devServer: {
        inline: true,
        port: 8085
    },
  module: {
    loaders: [
      {
        test: /.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  },
resolve: {
  extensions: ['', '.js', '.jsx','.es6']
},
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-04 03:08:33

我发现错误是由语法引起的。更新后的语法:

export default class Layout extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = { //state initialization }
  }
}

Layout.contextTypes={
  LayoutURL : React.PropTypes.string 
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49271707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档