• Stars
    star
    277
  • Rank 148,875 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

react-native-web plugin for Gatsby

Gatsby plugin for React-Native-Web / Expo

NPM Build Status

Adds React-Native-Web and Expo support to a Gatsby site.

Example "production" usage on my blog: sebastienlorber.com/using-expo-in-gatsby


Sponsor

ThisWeekInReact.com: the best newsletter to stay up-to-date with the React ecosystem:

ThisWeekInReact.com banner


Why

Main reasons:

  • sharing components between your mobile app and your static website.
  • using atomic CSS-in-JS with React-Native-Web

Cross-platform code is finally taking off, and it's time to share more code between web and mobile. There's already Expo web, but it is not suited for a marketing website that needs JAMStack / SEO / CMS integration / Performance / Gatsby-image ... This project aims to "merge" Expo for web with Gatsby, so that you can build useful things like:

  • Share a universal cross-platform design system between your mobile app and your marketing website
  • Blog about ReactNative, and include runnable RN demos directly in your MDX
  • Document your ReactNative components with Docz
  • Use ReactNativeWeb as a performant CSS-in-JS lib, like Twitter does (see atomic CSS-in-JS).

Supported features

This plugin uses the same setup as Expo for web, thanks to @expo/webpack-config

Support includes:

  • Primitive components from ReactNative (check RNW support)
  • Expo unimodules with web support like expo-camera (check Expo doc for support)
  • .web.js extension handling
  • Universal ReactNative design system libraries, like react-native-paper, react-native-ui-kitten...
  • Universal gesture systems with Animated, react-native-gesture-handler or Reanimated.
  • Universal SVG components using react-native-svg
  • Automatic transpilation of third party react-native libs
  • Works in MDX
  • Works in Docz (Gatsby-based)

Recipe

Use the new Gatsby Recipe feature to get started fast:

gatsby recipes https://raw.githubusercontent.com/slorber/gatsby-plugin-react-native-web/master/recipe.mdx

Manual setup

1. Install required dependencies

yarn add react-native react-native-web@~0.11.7 gatsby-plugin-react-native-web expo

2. Create a gatsby-config.js and use the plugin:

   module.exports = {
     plugins: [
       `gatsby-plugin-react-native-web`,
     ],
   }

3. Install additional cross-platform libraries / unimodules (optional)

Add expo audio/video components:

yarn add expo-av

4. Create a test pages

Create an example page like this one in your ./pages folder, or try importing React-Native / Expo components into an existing one.


Demo

The examples folder have runnable Gatsby site demos. They are also hosted:

Example usage

Very basic example:

import React from 'react'
import Link from 'gatsby-link'
import { StyleSheet, TouchableOpacity, Text, View } from 'react-native'

const styles = StyleSheet.create({
  box: { padding: 10, margin: 10, borderWidth: 1, borderColor: 'black' },
  text: { fontWeight: 'bold', color: 'red' },
  button: {
    marginVertical: 40,
    paddingVertical: 20,
    paddingHorizontal: 10,
    borderWidth: 1,
    borderColor: 'black',
    backgroundColor: 'lightgrey',
    alignItems: 'center',
  },
  buttonText: { fontWeight: 'bold', color: 'black' },
})

const IndexPage = () => (
  <View style={styles.box}>
    <Text style={styles.text}>
      Hi this is React-Native-Web rendered by Gatsby
    </Text>
    <TouchableOpacity style={styles.button} onPress={() => alert('it works')}>
      <Text style={styles.buttonText}>Button</Text>
    </TouchableOpacity>
    <Link to="/page-2/">Go to page 2</Link>
  </View>
)

export default IndexPage

image

How does it work

  • Adds babel-preset-expo which manages tree-shaking of react-native-web packages.
  • Implements module resolution for files with platform extensions like .web.js, .web.tsx...
  • Uses @expo/webpack-config which creates aliases for various React Native asset features and ensures that all React Native packages, and Unimodules are loaded with Babel.
  • Creates support for Gatsby SSR with react-native-web
  • Extracts critical CSS with the StyleSheet api of react-native-web and adds it to the static page.

FAQ

Expo already has web support, why do I need this?

Expo web support is more like Create-React-App, it only outputs a single html file and does client side routing. It works fine for apps, but miss the various benefits of Gatsby, including performance, SEO, CMS integration, Gatsby-image...

Actually, this plugin uses the same webpack config as Expo web support, and Expo (Evan Bacon) contributed to this project. You'll also find support for Next if you need a static/SSR hybrid.

