Expo
@rootnative/web-expo runs an Expo app on @rootnative/web in place of react-native-web. You keep your Expo setup: the dev server, expo export, and the Babel preset.
Install
Install the package, and remove react-native-web:
npm install @rootnative/web-expo
npm uninstall react-native-web
pnpm remove react-native-web keeps react-native-web and its dependencies in pnpm-lock.yaml and node_modules, because expo lists it as an optional peer. The bundle doesn't use it. To remove it, add this override and run pnpm install:
overrides:
react-native-web: '-'
After that install, you can keep the override or remove it. pnpm doesn't add react-native-web back.
Wrap your Metro config with withRootNative. If your app has no metro.config.js, add this one:
const { getDefaultConfig } = require('expo/metro-config')
const { withRootNative } = require('@rootnative/web-expo/metro.cjs')
module.exports = withRootNative(getDefaultConfig(__dirname))
On the web, withRootNative sends each import of react-native and of react-native-web to the package. On iOS and Android, it changes nothing. Restart the dev server with --clear after the change:
npx expo start --clear
Keep react-dom in the dependencies of your package.json. Without it, Expo stops with an error. With it, Expo prints this warning on each start or export of the web target:
react-native-web is not installed. Some React Native components may not work on web without it. Install it with: npx expo install react-native-web
The warning doesn't stop the dev server or expo export. Don't install react-native-web: the bundle doesn't use it. With the install without a Metro config, Expo doesn't warn.
Your code doesn't change, and TypeScript keeps the types of the react-native package. Some web-only code that you wrote for react-native-web isn't necessary now: see From react-native-web.
Without a Metro config
You can also install the package under the name react-native-web, with an npm alias. Then the app needs no withRootNative:
npm install react-native-web@npm:@rootnative/web-expo
An Expo app with web support lists react-native-web already, and the alias takes its place.
Versions
The package depends on the same version of @rootnative/web, and works only with that version. If your app imports @rootnative/web itself, for example Head from @rootnative/web/dom, install the version that the package names.
Web-only props in TypeScript
The react-native types don't declare the web-only props of this package, for example loading and fetchPriority on Image. Add the props that you use in a declaration file, in a folder that your tsconfig.json includes:
import 'react-native'
declare module 'react-native' {
interface ImageProps {
loading?: 'eager' | 'lazy'
fetchPriority?: 'high' | 'low' | 'auto'
}
}
The props types of React Native are interfaces, so your props merge with them. Keep the import line. Without it, the file replaces the react-native types, and TypeScript reports errors across your app.
How it works
On the web, Expo's bundler rewrites import { View } from 'react-native' to an import of react-native-web/dist/exports/View. withRootNative, or the alias, sends that path to the package. The package has a file at each of these paths, and each file loads only its own module of @rootnative/web.
These imports load the root of the package, and with it all of @rootnative/web:
- A namespace import, such as
import * as RN from 'react-native', or a default import. - A name that react-native-web doesn't have, such as
useAnimatedValue. require('react-native').
Expo Router uses require('react-native'). So with Expo Router, the bundle has all of @rootnative/web, also the components that your app doesn't use.
Expo also calls AppRegistry from react-native-web to start the app, and asks it for the reset styles of the page. The package gives both: your app renders with React DOM, and body gets no margin, as with react-native-web.
Expo's libraries import UIManager, NativeModules, LogBox, DeviceEventEmitter, NativeEventEmitter, and AssetRegistry on the web too. The package gives them as react-native-web does: NativeModules has no native module, LogBox does nothing, UIManager measures an element with the host methods of @rootnative/web, only JavaScript emits events on the two emitters, and AssetRegistry is the asset registry of @rootnative/web.
What works
| Part | Behavior |
|---|---|
| Components and APIs | Each one that @rootnative/web has. See Status. |
| Expo Router | Tabs, a stack, links, the back button, and a URL that opens a screen directly. |
| The dev server | Works. |
expo export --platform web | Works with web.output: 'single', the default, and with 'static': Expo Router renders each route on the server, and the page hydrates. |
| Reanimated 4 | Animates the components. Expo's Babel preset runs the Worklets plugin, so the app needs no setup. |
| Image imports | require('./logo.png') loads the image. On the web, Expo gives the import a uri, a width, and a height, and Image reads them. |
react-native-svg | Renders. The package has the assets registry that it loads on the web. |
expo-system-ui | Sets and reads the background color of the page. |
@expo/html-elements doesn't work with the package. It imports internal files of react-native-web's View, which the package doesn't have, so the bundle fails. Give View and Text a role instead: see Semantic HTML.
Names that load as stubs
A name that Expo's bundler asks for and @rootnative/web doesn't have loads as a stub. The import works, so a library that imports the name and doesn't use it still loads. A use throws an error that names it, such as Picker is not in @rootnative/web yet.
These names aren't in React Native 0.88: CheckBox, Picker, ProgressBar, and the others come from react-native-web, and React Native removed InteractionManager. The file expo-exports.json of the package lists all of them under missing: node_modules/@rootnative/web-expo/expo-exports.json, or node_modules/react-native-web/expo-exports.json with the alias.
Tests
On the web, Expo's Babel preset rewrites each import of react-native to a file of react-native-web, such as react-native-web/dist/exports/View. The jest-expo/web preset also maps react-native to react-native-web. Without react-native-web, Jest can't find these files. If you keep react-native-web for Jest, your web tests run on react-native-web, and not on the runtime that your app ships.
So map react-native and react-native-web to the package. Also let Babel transform @rootnative, because its packages are ES modules:
module.exports = {
preset: 'jest-expo/web',
moduleNameMapper: {
'^react-native$': '@rootnative/web-expo',
'^react-native-web$': '@rootnative/web-expo',
'^react-native-web/(.*)$': '@rootnative/web-expo/$1',
},
transformIgnorePatterns: [
'/node_modules/(?!(.pnpm|react-native|@react-native|@react-native-community|expo|@expo|@expo-google-fonts|react-navigation|@react-navigation|@sentry/react-native|native-base|standard-navigation|@rootnative))',
'/node_modules/react-native-reanimated/plugin/',
'/node_modules/@react-native/babel-preset/',
],
}
The two react-native-web lines are for libraries that import react-native-web, such as expo-system-ui. transformIgnorePatterns is the list of jest-expo 58 with @rootnative added. If your config has its own list, add @rootnative to it.
Jest runs your tests in jsdom, which has no layout:
- Each size is 0.
onLayoutdoesn't fire, andonContentSizeChangefires one time, on mount.- A scroll does nothing, for example
scrollToon a ref.
Test layout and scroll in a browser.