How to share React Components between projects

寵の児 提交于 2021-02-08 13:55:14

问题


I built a Component that I want to use in separate projects. What is the simplest best way to reuse this Component across projects using NPM?


回答1:


You haven't indicated whether you wish to keep this component private, so I'm going to answer two ways:

If you are going to open-source your component

This is easiest: publish your component(s) as an npm package (see the docs here). Then install your package just like any other in the projects where you will be using your components.

If you wish to keep it private

You have several options here:

  1. pay for a private modules on npm (docs here). If you go this route, you can install your package just like any other.

  2. Use npm link (docs) to create a link from the project(s) to the component package located on your local storage. This works but is kind of tricky to set up. The benefit of this is that if you rebuild your component, all projects with a link to the component will see those changes immediately.

  3. Use npm install to install from your (private) Github or other repository. This works similarly to the normal npm install and is a little easier to use than npm link. The downside is that any changes to the component need to be pushed to the remote repository and then the project(s) using the component need to be updated.



来源:https://stackoverflow.com/questions/46148261/how-to-share-react-components-between-projects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!