首页 > 解决方案 > 在“连接(组件)”的上下文中找不到“商店”,尽管传递了提供者和上下文

问题描述

在 React 应用程序中使用 Redux 时,许多其他问题Could not find "store" in the context of...通过提醒应该将容器组件包装在提供程序或根组件中,或者将特定上下文作为选项传递给connect. 我正在做所有这一切,它仅在我单独包装容器组件时才有效......而我希望提供者包装根组件。问题是什么?

版本

react: 16.13.1
redux: 4.0.5
react-redux: 7.2.0

索引.jsx

const store = createStore(reducer, state);
const context = React.createContext(undefined);
render(
    <Provider store={store} context={context}>
        <App />
    </Provider>,
    document.getElementById("app")
);

ContainerComponent.jsx呈现在App组件内部的某处。

export const ContainerComponent = connect(
    mapStateToProps,
    mapDispatchToProps,
    null,
    { context }
)(MyComponent);

错误

Uncaught Error: Could not find "store" in the context 
of "Connect(MyComponent)". Either wrap the root component
in a <Provider>, or pass a custom React context provider
to <Provider> and the corresponding React context consumer
to Connect(MyComponent) in connect options.

=> 这一切都完成了!

标签: reduxreact-redux

解决方案


The custom context parameters are only intended for very rare use cases. You shouldn't use it normally.

Remove that code.


推荐阅读