Skip to main content

Libraries

A React Native library runs here when it has a web build, or when it's plain React Native code. Your setup sends each react-native import inside the library to this package, and loads its .web.js files: withRootNative on Expo, or the Vite plugin. Some libraries need one more step.

LibraryWhat it needs
react-native-reanimated, and libraries on itOn Expo, nothing. On Vite, the worklets option of the plugin.
react-native-gesture-handlerWhat Reanimated needs, if your app uses it. Its Pressable needs 0.0.13 or later. See Gesture handler.
@react-native-vector-icons/*Nothing with the plugin. See Icon fonts.
react-native-svgNothing.
react-native-safe-area-contextNothing.
@react-native-async-storage/async-storageNothing.

A library with only native code, and no web build, doesn't run in a browser.

Reanimated​

Reanimated needs the Babel plugin of react-native-worklets. On Expo, the Babel preset of your app runs it, so you don't set anything.

On Vite, set the option of the plugin:

vite.config.ts
export default defineConfig({
plugins: [react(), rootnative({ worklets: true })],
})

A library built on Reanimated needs the same option. See worklets.

Gesture handler​

react-native-gesture-handler 3 runs on its own web build. Wrap your app in GestureHandlerRootView, as on native. These parts work, on Expo and on Vite, in Chrome, Firefox, and Safari:

  • The gesture hooks, such as usePanGesture, useTapGesture, and useLongPressGesture, with JavaScript callbacks or with Reanimated.
  • The Gesture builder, such as Gesture.Pan(), with GestureDetector.
  • RectButton and Pressable.
  • ReanimatedSwipeable, and the FlatList of the library.

Use 0.0.13 or later. In an earlier version, the Pressable of the library doesn't press: its button looks for role="button" on the element, and the package didn't write that attribute.

Icon fonts​

An icon set of @react-native-vector-icons renders each icon as a character of its font. The package loads the font file only through Expo. With the Vite plugin, you don't need Expo: the plugin adds the font of each icon set to the page when your app imports the set. See Icon fonts on the plugin page.

Without the plugin, give the browser the font yourself, or each icon is an empty box. Add it once, in the file that mounts your app:

src/main.tsx
import fontUrl from '@react-native-vector-icons/material-design-icons/fonts/MaterialDesignIcons.ttf?url'

const iconFont = new FontFace('MaterialDesignIcons', `url(${fontUrl})`)
document.fonts.add(iconFont)
void iconFont.load()

The family name must be the name that the icon set uses for its font. For Material Design Icons that's MaterialDesignIcons. For another set, read the postScriptName in the entry file of its package. The font file is in the fonts folder of the package.

TypeScript needs the types of Vite for the ?url import. Add "types": ["vite/client"] to the compilerOptions of your tsconfig.json.

Types of a library​

The type files of a native library import types from the real react-native package. Its global types don't fit a browser program, so a type check of those files reports errors such as Duplicate identifier 'FormData'.

Let TypeScript skip the type files of your dependencies:

tsconfig.json
{
"compilerOptions": {
"skipLibCheck": true
}
}

Your own code is still checked in full.

A library can add a prop to a component with declare module 'react-native'. The props types here are interfaces, as in React Native, so the new prop merges with them.