2

I'm studying React, and would like to know how best way to create a private component to reuse it in other projects.

Thanks!

1

1 Answer 1

3

Probably npm private package is what you are looking for. Then, you can just install your package by npm install and use components from it everywhere you want. However, this is paid feature of npm.

The alternative is an npm proxy registry. For example Verdaccio allows something like that. Similiar solution to the previous one, but you have to configure the registry yourself and use it in all your projects instead of default npm registry.

You can also just publish a public npm package if it's ok for you that it'll be available for everyone. It's probably the easiest solution for starters.

Alternatively, what we used in our project, were Git submodules. You place your shared components into a separate Git repository. Then, you link that as a submodule in you main project and you can use your components just like they were a part of your project. If you use Git, it might be the easiest option for you.

And finally, if you treat you projects as playground, you always can use copy-paste approach :) I don't recommend it in any commercial projects as maintaining the code in many places is pain in the neck.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.