reactjs - 如何将 store 作为 prop 显式传递给 "Connect()"

标签 reactjs phantomjs redux react-redux

我正在尝试测试我的 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/

相关文章:

node.js - 从本地主机发送POST请求到heroku服务器?

javascript - 无法使用 casperjs 评估和 __doPostBack 函数导航

phantomjs - 如何在casperjs/phantomjs中模拟 "press and hold"(长按)按键 Action ?

redux devtools 未捕获错误 : Actions must be plain objects. 使用自定义中间件进行异步操作

reactjs - 在 redux-saga 中使用回调时出现问题

intellij-idea - 如何在同一个编辑器中使用 PyCharm 和 WebStorm

javascript - 如何在服务器端渲染ES6方式上管理React组件?

javascript - 使用 phantomJS,等待按钮点击效果完成后再继续

javascript - ReactJS服务端渲染、setTimeout问题

javascript - 使用没有 async/await 的 Promise 时, Electron 打开对话框会挂起