All Projects → inokawa → react-native-react-bridge

inokawa / react-native-react-bridge

Licence: MIT license
An easy way to integrate your React (or Preact) app into React Native app with WebView.

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to react-native-react-bridge

tacklebox
🎣React UX components for handling common interactions
Stars: ✭ 15 (-82.14%)
Mutual labels:  react-dom, react-hooks
taro-playground
The Taro Playground App is a cross-platform application developed using Taro, to help developers develop and debug Taro applications.
Stars: ✭ 33 (-60.71%)
Mutual labels:  metro, expo
Wouter
🥢 A minimalist-friendly ~1.5KB routing for React and Preact. Nothing else but HOOKS.
Stars: ✭ 3,654 (+4250%)
Mutual labels:  preact, react-hooks
Preact Compat
🙌 React compatibility layer for Preact.
Stars: ✭ 927 (+1003.57%)
Mutual labels:  react-dom, preact
react-register-nodes
Register DOM Nodes in React
Stars: ✭ 32 (-61.9%)
Mutual labels:  react-dom, react-hooks
Portfolio
Expo + Next.js Portfolio
Stars: ✭ 56 (-33.33%)
Mutual labels:  preact, expo
tic-tac-toe-app
Online multiplayer Tic Tac Toe game for iOS, Android, and web.
Stars: ✭ 34 (-59.52%)
Mutual labels:  preact, expo
acharep
🏠 App to find fraternities near your university
Stars: ✭ 23 (-72.62%)
Mutual labels:  expo, react-hooks
react-hook-geolocation
A React hook to access data from the Geolocation API
Stars: ✭ 31 (-63.1%)
Mutual labels:  react-hooks
use-detect-print
A react hook to detect when a page is being printed
Stars: ✭ 55 (-34.52%)
Mutual labels:  react-hooks
meteor-react-native
Meteor client for React Native matching Meteor Spec
Stars: ✭ 43 (-48.81%)
Mutual labels:  expo
CefGlue
.NET binding for The Chromium Embedded Framework (CEF)
Stars: ✭ 44 (-47.62%)
Mutual labels:  webview
csgo-rcon-nodejs
A web panel to control a CS:GO server
Stars: ✭ 46 (-45.24%)
Mutual labels:  preact
react-native-web-view
An implementation of React Native's WebView that allows for postMessage on iOS devices.
Stars: ✭ 13 (-84.52%)
Mutual labels:  webview
pokehooks-labs
A laboratory to use pokemons and do some experiments with React Hooks API
Stars: ✭ 35 (-58.33%)
Mutual labels:  react-hooks
react-hooks-gatsby
Learn how to use React Hooks, and how they work with Gatsby. Watch the livestream:
Stars: ✭ 18 (-78.57%)
Mutual labels:  react-hooks
book-fullstack-react
Fullstack React: The Complete Guide to ReactJS and Friends by Anthony Accomazzo
Stars: ✭ 100 (+19.05%)
Mutual labels:  react-hooks
turtle-action
🐢 Turtle CLI with GitHub Actions for building expo-project
Stars: ✭ 16 (-80.95%)
Mutual labels:  expo
use-monaco
Use 🗒️ monaco-editor in any ⚛️ React app with simple hooks 🎣
Stars: ✭ 85 (+1.19%)
Mutual labels:  react-hooks
7-react-admin-ts
用 ts + react-hooks 实现的管理后台
Stars: ✭ 23 (-72.62%)
Mutual labels:  react-hooks

react-native-react-bridge

npm check

An easy way to integrate your React (or Preact) app into React Native app with WebView.

Why?

If you'd like to run your React web app in React Native, rewriting it for React Native or using react-native-web is preferred way in most cases. But sometimes rewriting is overkill, when you are just prototyping, or when the app includes something not available on React Native, like rich text editor with contenteditable or complicated logic with WebAssembly.

So how we run React app in React Native app as it is? It's logically possible if you run your web code in WebView using react-native-webview. However bundling React code with React Native is troublesome and implementing communication between React Native and WebView is so hard.

This library gives a bridge to make it easy. This will bundle the whole React app by some additional codes and it will be automatically re-compiled if you edit it. You rarely need to think which code you are editing for React or React Native, like isomorphic. The communication between React app and React Native app will be also simplified by this.

