Skip to content

trutoo/react-native-electron

 
 

Repository files navigation

React Native Electron Build Status npm version

Electron extensions to React Native for Web

Introduction

This project aims to provide extensions to React Native for Web targeted to the Electron environment to support additional modules exposed by React Native (ex Clipboard, WebView) using Electron APIs.

This is very early stage, not fully tested, and APIs will likely change between releases, so don't use this library if you need something stable.

Installation

npm install react-native-electron

electron, react and react-native-web are required peer dependencies, make sure to install them as well:

npm install electron react react-native-web

react-art is also needed if you use ART.

Example

See the example directory for the source code and Webpack config.

To run the demo app, fork this repository and run:

  • npm install
  • npm run example:server
  • In another terminal instance, npm run example:electron

For a more advanced example, see the GH Viewer client using this library among others of the React ecosystem.

APIs

Alert

React Native's Alert implementation using Electron's dialog

Alert.alert(
  title: string,
  message: ?string,
  buttons: ?Array<{text: string, onPress?: () => void}> = [],
  type: ?('none' | 'info' | 'error' | 'question' | 'warning') = 'none'
): void

Clipboard

React Native's Clipboard implementation using Electron's clipboard.

Clipboard.getString(type: ?string): Promise<?string>

Clipboard.setString(text: string, type: ?string): void

Linking

React Native's Linking implementation using Electron's app and shell APIs.

Linking.openURL(url: string): Promise<void>

Linking.addEventListener(type: string, handler: Function): void

Linking.removeEventListener(type: string, handler: Function): void

Linking.canOpenURL(): Promise<true>: always resolves to true

Linking.getInitialURL(): Promise<?string>: resolves with the process.argv[1] value, expecting the app to be opened by a command such as myapp myapp://test

Components

WebView

React Native's WebView implementation using Electron's webview tag

Communication between the WebView and its container is possible by providing an onMessage() handler, that will receive events having messages injected in event.nativeEvent.data, and calling the postMessage() method to send messages to the WebView contents. Messages must be strings.
This bridge is implemented using the preload attribute of Electron's WebView. If you override this attribute, make sure to provide a similar implementation so the bridge can work.
Loading this preload script uses node's __dirname, make sure your application injects it as expected, see this Webpack config for example.

Props

  • injectedJavaScript?: string: JS code executed after the dom-ready event is fired.
  • onError?: (event: Event) => void
  • onLoad?: (event: Event) => void
  • onLoadEnd?: (event: Event) => void
  • onLoadStart?: (event: Event) => void
  • onMessage?: (event: Event) => void
  • onNavigationStateChange?: (state: {loading: boolean, url: string}) => void
  • source: {uri: string} | {html: string}

Methods

  • postMessage (message: string): void. This method can only be called if the onMessage() prop is provided.

License

MIT
See LICENSE file.

About

Electron extensions to React Native for Web

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.9%
  • HTML 2.1%