简体   繁体   English

为什么未定义ReactRedux?

[英]Why isn't ReactRedux defined?

I am using webpack and babel. 我正在使用webpack和babel。 I have a file like this: 我有一个像这样的文件:

import React from 'react';
import ReactRedux from 'react-redux';

var Layout = React.createClass({
  render(){
    return (<div>Markup</div>);
  }
});


function mapStateToProps(state, action) {
  return state;
}

export default ReactRedux.connect(mapStateToProps)(Layout);

For some reason when I run webpack, after compiling, it runs with this error: Cannot read property 'connect' of undefined . 出于某种原因,当我运行webpack时,编译后它会运行,并显示以下错误: Cannot read property 'connect' of undefined Not sure why it would fail at getting ReactRedux object. 不知道为什么它在获取ReactRedux对象时会失败。 My webpack config is like this: 我的webpack配置是这样的:

var compiler = webpack({
    entry: "./dist/runner.js",
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          exclude: /(node_modules|bower_components)/,
          loader: 'babel', // 'babel-loader' is also a legal name to reference
          query: {
            presets: ['es2015', 'react']
          }
        }
      ]
    },
    devtool: 'source-map',
    output: {
      filename: "public/dist/bundle.js"
    }
});

This is because the react-redux package doesn't have a default export on the module. 这是因为react-redux软件包在模块上没有默认导出。 You can access the connect function manually like: 您可以手动访问连接功能,例如:

import { connect } from 'react-redux';

... ...

export default connect(mapStateToProps)(Layout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM