• Stars
    star
    625
  • Rank 71,862 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 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

create-react-native-module

Tool to create a React Native library module or native view component, with a single command.

GitHub license npm Mutation testing badge npm downloads total npm downloads GitHub watchers GitHub stars GitHub forks open bugs total open issues GitHub pull requests

See below for command-line usage, example with no view, and example with an extremely simple native view.

This tool based on react-native-create-library, with working example callbacks, optional native view, and more updates added by @brodybits (Christoper J. Brody aka Chris Brody) and other contributors.

Support options

General status

  • Minimum React Native version: 0.60 (outdated), 0.64 or upcoming 0.65 (recommended) - see issue #423
  • It is recommended to disable or remove Flipper on iOS in case of debug build issues - see issue #422
  • generated example app with symlink by default, has known issue with adding dependencies to the library root - see issue #308
  • Platform fork support
    • tvOS platform fork
      • requires use of --tvos-enabled option as documented below
      • requires the react-native-tvos fork, with minimum version of 0.60.x (newer version is highly recommended) ref:
      • It is recommended to disable or remove Flipper in case of debug build issues - see issue #422
  • Out-of-tree target platforms
    • Windows target platform - not supported directly, please use react-native-windows-init according to this procedure: https://aka.ms/RNW-NativeModuleSetup
    • for future consideration: macOS (see issue #94)

Why might you need this?

If you are looking to create a native module for React Native, you need some native code for each platform you want to support and then some JavaScript code to bind it all together. Setting this up by yourself can be time-consuming.

This is where this tool comes in. It creates a boilerplate with all current best practices in mind. Why not use react-native new-library? Unfortunately that command doesn't create an up-to-date library, requires an already initialized React Native project and only sets up the iOS side of things.

Alternatives

Outdated alternatives: see acknowledgements below

Installation

Package required to be installed globally if the recommended example app is generated:

$ npm install -g yarn

To install this package:

$ npm install -g create-react-native-module

Command-line usage

Navigate into an empty directory to execute the command.

$ create-react-native-module MyFancyLibrary

This will create the folder MyFancyLibrary in which the library will be created in.

Now install dependencies by running this command in the newly created library.

$ npm install
Usage: create-react-native-module [options] <name>

Options:

  -V, --version                             output the version number
  --package-name <packageName>              The full package name to be used in package.json. Default: react-native-(name in param-case)
  --is-view                                 Generate the package as a very simple native view component. Status: EXPERIMENTAL, with limited testing.
  --object-class-name                       The name of the object class to be exported by both JavaScript and native code. Default: (name in PascalCase)
  --native-package-id <nativePackageId>     [Android] The native Java package identifier used for Android (Default: `com.reactlibrary`)
  --platforms <platforms>                   Platforms the library module will be created for - comma separated (Default: `ios,android`)
  --tvos-enabled                            Generate the module with tvOS build enabled (requires react-native-tvos fork, with minimum version of 0.60, and iOS platform to be enabled)
  --github-account <githubAccount>          The github account where the library module is hosted (Default: `github_account`)
  --author-name <authorName>                The author's name (Default: `Your Name`)
  --author-email <authorEmail>              The author's email (Default: `[email protected]`)
  --license <license>                       The license type (Default: `MIT`)
  --use-apple-networking                    [iOS] EXPERIMENTAL FEATURE NOT SUPPORTED: Use `AFNetworking` dependency as a sample in the podspec & use it from the iOS code - see issue #426
  --generate-example                        Generate an example project and add the library module to it with symlink by defult, with overwrite of example metro.config.js to add workaround for Metro symlink issue - requires Yarn to be installed globally
  --example-file-linkage                    DEPRECATED: do `yarn add file:../` instead of `yarn add link:../` in a generated example project, and add a postinstall workaround script, with no overwrite of example metro.config.js
  --example-name <exampleName>              Name for the example project (default: `example`)
  --example-react-native-template <...>     The React Native template used for the generated example project, for example: react-native-tvos or [email protected] (requires --tvos-enabled option); [email protected] (default: `react-native@latest`)
  --write-example-podfile                   [iOS] EXPERIMENTAL FEATURE NOT SUPPORTED: write (or overwrite) example ios/Podfile
  -h, --help                                output usage information

Programmatic usage

const createLibraryModule = require('create-react-native-module');

createLibraryModule({
  name: 'MyFancyLibraryModule'
}).then(() => {
  console.log('Oh yay! My library module has been created!');
})

Options

{
  name: String, /* The name of the library (mandatory) */
  packageName: String, /* The full package name to be used in package.json. Default: react-native-(name in param-case) */
  isView: Boolean, /* Generate the package as a very simple native view component. Status: EXPERIMENTAL, with limited testing. (Default: false) */
  objectClassName: String, /* The name of the object class to be exported by both JavaScript and native code. Default: (name in PascalCase) */
  platforms: Array | String, /* Platforms the library will be created for. (Default: ['android', 'ios']) */
  nativePackageId: String, /* [Android] The native Java package identifier used for Android (Default: `com.reactlibrary`) */
  tvosEnabled: Boolean, /* Generate the module with tvOS build enabled (requires react-native-tvos fork, with minimum version of 0.60, and iOS platform to be enabled) */
  githubAccount: String, /* The github account where the library is hosted (Default: `github_account`) */
  authorName: String, /* The author's name (Default: `Your Name`) */
  authorEmail: String, /* The author's email (Default: `[email protected]`) */
  license: String, /* The license type of this library (Default: `MIT`) */
  useAppleNetworking: Boolean, /* [iOS] EXPERIMENTAL FEATURE NOT SUPPORTED: Use `AFNetworking` dependency as a sample in the podspec & use it from the iOS code - see issue #426 (Default: false) */
  generateExample: Boolean, /* Generate an example project and add the library module to it with symlink by defult, with overwrite of example metro.config.js to add workaround for Metro symlink issue - requires Yarn to be installed globally (Default: false) */
  exampleFileLinkage: Boolean, /* DEPRECATED: do `yarn add file:../` instead of `yarn add link:../` in a generated example project, and add a postinstall workaround script, with no overwrite of example metro.config.js (Default: false) */
  exampleName: String, /* Name for the example project (Default: `example`) */
  exampleReactNativeTemplate: String, /* The React Native template used for the generated example project, for example: react-native-tvos or [email protected] (requires --tvos-enabled option); [email protected] (Default: `react-native@latest`) */
  writeExamplePodfile: Boolean, /* [iOS] EXPERIMENTAL FEATURE NOT SUPPORTED: write (or overwrite) example ios/Podfile (Default: false) */
}

Examples

Example module with no view

Create the module with no view:

create-react-native-module --package-identifier io.mylibrary --generate-example AliceHelper

The module would be generated in the react-native-alice-helper subdirectory, and the example test app would be in react-native-alice-helper/example.

Then go into the example app subdirectory:

cd react-native-alice-helper/example

Running the example app

Recommended: Follow the instructions shown in the end of the console log output, which are more likely to be up-to-date.

Extra notes:

Within the example test app subdirectory:

It is recommended to start the Metro Bundler manually (within react-native-alice-helper/example), which would run in the foreground:

yarn start

Otherwise, React Native will open its own window to run the Metro Bundler.

To run on Android, do the following command (within react-native-alice-helper/example):

yarn android

for React Native 0.60: npx react-native run-android

This assumes that the ANDROID_HOME environmental variable is set properly. Here is a sample command that does not make such an assumption on a mac:

ANDROID_HOME=~/Library/Android/sdk npx react-native run-android

For iOS:

Extra installation step needed in case of clean checkout only:

cd ios && pod install && cd ..

Then to run on iOS:

yarn ios

for React Native 0.60: npx react-native run-ios

or do the following command to open the iOS project in Xcode:

open ios/example.xcodeproj

Expected result

The example app shows the following indications:

  • STATUS: native callback received
  • NATIVE CALLBACK MESSAGE with the number argument and string argument values that are received by the native module

Example view module

EXPERIMENTAL feature with limited testing

Create the module with an extremely simple view:

create-react-native-module --package-identifier io.mylibrary --is-view --generate-example CarolWidget

The module would be generated in the react-native-carol-widget subdirectory, and the example test app would be in react-native-carol-widget/example.

Then go into the example app subdirectory:

cd react-native-carol-widget/example

Running the view example app

Recommended: Follow the instructions shown in the end of the console log output, which are more likely to be up-to-date.

Some extra notes:

Within the example test app subdirectory:

It is recommended to start the Metro Bundler manually as described above (within react-native-carol-widget/example):

yarn start

To run on Android: do yarn android or npx react-native run-android as described for the other example above.

To run on iOS (as described above):

  • in case of clean checkout only: do pod install in ios subdirectory
  • do yarn ios, npx react-native run-ios, or open ios/example.xcodeproj

Expected result:

  • on Android: a check box that is checked (and cannot be changed)
  • on iOS: a label with 5 red asterisks

Acknowledgements

License

MIT

More Repositories

1

prettierx

a less opinionated fork of Prettier code formatter
JavaScript
198
star
2

cordova-sqlite-ext

A Cordova/PhoneGap plugin to open and use sqlite databases on Android/iOS/macOS/Windows with REGEXP (Android/macOS/iOS) and pre-populated databases (Android/iOS/macOS/Windows)
JavaScript
73
star
3

react-native-module-init

JavaScript
54
star
4

sql-promise-helper

JavaScript
11
star
5

prettier-plugin-x

JavaScript
10
star
6

cordova-sqlite-legacy

Legacy version of Cordova/PhoneGap plugin to open and use sqlite databases on Windows 8.1/Windows Phone 8.1 in addition to Android/iOS/macOS/Windows 10 with HTML5/Web SQL API
JavaScript
6
star
7

hyperapp-like-native-view-demo

Objective-C
5
star
8

Android-sqlite-ext-native-driver

C
4
star
9

cordova-sqlite-test-app

JavaScript
4
star
10

cordova-sqlite-storage-dependencies

C
4
star
11

sqlite3-base64

C
3
star
12

snowpack-vue-capacitor-2-demo

Swift
3
star
13

Android-sqlite-native-driver-ICU

Native build of sqlite for Android-sqlite-connector with ICU (4.4l) embedded
C
3
star
14

ask-me-anything

ASK ME ANYTHING such as support, ideas, or personal questions
3
star
15

android-sqlite-native-ndk-connector

Java
2
star
16

android-sqlite-ndk-native-driver

C
2
star
17

cordova-sqlite-ext-bootstrap-starter-app

JavaScript
2
star
18

node-php-serve

PHP Server command line for Node.js
JavaScript
2
star
19

Cordova-PGB-dialog-test-Windows10-arm

Cordova PhoneGap Build dialog test app for Windows 10 (arm) - WORKING OK ON MY MOBILE DEVICE
HTML
1
star
20

cordova-sqleet2020-evplus-build-free

C
1
star
21

phonegap-lite-ios-gen

PhoneGap iOS projects from the command line with no built-in plugins except DebugConsole
JavaScript
1
star
22

cordova-iab-custom-scheme-test-wip

JavaScript
1
star
23

java-node

EXPERIMENTAL: Native interface between Java and Node (v4.x) library
C++
1
star
24

libb64-core

C
1
star
25

Android-sqlite-native-driver-regexp-pcre

C
1
star
26

aq-query-test-android

Asynchronous Javascript query from web worker in WebView working for Android
Java
1
star
27

cordova-notes

1
star
28

cordova-aqs-test

Java
1
star
29

hyperapp-rewrite-demo-on-inferno-and-superfine

JavaScript
1
star
30

expo-razzle-react-primitives-starter

JavaScript
1
star
31

Cordova-quick-start-checklist

1
star
32

cordova-plugin-sqlcipher-crypto-batch-connection-manager-core-pro-free-unstable-0x

Java
1
star
33

prettier-plugin-prettierx-typescript

JavaScript
1
star
34

cordova-plugin-sqlite-batch-connection-manager-core-pro-free-unstable-0x

Java
1
star
35

leb

Utilities for the LEB128 number representation
JavaScript
1
star
36

Cordova-pre-populated-db-example-android

DEPRECATED AND NOT SUPPORTED
JavaScript
1
star
37

cordova-plugin-sqlite-batch-connection-manager-core-unstable-0x

Java
1
star
38

cordova-sqlite-evcore-legacy-ext-common-free

Cordova sqlite plugin with Android performance enhancements, GPL or commercial license options, with LEGACY SUPPORT for Windows 8.1 / Windows Phone 8.1 on VS 2015
JavaScript
1
star
39

Cordova-PGPluginShim

Shim files to run PhoneGap 1.4- plugins on Cordova 1.5+
Objective-C
1
star
40

react-primitives-input

JavaScript
1
star
41

cordova-sqlite-with-jquery-ajax-api-demo

Sample Cordova sqlite Ajax demo
C
1
star
42

Cordova-scripts

Shell
1
star
43

cordova-sqlite-evplus-legacy-attach-detach-free

Cordova sqlite storage plugin, free enterprise version (GPL) for Android and iOS with memory improvement, support for attach/detach, and other extra features
C
1
star
44

phonegap-lite-android

PhoneGap iOS projects from the command line with no built-in plugins (except Device builtins)
Java
1
star