Features

  • Create React (or Preact) app bundle for WebView automatically in build process of React Native
    • .js, .ts, .jsx, .tsx, .mjs and .cjs will be packed into one source.
    • NOTE: Only the edits in the entry file of web will invoke rebuild because of the limitation of metro's build process.
  • Handle communication between React Native and WebView with React hook style
    • With useWebViewMessage hook, you can subscribe messages from WebView.
    • With useNativeMessage hook, you can subscribe messages from React Native.
    • emit function sends message.
  • Support bundling some assets in web side with ES6 import syntax

If you have some feature requests or improvements, please create a issue or PR.

Install

npm install react-native-react-bridge react-native-webview

# Necessary only if you render React app in WebView
npm install react-dom

# Necessary only if you render Preact app in WebView
npm install preact

Requirements

  • react >= 16.8
  • react-native >= 0.60
  • (preact >= 10.0)

Supported react-native versions

react-native-react-bridge react-native
>=0.9.0 >=0.65.0
0.0.0 - 0.8.1 <=0.64.2

Usage

1. Fix metro.config.js to use babelTransformer from this library.

React Native

module.exports = {
  transformer: {
    // This detects entry points of React app and transforms them
    // For the other files this will switch to use default `metro-react-native-babel-transformer` for transforming
    babelTransformerPath: require.resolve('react-native-react-bridge/lib/plugin'),
    ...
  },
  rnrb: {
    // Set `true` if you use Preact in web side.
    // This will alias imports from `react` and `react-dom` to `preact/compat` automatically.
    preact: true
  },
  ...
};

Expo

const { getDefaultConfig } = require("expo/metro-config");

const config = getDefaultConfig(__dirname);

module.exports = {
  ...config,
  transformer: {
    ...config.transformer,
    babelTransformerPath: require.resolve(
      "react-native-react-bridge/lib/plugin"
    ),
  },
};

2. Make entry file for web app.

  • If you use React in web, import modules from react and react-native-react-bridge/lib/web.
  • If you use Preact in web, import modules from preact and react-native-react-bridge/lib/web/preact.
  • If you use Preact in web but with React aliases, import modules from react and react-native-react-bridge/lib/web.
// WebApp.js

import React, { useState } from "react";
import {
  webViewRender,
  emit,
  useNativeMessage,
} from "react-native-react-bridge/lib/web";
// Importing css is supported
import "./example.css";
// Images are loaded as base64 encoded string
import image from "./foo.png";

const Root = () => {
  const [data, setData] = useState("");
  // useNativeMessage hook receives message from React Native
  useNativeMessage((message) => {
    if (message.type === "success") {
      setData(message.data);
    }
  });
  return (
    <div>
      <img src={image} />
      <div>{data}</div>
      <button
        onClick={() => {
          // emit sends message to React Native
          //   type: event name
          //   data: some data which will be serialized by JSON.stringify
          emit({ type: "hello", data: 123 });
        }}
      />
    </div>
  );
};

// This statement is detected by babelTransformer as an entry point
// All dependencies are resolved, compressed and stringified into one file
export default webViewRender(<Root />);

3. Use the entry file in your React Native app with WebView.

// App.js

import React from "react";
import WebView from "react-native-webview";
import { useWebViewMessage } from "react-native-react-bridge";
import webApp from "./WebApp";

const App = () => {
  // useWebViewMessage hook create props for WebView and handle communication
  // The argument is callback to receive message from React
  const { ref, onMessage, emit } = useWebViewMessage((message) => {
    // emit sends message to React
    //   type: event name
    //   data: some data which will be serialized by JSON.stringify
    if (message.type === "hello" && message.data === 123) {
      emit({ type: "success", data: "succeeded!" });
    }
  });

  return (
    <WebView
      // ref, source and onMessage must be passed to react-native-webview
      ref={ref}
      // Pass the source code of React app
      source={{ html: webApp }}
      onMessage={onMessage}
    />
  );
};

4. Start your React Native app!

FAQs

My webview displays a blank page.

react-native-webview has some ways to show errors occurred in webview. This may be helpful to troubleshoot it.

https://github.com/react-native-webview/react-native-webview/blob/master/docs/Reference.md#onerror

Demo

This repository includes demo app.

React Native

Before running this app, please prepare environment for React Native (https://reactnative.dev/docs/environment-setup).

git clone [email protected]:inokawa/react-native-react-bridge.git
cd examples/DemoApp
yarn
yarn ios # or yarn android

Expo

git clone [email protected]:inokawa/react-native-react-bridge.git
cd examples/DemoAppExpo
yarn
expo start
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].