简体   繁体   English

如何在多个 react-native 项目中复用代码?

[英]How to re-use code in multiple react-native projects?

I have so many react-native projects and most of them required the same logic in many parts of the app.我有这么多 react-native 项目,其中大多数项目在应用程序的许多部分都需要相同的逻辑。 like the same hooks, same util functions, and identical components.就像相同的钩子、相同的 util 函数和相同的组件。

I am copying and pasting all the code to other projects.我正在将所有代码复制并粘贴到其他项目。 Ya I know that's a bad approach.是的,我知道这是一个糟糕的方法。 If I change some code in some hooks then I have to update all the projects with it.如果我更改某些挂钩中的某些代码,那么我必须用它更新所有项目。 and that's very bug prone这很容易出错

I don't know much yet about how to solve this problem, but I did some search and I found that我对如何解决这个问题还不太了解,但我做了一些搜索,我发现

  1. I can try to create a JS library and a git submodule, and use them in all projects.我可以尝试创建一个 JS 库和一个 git 子模块,并在所有项目中使用它们。
  2. build JS library and publish it to npm.构建JS库发布到npm。

I am not sure how react-native would work for both of these approaches.我不确定 react-native 如何适用于这两种方法。

Will I need to create react native module?我需要创建 React Native 模块吗? or do something else?还是做点别的? If you have some resources to read please attach some as I couldn't find much on google.如果您有一些资源可供阅读,请附上一些资源,因为我在谷歌上找不到太多。

Before segmenting your code, make sure you distinguish your business logic and other Logic reduce their dependencies.在分割你的代码之前,确保你区分你的业务逻辑和其他逻辑减少它们的依赖。

Here Approach can vary depending on your use case,这里的方法可以根据您的用例而有所不同,

Firstly about the tooling you need to use,首先关于你需要使用的工具,

Git submodules Git 个子模块

Git submodules has a steep learning curve, would be recommended using one git submodule, for all shared code, instead of several submodules as their were many issue with git submodules like dependency relationships between the modules, pull in the parent doesn't pull child code etc and many more Git 子模块有一个陡峭的学习曲线,建议使用一个 git 子模块,用于所有共享代码,而不是几个子模块,因为它们与 git 子模块有很多问题,例如模块之间的依赖关系,拉入父模块不会拉取子代码等等等等

If you feel all your code can fit into a submodule without issues with dependencies, you can try this approach如果你觉得你的所有代码都可以放入一个子模块而没有依赖问题,你可以尝试这种方法

you can find more details here npm vs git submodules您可以在此处找到更多详细信息npm 与 git 子模块

Coming to other approaches, out goal was to create the reusable components or code.对于其他方法,我们的目标是创建可重用的组件或代码。 your code as components and others您的代码作为组件和其他

For sharable Components you can use different approach's like:-对于可共享组件,您可以使用不同的方法,例如:-

Bit - for components钻头 - 用于组件

bit - it works with react native too bit - 它也适用于 React Native

NPM NPM

react-native-builder-bob react-native-builder-bob helps you create react-native npm packages on both native and javascript side too.You can create you components or native code as package to cater to your projects, if your hooks contain specific code related with react native you can go with the builder-bob react-native-builder-bob react-native-builder-bob帮助您在本机和 javascript 端创建 react-native npm 包。如果您的挂钩包含特定代码,您可以创建组件或本机代码作为 package 以满足您的项目与本机反应相关,您可以使用 builder-bob go

For Hooks and utils you can create npm packages directly or you can also use react-native-builder-bob but that would be a overhead as it will include react-native,对于Hooksutils ,您可以直接创建 npm 包,或者您也可以使用 react-native-builder-bob,但这将是一个开销,因为它将包含 react-native,

Choose wisely how do many package/s needed because your components or utils can be like a collection or individual working set, depending on the functionally as all code can fit in single package or many.明智地选择需要多少个包,因为您的组件或实用程序可以像一个集合或单个工作集,这取决于功能,因为所有代码都可以放在单个 package 或多个中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM