简体   繁体   English

根路由组件未连接到Redux

[英]Root route component not connecting to redux

I'm stuck on a problem that seems to be obvious: the root component of a router is not being subscribed to store changes and it's also not receiving the props even though the mapStateToProps is called. 我陷入一个似乎很明显的问题:路由器的根组件未订阅存储更改,即使调用了mapStateToProps,它也没有接收到道具。

My routes are defined as plain objects: 我的路线被定义为普通对象:

const createRoutes = (store) => {
    path: '/',
    component: RootComponent,
    indexRoute: Home,
    childRoutes: [
        SubRoute
    ]
}
export default createRoutes

Then I import this object when creating the App: 然后在创建应用程序时导入此对象:

let render = () => {
    const routes = require('./routes').default(store);
    ReactDOM.render(
        <AppContainer store={store} routes={routes} />,
        MOUNT_NODE                                                                      
    ) 
}

My RootComponent looks like this: 我的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)

When I click the button, the dispatch works since I can see in the DevTools that the state is changed. 单击按钮后,调度工作正常,因为在DevTools中可以看到状态已更改。 The mapStateToProps and mapDispatchToProps are also invoked but my RootComponent is never updated. mapStateToProps和mapDispatchToProps也被调用,但我的RootComponent从未更新。

If I do this same code for child routes, everything works fine! 如果我对子路线使用相同的代码,则一切正常! Any ideas on why RootComponent is not being subscribed to the store? 关于为什么未订阅RootComponent的任何想法?

EDIT: By the way, <AppContainer /> is using <Provider /> from react-redux . 编辑:顺便说一句, <AppContainer />正在使用react-redux <Provider /> The code is like this: 代码是这样的:

class AppContainer extends Component {
    render () {
        const { routes, store } = this.props

        return (
           <Provider store={store}>
               <Router history={browserHistory} routes={routes} />
           </Provider>
        )
    }
}

You make no mention of using the <Provider /> component from react-redux, I'm guessing you must be defining it somewhere but in case you're not: 您没有提到使用react-redux中的<Provider />组件,我想您必须在某个地方定义它,但是如果您不这样做的话:

It is the <Provider /> that makes the store available to the connect call you make in your container component. 正是<Provider />使存储可用于您在容器组件中进行的connect调用。

Check out the docs for more info 查看文档以获取更多信息

Ok, found the problem: turned out that the code mapStateToProps was not getting the values from the state correctly (even though I have printed the state a million times and didn't realize that). 好的,发现了问题:原来代码mapStateToProps不能正确地从状态中获取值(即使我已经打印了100万次该状态并且没有意识到这一点)。 So, with no changes in the state, the component was obviously not being updated. 因此,在状态没有变化的情况下,该组件显然没有被更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM