Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
react-native-web
Advanced tools
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.
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') });
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 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 (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 components and APIs for the Web. ~17.7 KB minified and gzipped.
npm install --save react react-dom react-native-web
React Native for Web exports its components and a reference to the React
installation. Styles are defined with, and used as JavaScript objects.
Component:
import React, { Image, StyleSheet, Text, View } from 'react-native-web'
const Title = ({ children }) => <Text style={styles.title}>{children}</Text>
const Summary = ({ children }) => (
<View style={styles.text}>
<Text style={styles.subtitle}>{children}</Text>
</View>
)
class App extends React.Component {
render() {
return (
<View style={styles.row}>
<Image
source={{ uri: 'http://facebook.github.io/react/img/logo_og.png' }}
style={styles.image}
/>
<Title>React Native Web</Title>
<Summary>Build high quality web apps using React</Summary>
</View>
)
},
})
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
margin: 40
},
image: {
height: 40,
marginRight: 10,
width: 40,
},
text: {
flex: 1,
justifyContent: 'center'
},
title: {
fontSize: '1.25rem',
fontWeight: 'bold'
},
subtitle: {
fontSize: '1rem'
}
})
Pre-render styles on the server:
// server.js
import App from './components/App'
import React, { StyleSheet } from 'react-native-web'
const html = React.renderToString(<App />);
const css = StyleSheet.renderToString();
const Html = () => (
<html>
<head>
<meta charSet="utf-8" />
<meta content="initial-scale=1,width=device-width" name="viewport" />
<style id="react-stylesheet" dangerouslySetInnerHTML={{ __html: css } />
</head>
<body>
<div id="react-root" dangerouslySetInnerHTML={{ __html: html }} />
</body>
</html>
)
Render styles on the client:
// client.js
import App from './components/App'
import React, { StyleSheet } from 'react-native-web'
import ReactDOM from 'react-dom'
const reactRoot = document.getElementById('react-root')
const reactStyleSheet = document.getElementById('react-stylesheet')
ReactDOM.render(<App />, reactRoot)
reactStyleSheet.textContent = StyleSheet.renderToString()
StyleSheet
StyleSheet is a style abstraction that transforms inline styles to CSS on the client or the server. It provides a minimal CSS reset.
Image
An accessibile image component with support for image resizing, default image, and child content.
ListView
(TODO)
ScrollView
(TODO)
Text
Displays text as an inline block and supports basic press handling.
TextInput
Accessible single- and multi-line text input via a keyboard.
Touchable
Touch bindings for press and long press.
View
The fundamental UI building block using flexbox for layout.
React Native for Web relies on styles being defined in JavaScript.
The View
component makes it easy to build common layouts with flexbox, such
as stacked and nested boxes with margin and padding. See this guide to
flexbox.
Styling components can be achieved with inline styles or the use of StyleSheet.
Major accessibility features are available through the following props:
accessible
, accessibilityLabel
, accessibilityLiveRegion
, and
accessibilityRole
. The accessibilityRole
prop is used to determine the
rendered DOM element. For example:
<View accessibilityRole='banner' />
=> <header role='banner' />
.<View accessibilityRole='button' />
=> <button type='button' role='button' />
.<Text accessibilityRole='link' href='/' />
=> <a role='link' href='/' />
.See the component documentation for more details.
Please read the contribution guidelines. Contributions are welcome!
Thanks to current and past members of the React and React Native teams (in particular Vjeux and Pete Hunt).
Thanks to react-tappable for
backing the current implementation of Touchable
.
Copyright (c) 2015 Nicolas Gallagher. Released under the MIT license.
FAQs
React Native for Web
The npm package react-native-web receives a total of 515,298 weekly downloads. As such, react-native-web popularity was classified as popular.
We found that react-native-web demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.