Adding a Library with Native Code

+15 Mana ✨

A library that includes native code needs more than npm install. Its native part has to be linked into your iOS and Android projects, and the app binary rebuilt before you can use the library. If you first check that the library works with React Native, with your platforms and with your React Native version, you spend that rebuild only on libraries that will run.

Key terms

  • Linking: connecting a library's native code to your app's iOS and Android projects so the app can use it.
  • CocoaPods: the tool React Native uses to manage iOS project dependencies, run through pod install.
  • Gradle: what React Native uses to manage Android project dependencies.
  • React Native Directory: a searchable database of libraries built for React Native, which can filter by platform.

Linking on iOS and Android

The install step is the same as for any other package. npm install react-native-webview or yarn add react-native-webview fetches the library from the npm registry, and the platform steps below take over from that point.

Most React Native libraries follow React Native's convention of using CocoaPods for iOS. Running pod install in the ios directory links the library to your native iOS project, and npx pod-install does the same from the project root.

The app template runs CocoaPods through Ruby's Bundler instead. The first time you create a project, bundle install installs CocoaPods itself. After that, run bundle exec pod install on first clone and every time your native dependencies change, then rebuild the iOS binary with npm run ios.

On Android, React Native manages dependencies with Gradle. After you install a library with native dependencies, rebuild the app binary with npm run android.

The commands below the lesson cover the whole sequence, with a placeholder for the package name. A library that doesn't follow the CocoaPods convention documents its own steps in its README, and those are the ones to follow.

Checking that a library runs on React Native

The npm registry lists libraries for many JavaScript environments, and not all of them work in React Native. Libraries built for another platform usually don't. react-select is built for the web and targets react-dom, and rimraf is built for Node.js and works with your computer's file system. Libraries that only use JavaScript language features, such as lodash, work in any environment. If you still can't tell whether a library works in React Native, the easiest way to find out is to install it and try it. If it doesn't work, remove it with npm uninstall.

Start your search on React Native Directory, which lists libraries built for React Native and filters them by platform: iOS, Android, Web and Windows. For a library that isn't listed there, check its README for what it supports. Support differs between projects even within one group:

  • React Native Community libraries often support iOS, tvOS, Android and Windows, but this varies across projects.
  • Libraries built by Expo are written in TypeScript and support iOS, Android and react-native-web wherever possible.

Matching your React Native version

The latest version of a library is typically compatible with the latest React Native. On an older React Native version, look in the library's README for the version to install, then install it with npm install <library-name>@<version-number>. A library with native code then goes through the same linking and rebuild steps as any other.

Code Examples

bash
# From the project root: install the package from npm
npm install <library-name>

# On an older React Native version, install the release its README names
npm install <library-name>@<version-number>

# iOS, first time only: install CocoaPods through Bundler
bundle install

# iOS: link the native code, on first clone and after every native dependency change
cd ios && bundle exec pod install && cd ..

# Rebuild each app binary so it contains the library's native code
npm run ios
npm run android
✓ Completed