我被困在一个似乎很明显的问题上:路由器的根组件并未被订阅存储更改,即使调用了mapStateToProps也没有收到道具。根路由组件没有连接到redux
我的路线定义为普通的对象:
const createRoutes = (store) => {
path: '/',
component: RootComponent,
indexRoute: Home,
childRoutes: [
SubRoute
]
}
export default createRoutes
于是我进口创造应用程序时,这个对象:
let render =() => {
const routes = require('./routes').default(store);
ReactDOM.render(
<AppContainer store={store} routes={routes} />,
MOUNT_NODE
)
}
我RootComponent看起来是这样的:
const RootComponent = ({address, action}) => {
<div>
{console.log('addr: ' + address)}
<button onClick={action}>test</button>
</div>
}
const mapStateToProps = (state) => {
console.log('mapStateToProps');
return {address: state.address};
}
const mapDispatchToProps = (dispatch) => {
action = (evt) => {
console.log('mapDispatchToProps');
dispatch({type: 'TEST', payload: {address: 'xxx'}});
}
}
export default connect(mapStateToProps, mapDispatchToProps)(RootComponent)
当我点击按钮,调度工作,因为我可以在DevTools中看到状态已更改。 mapStateToProps和mapDispatchToProps也被调用,但我的RootComponent永远不会更新。
如果我为子路由做同样的代码,一切正常!关于为什么RootComponent没有订阅商店的任何想法?
编辑:顺便说一句,<AppContainer />
使用<Provider />
从react-redux
。该代码是这样的:
class AppContainer extends Component {
render() {
const { routes, store } = this.props
return (
<Provider store={store}>
<Router history={browserHistory} routes={routes} />
</Provider>
)
}
}
我有同样的问题。你是如何使'mapStateToProps'接收状态的? – ilyo
'mapStatetoProps'实际上正确地接收了状态。但是我错误地设置了组件的状态。并且由于组件的状态没有改变(因为它被设置不正确),组件没有被更新。 – danielnovy
回答你自己的问题没有意义,只能说“我发现了一个正确的方法来使它工作!”请提供使其工作的实际代码,这样其他人也可以从中受益。 – ilyo