【问题标题】:React-Redux migration from 5.x to 7.x broke `connect`React-Redux 从 5.x 迁移到 7.x 破坏了 `connect`
【发布时间】:2021-09-17 20:18:47
【问题描述】:

我正在尝试从 react-redux (https://www.npmjs.com/package/react-redux) 从 5.x 升级到 7.x,以使用钩子并通过 connect() 函数实现了一些重大更改。它一直给我一个错误,我必须将我的应用程序包装在我已经拥有的提供程序中。

错误:

Uncaught (in promise) Invariant Violation: Could not find "store" in either the context or props of "Connect(DateRangeSelector)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(DateRangeSelector)".

提供者代码:

import React from 'react';
import { Provider } from 'react-redux';

import store from 'scripts/setup';
import FullReactComponent from 'root/FullReactComponent';


const App = () => (
  <Provider store={store}>
      <FullReactComponent />
  </Provider>
);

export default App;

连接代码:

FullReactRootComponent.propTypes = {
  bgColor: PropTypes.string.isRequired,
};

export const mapStateToProps = ({ portal }) => ({
  bgColor: portal.get('bgColor'),
});

export const mapDispatchToProps = {
  fetchEnabledRoutes: actions.fetchEnabledRoutes,
};

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(FullReactRootComponent);

有没有人在升级时也遇到过这个问题?连接 API 有变化吗?

编辑:这是一个在两个快照之间未正确调用 Connect 的示例:

5.x:

 <Connect(ModalComponent) open={true}>
   <ModalAdd /> 
</Connect(ModalComponent)>

7.x:

<ModalComponent
  open={true}
/>

【问题讨论】:

  • 但是代码对我来说看起来不错,损坏的部分应该在其他地方。
  • 是的,我阅读了发行说明,从 5.x 开始,它似乎没有中断,因为 6.x 中主要的 thrash 删除了移动商店的能力。
  • 您是否不小心在应用程序中拥有多个版本的 React-Redux?
  • 当@markerikson 来调试你的redux 问题时,你知道你会得到很好的建议。

标签: javascript reactjs redux react-redux


【解决方案1】:

从@markerikson 得知,我的yarn.lock 中有react-redux 的重复副本。为了解决这个问题,我将 react-redux 放在 package.json 中的 resolutions 中,并强制任何共享库为 7.x。

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    相关资源
    最近更新 更多