What is react-native-web?
The react-native-web package allows you to use React Native components and APIs to build web applications. It provides a way to write a single codebase that can run on both mobile and web platforms, leveraging the same React Native components.
What are react-native-web's main functionalities?
Cross-Platform Components
This feature allows you to use React Native components like View and Text to build web applications. The code sample demonstrates a simple app that displays 'Hello, world!' using React Native components.
import { View, Text } from 'react-native';
import { AppRegistry } from 'react-native-web';
const App = () => (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Hello, world!</Text>
</View>
);
AppRegistry.registerComponent('App', () => App);
AppRegistry.runApplication('App', { initialProps: {}, rootTag: document.getElementById('app') });
StyleSheet for Web
This feature allows you to use the StyleSheet API from React Native to style your web components. The code sample shows how to create and apply styles using StyleSheet.
import { StyleSheet, View, Text } from 'react-native';
import { AppRegistry } from 'react-native-web';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
const App = () => (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native Web!</Text>
</View>
);
AppRegistry.registerComponent('App', () => App);
AppRegistry.runApplication('App', { initialProps: {}, rootTag: document.getElementById('app') });
Event Handling
This feature allows you to handle events such as button presses using React Native's event handling system. The code sample demonstrates a button that shows an alert when pressed.
import { View, Button, Alert } from 'react-native';
import { AppRegistry } from 'react-native-web';
const App = () => (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button
title="Press me"
onPress={() => Alert.alert('Button pressed!')}
/>
</View>
);
AppRegistry.registerComponent('App', () => App);
AppRegistry.runApplication('App', { initialProps: {}, rootTag: document.getElementById('app') });
Other packages similar to react-native-web
react-native-dom
react-native-dom is another package that aims to bring React Native to the web. It provides a similar set of components and APIs as react-native-web but focuses more on integrating with the DOM directly. It is less mature and has a smaller community compared to react-native-web.
react-primitives
react-primitives is a package that provides a set of primitive components that work across multiple platforms, including web, iOS, and Android. It is more lightweight compared to react-native-web and focuses on providing a minimal set of components for cross-platform development.
reactxp
reactxp (React Cross-Platform) is a library developed by Microsoft that allows you to build cross-platform apps using React and React Native. It provides a unified API for building apps that run on web, iOS, Android, and Windows. It is more opinionated and comes with its own set of components and APIs.
React Native for Web

React Native components and APIs for the Web.
Browser support: Chrome, Firefox, Safari >= 7, IE 10, Edge.
Overview
"React Native for Web" is a project to bring React Native's building blocks and
touch handling to the Web. Read more.
Browse the UI Explorer to see React Native examples running on
Web. Or try it out
online with React Native for Web: Playground.
Quick start
To install in your app:
npm install --save react react-native-web
Read the Client and Server rendering guide.
Alternatively, you can quickly setup a local project
using create-react-app
(which supports react-native-web
out-of-the-box once installed) and
react-native-web-starter.
Documentation
Guides:
Exported modules:
Why?
There are many different teams at Twitter building web applications with React.
We want to share React components, libraries, and APIs between teams…much like
the OSS community tries to do. At our scale, this involves dealing with
multiple, inter-related problems including: a common way to handle style,
animation, touch, viewport adaptation, accessibility, themes, RTL layout, and
server-rendering.
This is hard to do with React DOM, as the components are essentially the same
low-level building blocks that the browser provides. However, React Native
avoids, solves, or can solve almost all these problems facing Web teams.
Central to this is React Native's JavaScript style API (not strictly
"CSS-in-JS") which avoids the key problems with
CSS by giving up some of the
complexity of CSS.
Example code
import React from 'react'
import { AppRegistry, Image, StyleSheet, Text, View } from 'react-native'
const Card = ({ children }) => <View style={styles.card}>{children}</View>
const Title = ({ children }) => <Text style={styles.title}>{children}</Text>
const Photo = ({ uri }) => <Image source={{ uri }} style={styles.image} />
const App = () => (
<Card>
<Title>App Card</Title>
<Photo uri="/some-photo.jpg" />
</Card>
)
const styles = StyleSheet.create({
card: {
flexGrow: 1,
justifyContent: 'center'
},
title: {
fontSize: '1.25rem',
fontWeight: 'bold'
},
image: {
height: 40,
marginVertical: 10,
width: 40
}
})
AppRegistry.registerComponent('MyApp', () => App)
AppRegistry.runApplication('MyApp', { rootTag: document.getElementById('react-root') })
Related projects
License
React Native for Web is BSD licensed.