为什么没有定义ReactRedux?

Dav*_*ein 2 javascript reactjs webpack babeljs redux

我正在使用webpack和babel.我有这样一个文件:

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);
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当我运行webpack时,在编译之后,它会运行此错误:Cannot read property 'connect' of undefined.不确定为什么它会在获取ReactRedux对象时失败.我的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"
    }
});
Run Code Online (Sandbox Code Playgroud)

Rob*_*der 5

这是因为react-redux包在模块上没有默认导出.您可以手动访问connect函数,如:

import { connect } from 'react-redux';
Run Code Online (Sandbox Code Playgroud)

...

export default connect(mapStateToProps)(Layout);
Run Code Online (Sandbox Code Playgroud)