0

Suppose I want to share some common data object or utility classes among my React projects (locally only). For example, a utility class that performs complex string or data structures manipulation.

Which would be the best way of doing it? Should I create a separated NPM module for the library, and link it to each project?

I have seen some solutions that use create-app-rewired, but they require some custom getBabelLoader stuff and it seems too much for such a simple use case.

Timbergus
  • 3,167
  • 2
  • 36
  • 35
corsair
  • 347
  • 3
  • 13

1 Answers1

1

You can setup Yarn workspaces & have scoped package names like @project/libraryA or @project/libraryB. You can then import using import { someFunc } from '@project/libraryB' in @project/libraryA

You'll want to export all the functions you want to share in your package.json main field, which usually points to index.js.

I would do more research on monorepos.

A great tool I found recently is https://nx.dev. It is a Monorepo build tool similar to the one found in Angular CLI, but this works for React as well.

Webber
  • 941
  • 11
  • 26