DEV Community

Cover image for How to ship static assets with your React Native library
Vince Picone
Vince Picone

Posted on

How to ship static assets with your React Native library

Background

Work on React Native is moving at an incredible. Incredible, fresh features are being shipped regularly with more and more large tech companies pitching in.

One issue with the speed of this development, however, is that things change pretty rapidly. You might find conflicting answers to a problem you run into and you'll need to suss out which one is the most modern/correct solution. This is the situation I found myself in when I was trying to find the best way to ship some font files (Plex) with the component library I'm working on (Carbon).

The Solution

  1. Create an assets (or call it whatever you like) directory with your font files/static assets the root of your project. You can also throw it under src or somewhere else, you'll just need to update the path in the next step.

  2. Also in the root of your package add a react-native.config.js file with the following:

// react-native-config.js
module.exports = {
  dependency: {
    assets: ['assets'], // or whatever your directory is
  },
};
  1. After installing your package, developers will just need to run npx react-native link my-special-package in the root of their project (replacing my-special-package with the name of your library). Update your docs to add this step accordingly.

  2. React native will update the corresponding native files in their projects and allow them to be referenced accordingly.

Wrap up/caveats

This felt really clean solution and allowed me to keep working on my package. The dependent package I used was generated using @react-native-community/cli, I haven't experimented with an expo package just yet.

I'd love to here how other folks have dealt with this or other React Native problems.

Resources

Top comments (0)