4.7 KiB
React Native for Web
The core React Native components adapted and expanded upon for the web, backed by a precomputed CSS library. ~19KB minified and gzipped.
Table of contents
Install
npm install --save react react-native-web
Use
React Native for Web exports its components and a reference to the React
installation. Styles are authored in JavaScript as plain objects.
import React, { View } from 'react-native-web'
class MyComponent extends React.Component {
render() {
return (
<View style={styles.root} />
)
}
}
const styles = {
root: {
borderColor: 'currentcolor'
borderWidth: '5px',
flexDirection: 'row'
height: '5em'
}
}
Components
Image
An accessibile image component with support for image resizing, default image, and child content.
ListView
(TODO)
ScrollView
(TODO)
Swipeable
Touch bindings for swipe gestures.
Text
Displays text as an inline block and supports basic press handling.
TextInput
Accessible single- and multi-line text input via a keyboard. Supports features
Touchable
Touch bindings for press and long press.
View
The fundamental UI building block: layout with flexbox, layout and positioning styles, and accessibility controls.
Styling
React Native for Web provides a mechanism to declare all your styles and layout
inline with the components that use them. The View component makes it easy to
build common layouts with flexbox, such as stacked and nested boxes with margin
and padding.
Styling is identical to using inline styles in React, but most inline styles are converted to single-purpose classes. The current implementation includes 300+ precomputed CSS declarations (~4.5KB gzipped) that cover a large proportion of common styles. A more sophisticated build-time implementation may produce a slightly larger CSS file for large apps, and fall back to fewer inline styles. Read more about the styling strategy.
See this guide to flexbox.
import React, { Image, Text, View } from 'react-native-web'
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}
/>
<View style={styles.text}>
<Text style={styles.title}>
React Native Web
</Text>
<Text style={styles.subtitle}>
Build high quality web apps using React
</Text>
</View>
</View>
)
},
})
const styles = {
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'
}
}
Combine and override style objects:
import baseStyle from './baseStyle'
const buttonStyle = {
...baseStyle,
backgroundColor: '#333',
color: '#fff'
}
Contributing
Please read the contribution guidelines. Contributions are welcome!
Thanks
Thanks to current and past members of the React and React Native teams (in particular Vjeux and Pete Hunt), and Tobias Koppers for Webpack and CSS loader.
Thanks to react-swipeable
for the current implementation of Swipeable, and to
react-tappable for backing the
current implementation of Touchable.
License
Copyright (c) 2015 Nicolas Gallagher. Released under the MIT license.