我正在尝试测试我的 React 组件并收到以下错误。
Invariant Violation: Could not find "store" in either the context or props of "Connect()". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect()".
在测试中渲染组件时出现错误。
beforeEach(() => {
Component = TestUtils.renderIntoDocument(<SideMenu />);
});
在页面上渲染组件时效果很好。但是在测试中,我无法将存储显式传递到组件中。
有人能指出正确的方向吗?
最佳答案
要回答这个问题(我遇到了这个问题,接受的答案不是我需要的),创建一个新方法,例如:
function connectWithStore(store, WrappedComponent, ...args) {
let ConnectedWrappedComponent = connect(...args)(WrappedComponent)
return function (props) {
return <ConnectedWrappedComponent {...props} store={store} />
}
}
然后,要连接,请使用:
const ConnectedApp = connectWithStore(store, App, mapStateToProps, mapDispatchToProps,)
export default ConnectedApp;
请参见此处:https://github.com/reactjs/react-redux/issues/390#issuecomment-221389608
关于reactjs - 如何将 store 作为 prop 显式传递给 "Connect()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707780/