React-redux 连接方法无法在道具中找到商店
React-redux connect method unable to locate store in props
我有一个主要模仿 redux real-world 示例的应用程序结构,但无法克服此错误:
Uncaught Error: Invariant Violation: Could not find "store" in either the context or props of "Connect(Home)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Home)".
以下是我的应用程序结构:
渲染
import { render, createFactory } from 'react'
import { Provider } from 'react-redux';
import router from './router/router'
import store from './flux/store'
window.onload = () => {
render(createFactory(Provider)({store: store},
() => router
), document.body)
};
路由器
import { _ } from 'factories'
import { Router, IndexRoute, Route } from 'react-router'
import history from 'history/lib/createBrowserHistory'
import Home from '../components/home/home'
let route = _(Route),
index = _(IndexRoute);
let router = _(Router)({history: history()},
route({path: 'home', component: Home})
);
export default router;
主页组件
import React, { PropTypes } from 'react'
import { div } from 'factories'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux';
import * as users from '../../flux/actions/users'
class Home extends React.Component {
constructor() {
super();
this.state = {}
}
componentWillMount() {
console.log(this.props.users.user)
}
componentWillUnmount() {
}
render() {
return (
div({}, "Home")
)
}
}
export default connect(
state => {
return {
users: state.users
}
},
dispatch => bindActionCreators(users, dispatch)
)(Home)
这导致出现上述错误。如您所见,我以与 redux 示例中所示相同的方式传递商店。
在 redux troubleshooting docs 中找到了解决方案。我需要让我的 react-router return 一个创建实际路由器的函数:
import { _ } from 'factories'
import { Router, IndexRoute, Route } from 'react-router'
import history from 'history/lib/createBrowserHistory'
import Home from '../components/home/home'
let route = _(Route),
index = _(IndexRoute);
const router = () =>
_(Router)({history: history()},
route({path: 'home', component: Home})
);
export default router;
我有一个主要模仿 redux real-world 示例的应用程序结构,但无法克服此错误:
Uncaught Error: Invariant Violation: Could not find "store" in either the context or props of "Connect(Home)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Home)".
以下是我的应用程序结构:
渲染
import { render, createFactory } from 'react'
import { Provider } from 'react-redux';
import router from './router/router'
import store from './flux/store'
window.onload = () => {
render(createFactory(Provider)({store: store},
() => router
), document.body)
};
路由器
import { _ } from 'factories'
import { Router, IndexRoute, Route } from 'react-router'
import history from 'history/lib/createBrowserHistory'
import Home from '../components/home/home'
let route = _(Route),
index = _(IndexRoute);
let router = _(Router)({history: history()},
route({path: 'home', component: Home})
);
export default router;
主页组件
import React, { PropTypes } from 'react'
import { div } from 'factories'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux';
import * as users from '../../flux/actions/users'
class Home extends React.Component {
constructor() {
super();
this.state = {}
}
componentWillMount() {
console.log(this.props.users.user)
}
componentWillUnmount() {
}
render() {
return (
div({}, "Home")
)
}
}
export default connect(
state => {
return {
users: state.users
}
},
dispatch => bindActionCreators(users, dispatch)
)(Home)
这导致出现上述错误。如您所见,我以与 redux 示例中所示相同的方式传递商店。
在 redux troubleshooting docs 中找到了解决方案。我需要让我的 react-router return 一个创建实际路由器的函数:
import { _ } from 'factories'
import { Router, IndexRoute, Route } from 'react-router'
import history from 'history/lib/createBrowserHistory'
import Home from '../components/home/home'
let route = _(Route),
index = _(IndexRoute);
const router = () =>
_(Router)({history: history()},
route({path: 'home', component: Home})
);
export default router;