【问题标题】:react-redux's @connect produces the error 'connect.js:129Uncaught TypeError: Cannot read property 'store' of undefined'react-redux 的@connect 产生错误'connect.js:129Uncaught TypeError: Cannot read property 'store' of undefined'
【发布时间】:2016-11-05 01:26:22
【问题描述】:

运行我的应用程序时(成功捆绑了带有装饰器支持的 webpack+babel),控制台显示:

'connect.js:129 Uncaught TypeError: Cannot read property 'store' of undefined'

屏幕上没有显示其他内容 所有软件包都是最新的。

有什么想法吗?谢谢。

app.js:

import React from 'react'
import ReactDOM from 'react-dom'
import Main from './components/main.jsx'

import { combineReducers } from 'redux'
import { Provider, connect } from 'react-redux'
import { createStore } from 'redux'

import * as mainActions from './actions/main_a'
import * as reducers from './reducers/main_r'

const reducer = combineReducers(reducers)
const store = createStore(reducer)

let mapDispatchToProps = (dispatch) => ({
    fireTestAction: () => dispatch(mainActions.testAction( true ))
});

let mapStateToProps = () => ({})

@connect(mapStateToProps, mapDispatchToProps)
export default class App extends React.Component{
    render() {
        return (
            <Provider store={store}>
                <Main txt='Fire test action' action={ fireTestAction} />
            </Provider>,
                document.getElementById('app')
        )
    }
}

var app = new App;
app.render();

package.json:

"devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.10.4",
    "babel-loader": "^6.2.4",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "gulp": "^3.9.1",
    "gulp-exec": "^2.1.2",
    "gulp-nodemon": "^2.1.0",
    "gulp-shell": "^0.5.2",
    "gulp-webpack": "^1.5.0",
    "react-hot-loader": "^1.3.0",
    "redux-devtools": "^3.3.1",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": {
    "express": "^4.14.0",
    "react": "^15.1.0",
    "react-dom": "^15.1.0",
    "react-redux": "^4.4.5",
    "redux": "^3.5.2"
  }
}

【问题讨论】:

    标签: reactjs decorator redux react-redux ecmascript-2016


    【解决方案1】:

    这是因为您在使用提供程序的同一组件上使用connect

    &lt;Provider store&gt;

    使 Redux 存储可用于 下面的组件层次结构。通常,你不能在没有的情况下使用 connect() 将根组件包装在 &lt;Provider&gt; 中。

    https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store

    您应该在&lt;Main&gt; 组件上使用连接。

    编辑:

    另外,如果App 组件是你的入口点,你不需要导出它,你也不需要这样做:

    var app = new App; app.render();

    改为使用 ReactDOM 来呈现应用程序。喜欢:

    ReactDOM.render(
     <Provider store={store}>
      <Main txt='Fire test action' action={ fireTestAction} />
     </Provider>,
     document.getElementById('app')
    )
    

    【讨论】:

      【解决方案2】:

      老问题,但我遇到了这个错误,因为我试图通过调用new MyComponent(props) 从给定的MyComponent: React.ComponentClass 创建一个组件,这不是创建 React 组件的正确方法。正确的方法是拨打React.createElement(MyComponent, props)

      【讨论】:

        猜你喜欢
        • 2021-03-13
        • 2021-06-08
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        • 2017-04-24
        相关资源
        最近更新 更多