Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

react-native-web plugin for Gatsby

License

NotificationsYou must be signed in to change notification settings

slorber/gatsby-plugin-react-native-web

Repository files navigation

NPMBuild Status

AddsReact-Native-Web andExpo 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.
  • usingatomic 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 (seeatomic 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 RNWsupport)
  • Expo unimodules with web support likeexpo-camera (check Expo doc for support)
  • .web.js extension handling
  • Universal ReactNative design system libraries, likereact-native-paper,react-native-ui-kitten...
  • Universal gesture systems with Animated,react-native-gesture-handler or Reanimated.
  • Universal SVG components usingreact-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 agatsby-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 likethis one in your./pages folder, or try importing React-Native / Expo components into an existing one.


Demo

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

Example usage

Very basic example:

importReactfrom'react'importLinkfrom'gatsby-link'import{StyleSheet,TouchableOpacity,Text,View}from'react-native'conststyles=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'},})constIndexPage=()=>(<Viewstyle={styles.box}><Textstyle={styles.text}>      Hi this is React-Native-Web rendered by Gatsby</Text><TouchableOpacitystyle={styles.button}onPress={()=>alert('it works')}><Textstyle={styles.buttonText}>Button</Text></TouchableOpacity><Linkto="/page-2/">Go to page 2</Link></View>)exportdefaultIndexPage

image

How does it work

  • Addsbabel-preset-expo which manages tree-shaking ofreact-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 withreact-native-web
  • Extracts critical CSS with theStyleSheet api ofreact-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-platformnavigate() function, and your own cross-platformLink component (take a look atexpo-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 aboutadding 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 usesexpo-module-scripts.

You can also checkreact-native-community/bob

Hire a freelance expert

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

Releases

No releases published

Packages

No packages published

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp