react-redux's @connect produces the error 'connect.js:129Uncaught TypeError: Cannot read property 'store' of undefined'
react-redux's @connect produces the error 'connect.js:129Uncaught TypeError: Cannot read property 'store' of undefined'
当 运行 我的应用程序(成功与支持装饰器的 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"
}
}
这是因为您在使用提供程序的同一组件上使用 connect
。
<Provider store>
Makes the Redux store available to the connect() calls in the
component hierarchy below. Normally, you can’t use connect() without
wrapping the root component in <Provider>
.
https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store
您应该在 <Main>
组件上使用连接。
编辑:
此外,如果 App
组件是您的入口点,则不需要导出它,也不需要这样做:
var app = new App;
app.render();
。
而是使用 ReactDOM 呈现应用程序。喜欢:
ReactDOM.render(
<Provider store={store}>
<Main txt='Fire test action' action={ fireTestAction} />
</Provider>,
document.getElementById('app')
)
老问题,但我 运行 遇到了这个错误,因为我试图通过调用 new MyComponent(props)
从给定的 MyComponent: React.ComponentClass
创建组件,这不是正确的创建方式一个 React 组件。正确的方法是调用 React.createElement(MyComponent, props)
当 运行 我的应用程序(成功与支持装饰器的 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"
}
}
这是因为您在使用提供程序的同一组件上使用 connect
。
<Provider store>
Makes the Redux store available to the connect() calls in the component hierarchy below. Normally, you can’t use connect() without wrapping the root component in
<Provider>
.
https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store
您应该在 <Main>
组件上使用连接。
编辑:
此外,如果 App
组件是您的入口点,则不需要导出它,也不需要这样做:
var app = new App;
app.render();
。
而是使用 ReactDOM 呈现应用程序。喜欢:
ReactDOM.render(
<Provider store={store}>
<Main txt='Fire test action' action={ fireTestAction} />
</Provider>,
document.getElementById('app')
)
老问题,但我 运行 遇到了这个错误,因为我试图通过调用 new MyComponent(props)
从给定的 MyComponent: React.ComponentClass
创建组件,这不是正确的创建方式一个 React 组件。正确的方法是调用 React.createElement(MyComponent, props)