【问题标题】:Why isn't ReactRedux defined?为什么没有定义 ReactRedux?
【发布时间】:2016-07-24 10:25:59
【问题描述】:

我正在使用 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);

由于某种原因,当我运行 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"
    }
});

【问题讨论】:

    标签: javascript reactjs webpack babeljs redux


    【解决方案1】:

    这是因为 react-redux 包在模块上没有默认导出。您可以手动访问连接功能,例如:

    import { connect } from 'react-redux';
    

    ...

    export default connect(mapStateToProps)(Layout);
    

    【讨论】:

    • 或者,import * as ReactRedux from 'react-redux';
    • 是的,我在发布后不久就意识到了! :)
    • PS:我刚刚从 require 转换为 import 这就是为什么我对相同的代码停止工作感到特别困惑
    猜你喜欢
    • 2020-01-01
    • 2013-08-16
    • 1970-01-01
    • 2018-02-11
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    相关资源
    最近更新 更多