分享在monorepo内的工作区或库之间的redux状态。

huangapple go评论77阅读模式
英文:

Share redux state among workspaces or libraries inside a monorepo

问题

我最近很难理解如何在共享应用程序和库之间管理一些逻辑,特别是在主应用程序中设置了redux,并且需要在共享UI组件中使用它。

简而言之:

我有一个react应用程序和一个共享UI库NX monorepo中(但它是基于包的,所以基本上是一个npm monorepo):

Nx-project/
├── apps/
│   └── react-app/
│       └── redux-store.js
└── libs/
    └── shared-ui/
        └── component-that-use-redux

我需要在我的共享UI组件中使用redux hooks(来自react-app)。
我应该如何进行,这里有什么最佳实践吗?

我应该简单地从应用程序中导入吗?
这不会在编译共享UI库时引起冲突问题吗?

提前感谢您能给我的任何见解。

英文:

I’m recently having a hard time to understand how to manage some logic between shared apps and libs, in particular with redux setted up in the main app and the need to use it on the shared-ui components.

On a nutshell:

I have a react-app and a shared UI library on an NX monorepo (but it's package-based so it's basically an npm monorepo):

Nx-project/
├── apps/
│   └── react-app/
│       └── redux-store.js
└── libs/
    └── shared-ui/
        └── component-that-use-redux

I need to use redux hooks (from react-app) inside my shared UI component.
How should I proceed, what's the best practice here?

Should I simply import from the app?
Doesn’t that represent a conflictual issue when it comes to compile the shared ui lib?

Thanks in advance for any insight you can give me.

答案1

得分: 1

首先,不要从应用程序导入到库中。为您的操作、reducers、类型等创建另一个NX库,并将它们也导入到您的应用程序中。您可以在您的UI库中使用Redux包的钩子(即从redux导入)。我不确定您正在使用的Redux风格是什么,但基本上您可以在应用程序中使用官方Redux钩子以及来自您单独的NX库的reducers、操作和类型。尽量将尽可能少的代码放入您的应用程序中,保持您的NX库小而一切应该就位。

英文:

Firstly, don't import from the app to the lib. Create another NX library for your actions, reducers, types, etc, and import them into your app as well. You can use the Redux package hooks in your UI library (i.e. import from redux). I don't know exactly what the flavour of Redux is that you are using, but basically you can use the official Redux hooks, and reducers, actions and types from your separate NX library inside your app. Try to put as little code as possible in your app and keep your NX libraries small and everything should fit in to place.

huangapple
  • 本文由 发表于 2023年7月6日 16:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定