Git Product home page Git Product logo

react-native-web's Introduction

React Native for Web

Build Status npm version gzipped size

React Native components and APIs for the Web. Flexbox layout and JavaScript styling.

Table of contents

Quick start

You can try the latest version on CodePen.

To install in your app:

npm install --save [email protected] [email protected] react-native-web

Overview

Importing

All API's, components, and a Web-specific React are provided by the react-native-web module:

import React, { Image, StyleSheet, Text, View } from 'react-native-web'

Client-side rendering

Client-side rendering requires that you use the module's React export. React.render is a thin wrapper around ReactDOM.render that renders your application and the style sheet. Styles are updated if new bundles are loaded asynchronously.

// client.js
import App from './components/App'
import React from 'react-native-web'

React.render(<App />, document.getElementById('react-root'))

Server-side rendering

Server-side rendering is done by calling React.renderToString or React.renderToStaticMarkup, the output of both includes the style sheet.

// server.js
import App from './components/App'
import React from 'react-native-web'

const html = React.renderToString(<App />);

const Html = () => (
  <html>
    <head>
      <meta charSet="utf-8" />
      <meta content="initial-scale=1,width=device-width" name="viewport" />
    </head>
    <body>
      <div id="react-root" dangerouslySetInnerHTML={{ __html: html }} />
    </body>
  </html>
)

Styling

React Native for Web allows you to define styles using JavaScript, either with inline styles or StyleSheet.create.

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.

Accessibility

The most common and best supported accessibility features of the Web are leveraged through 4 props available on most components: accessible, accessibilityLabel, accessibilityLiveRegion, and accessibilityRole.

Example

More examples can be found in the examples directory.

import React, { Image, StyleSheet, Text, View } from 'react-native-web'

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 styles = StyleSheet.create({
  card: {
    flexGrow: 1,
    justifyContent: 'center'
  },
  title: {
    fontSize: '1.25rem',
    fontWeight: 'bold'
  },
  image: {
    height: 40,
    marginRight: 10,
    width: 40
  }
})

APIs

StyleSheet

StyleSheet is a style abstraction that transforms inline styles to CSS on the client or the server. It provides a minimal CSS reset targeting elements and pseudo-elements beyond the reach of React inline styles.

Components

Image

An accessibile image component with support for image resizing, default image, and child content.

ListView

(TODO)

ScrollView

A scrollable view with event throttling.

Text

Displays text inline 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.

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).

Thanks to react-tappable for backing the current implementation of Touchable.

License

Copyright (c) 2015 Nicolas Gallagher. Released under the MIT license.

react-native-web's People

Contributors

dremora avatar necolas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.