简体   繁体   English

在同一回购中的两个React项目之间共享组件

[英]Sharing components between two react projects in same repo

I have two projects in the same repo that I'd like to be able to share components. 我希望在同一回购中有两个项目可以共享组件。 Ideally, under the root folder, I'd like to have a separate folder for my first project, containing all node modules, ios files, etc, and a separate similar folder for my second project. 理想情况下,在根文件夹下,我想为我的第一个项目创建一个单独的文件夹,其中包含所有节点模块,ios文件等,并为我的第二个项目创建一个单独的类似文件夹。 Then I'd like to create a 'shared' folder on the same level, containing only the components I am sharing between the two. 然后,我想在同一级别上创建一个“共享”文件夹,仅包含我在两者之间共享的组件。 So ideally, all three of those folders will be at the same level, under the root folder. 因此,理想情况下,这三个文件夹都将位于根文件夹下的同一级别。 When trying to import a component from the 'shared' folder within my project, it first told me it needed a package.json. 当尝试从项目中的“共享”文件夹中导入组件时,它首先告诉我它需要package.json。 After I created that, it gives me the error "unable to resolve [folderName] as a folder: it contained a package, but its 'main' could not be resolved". 创建完该文件后,它给我错误“无法将[folderName]解析为文件夹:它包含一个包,但无法解析其'main'”。

Has anyone had any experience with this? 有没有人有这方面的经验? If I want my two projects to be able to access components within the shared folder, how should I go about structuring the folders? 如果我希望我的两个项目能够访问共享文件夹中的组件,那么应该如何构造文件夹?

react-native look for package.json only if you import from a folder . 仅当您从folder导入时,react-native才寻找package.json

import from a file instead. 而是从文件导入。

example: 例:

import { Button } from './shared/button/mybuttonjs';

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

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