Tool to create a React Native library module or native view component, with a single command.
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.
- community support via issues
- commercial support is available, see http://xpbrew.consulting
- Minimum React Native version: 0.60 (outdated), 0.61 (recommended)
- 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 ref: react-native-community/react-native-tvos#11), issue #95
- requires use of
- tvOS platform fork
- Out-of-tree platforms
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.
brodybits/react-native-module-init
- new interactive CLI that uses the templates from this utiityreact-native-community/bob
- opinionated, interactive library CLI that is designed to support both native libraries and libraries with web support
Outdated alternatives: see acknowledgements below
Packages 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
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
--module-name <moduleName> The module package name to be used in package.json. Default: react-native-(name in param-case)
--view Generate the package as a very simple native view component
--object-class-name The name of the object class to be exported by both JavaScript and native code. Default: (name in PascalCase)
--prefix <prefix> DEPRECATED: The prefix of the name of the object class to be exported by both JavaScript and native code, ignored if --object-class-name is specified (Default: ``)
--module-prefix <modulePrefix> The prefix of the generated module package name, ignored if --module-name is specified (Default: `react-native`)
--package-identifier <packageIdentifier> [Android] The Java package identifier used by the Android module (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: `yourname@email.com`)
--license <license> The license type (Default: `MIT`)
--use-apple-networking [iOS] Use `AFNetworking` dependency as a sample in the podspec & use it from the iOS code
--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-version <version> React Native version for the generated example project (default: `react-native@latest`)
--write-example-podfile [iOS] EXPERIMENTAL FEATURE NOT SUPPORTED: write (or overwrite) example ios/Podfile
-h, --help output usage information
const createLibraryModule = require('create-react-native-module');
createLibraryModule({
name: 'MyFancyLibraryModule'
}).then(() => {
console.log('Oh yay! My library module has been created!');
})
{
name: String, /* The name of the library (mandatory) */
moduleName: String, /* The module package name to be used in package.json. Default: react-native-(name in param-case) */
view: Boolean, /* Generate the package as a very simple native view component (Default: false) */
objectClassName: String, /* The name of the object class to be exported by both JavaScript and native code. Default: (name in PascalCase) */
prefix: String, /* DEPRECATED: The prefix of the name of the object class to be exported by both JavaScript and native code, ignored if objectClassName is specified (Default: ``) */
modulePrefix: String, /* The prefix of the generated module package name, ignored if moduleName is specified (Default: `react-native`) */
platforms: Array | String, /* Platforms the library will be created for. (Default: ['android', 'ios']) */
packageIdentifier: String, /* [Android] The Java package identifier used by the Android module (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: `yourname@email.com`) */
license: String, /* The license type of this library (Default: `MIT`) */
useAppleNetworking: Boolean, /* [iOS] Use `AFNetworking` dependency as a sample in the podspec & use it from the iOS code (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`) */
exampleReactNativeVersion: String, /* React Native version for the generated example project (Default: `react-native@latest`) */
writeExamplePodfile: Boolean, /* [iOS] EXPERIMENTAL FEATURE NOT SUPPORTED: write (or overwrite) example ios/Podfile (Default: false) */
}
Create the module with no view:
create-react-native-module --prefix CB --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
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
):
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 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:
react-native run-ios
or do the following command to open the iOS project in Xcode:
open ios/example.xcodeproj
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
Create the module with an extremely simple view:
create-react-native-module --prefix CB --package-identifier io.mylibrary --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
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 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
inios
subdirectory - do
react-native run-ios
oropen 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
react-native-create-library
- original basis of this projectreact-native-share
- was acknowledged as "a great source of inspiration" forreact-native-create-library