当运行我的应用程序(成功地与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"
}
}发布于 2016-07-03 03:54:39
这是因为您在使用提供程序的同一组件上使用connect。
<Provider store>使Redux存储对于下面组件层次结构中的connect()调用可用。通常,如果不在<Provider>中包装根组件,就不能使用connect()。
https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store
您应该在<Main>组件上使用connect。
编辑:
另外,如果App组件是您的入口点,则不需要导出它,也不需要这样做:
var app = new App; app.render();。
相反,使用ReactDOM来呈现应用程序。比如:
ReactDOM.render(
<Provider store={store}>
<Main txt='Fire test action' action={ fireTestAction} />
</Provider>,
document.getElementById('app')
)发布于 2019-03-04 18:32:34
老问题,但我遇到了这个错误,因为我试图通过调用MyComponent: React.ComponentClass来从给定的new MyComponent(props)创建组件,这不是创建React组件的正确方式。正确的方法是调用React.createElement(MyComponent, props)
https://stackoverflow.com/questions/38165642
复制相似问题