【问题标题】:Can I use react-redux to implement third party npm package for apps which are not using react-redux?我可以使用 react-redux 为不使用 react-redux 的应用程序实现第三方 npm 包吗?
【发布时间】:2021-04-20 05:30:49
【问题描述】:

我有一个使用 redux 实现的 react 应用程序。在使用 redux 之前,我能够成功地将其发布为 npm 包库,并将其用作另一个 react 应用程序中的第三方组件。 然后我使用 redux 来实现并在更改版本后通过简单地运行“npm publish”再次发布它。我能够成功发布它。 但是当我在另一个没有使用 redux 的 react 应用程序中将它用作第三方组件时,它给了我一个错误。

有没有办法发布一个使用 react-redux 的包,以用作不使用 redux 的 react 应用程序中的第三方组件?

Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>
▶ 21 stack frames were collapsed.
Module.<anonymous>
D:/test/my-app/src/index.js:7
   4 | import App from './App';
   5 | import reportWebVitals from './reportWebVitals';
   6 | 
>  7 | ReactDOM.render(
   8 |   <React.StrictMode>
   9 |     <App />
  10 |   </React.StrictMode>,

【问题讨论】:

  • 您找到解决方法了吗?我想将 React-Redux 添加到 notistack 但作为您的问题状态,也让不做 Redux 的用户能够使用我的 PR

标签: javascript reactjs redux react-redux npm-package


【解决方案1】:

您可以使用custom context 来防止您的包的redux 存储与可能已经存在的redux 存储发生冲突。在这种情况下,您将导出您自己的 Provider 组件,该组件创建并向下传递了一个 redux 存储,并且您的包的使用者需要将该 Provider 包装在他们的应用程序周围。

【讨论】:

  • 谢谢 你能显示一些教程的链接吗?有什么方法可以发布一个使用 react-redux 但不将其自身导入 package.json 的包库?
  • @Erik 我不知道有任何关于这方面的教程,抱歉。您可以将 react-redux 作为 peerDependency,但至少这是必要的。
猜你喜欢
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 2017-08-06
  • 2018-06-18
  • 2020-06-06
  • 2017-10-16
相关资源
最近更新 更多