How to share code for navigation/routing?

This is not easy, because navigation patterns are different between web and mobile.

ReactNavigation may have web support, but Gatsby can't use ReactNavigation config easily to construct static pages.

You'd rather keep using platform-specific navigation trees (pages for Gatsby, and stacks/tabs for ReactNavigation).

Eventually you could build your own cross-platform navigate() function, and your own cross-platform Link component (take a look at expo-gatsby-navigation).

Can I share the same repo to build a mobile app and a Gatsby site with shared components?

The most simple way to share code between an Expo app and a Gatsby site is currently to use a single folder for both the Expo app and the Gatsby app.

Otherwise you can try to setup a monorepo, but keep in mind this requires more complex configuration, and Metro does not follow symlinks.

You can also read the Expo doc about adding Gatsby support to an existing app.

How can I publish an universal cross-platform component that works on web and mobile?

You can take a loot at this example: expo-dark-mode-switch. It uses expo-module-scripts.

You can also check react-native-community/bob

Hire a freelance expert

Looking for a React/ReactNative freelance expert with more than 5 years production experience? Contact me from my website or with Twitter.

More Repositories

1

react-async-hook

React hook to handle any async operation in React components, and prevent race conditions
TypeScript
1,140
star
2

react-native-scroll-into-view

Scroll a ReactNative View into the visible screen. Similar to DOMElement.scrollIntoView() browser function.
TypeScript
409
star
3

awesome-debounce-promise

Debounce your API calls easily and stay in promised land.
TypeScript
376
star
4

scalable-frontend-with-elm-or-redux

An attempt to make Redux and Elm applications scale
JavaScript
363
star
5

trailing-slash-guide

Understand and fix your static website trailing slash issues!
HTML
307
star
6

react-native-animation-hooks

A simple declarative API for React-Native animations, using hooks
TypeScript
243
star
7

combine-promises

Like Promise.all(array) but with an object instead of an array.
TypeScript
202
star
8

rereduce

Reducer library for Redux
JavaScript
166
star
9

ajax-interceptor

This permits to wire interceptors on XHR requests and responses
JavaScript
122
star
10

react-reboot

The easiest way to refresh your React components with up-to-date syntax.
JavaScript
62
star
11

react-nested-loader

The easiest way to manage loaders/errors inside a button. NOT an UI lib.
JavaScript
62
star
12

sebastienlorber.com

My personal website
JavaScript
58
star
13

gcm-server-repository

maven repository for gcm-server for Android push notifications
Shell
46
star
14

react-native-alert-async

An Alert.alert that you can await
JavaScript
45
star
15

awesome-imperative-promise

Offer an imperative API on top of promise, with cancellation support
TypeScript
41
star
16

micro-typed-events

The smallest, most convenient typesafe TS event emitter you'll ever need
TypeScript
40
star
17

todomvc-onboarding

Showcase different ways to add a complex onboarding to a TodoMVC app
36
star
18

use-async-setState

Permits to await setState()
TypeScript
35
star
19

expo-typescript

TypeScript
33
star
20

redux-dispatch-subscribe

store enhancer for redux which allows to listen for dispatched actions
JavaScript
29
star
21

react-three-fiber-journey

Three.js Journey implemented with React-Three-Fiber
TypeScript
27
star
22

awesome-only-resolves-last-promise

Wraps an async function, and ensures that only last call will ever resolve/reject
TypeScript
26
star
23

docusaurus-crowdin-example

JavaScript
11
star
24

backspace-disabler

Disable the annoying browser backward navigation when the user press the backspace key
JavaScript
11
star
25

react-native-storage-slot

A simple typed wrapper around AsyncStorage
TypeScript
10
star
26

slorber

8
star
27

awesome-icu

Reference everything you find interesting about ICU translations
7
star
28

this-week-in-react

ThisWeekInReact.com
MDX
6
star
29

slo.im

slo.im shortcuts
4
star
30

react-updatable-context

React context which inject a value setter/updater in the Consumer
JavaScript
3
star
31

slorber-website

My website
JavaScript
2
star
32

test-docu

JavaScript
2
star
33

expo-ci

JavaScript
2
star
34

traced-svg-parameter-chooser

JavaScript
2
star
35

docusaurus-visual-tests

TypeScript
2
star
36

docusaurus-starter

JavaScript
1
star
37

bug-expokit-crashlytics

bug-expokit-crashlytics
JavaScript
1
star
38

slorber-api-screenshot

JavaScript
1
star
39

docusaurus-dsfr

TypeScript
1
star