2016-04-04 59 views
2

我正在使用webpack和babel。我有這樣一個文件:爲什麼不定義ReactRedux?

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" 
    } 
}); 

回答

5

這是因爲react-redux軟件包在模塊上沒有默認導出。您可以訪問連接功能手動喜歡:

import { connect } from 'react-redux'; 

...

export default connect(mapStateToProps)(Layout); 
+1

,或者,'進口*爲ReactRedux從「反應 - 終極版」;' –

+0

是的,我意識到,不久後發帖! :) –

+0

啊謝謝!與* :) –