Can’t find a native module for that native feature you desperately need for your React Native app? I’d recommend to simply just making your own. I don’t even code Java and I find creating native modules easy. Hope you will too! Getting setup is just a few commands and it should only take you a couple minutes to write a wrapper around your favorite library and then ship it to npm and link it into your app.
To start, simply install the react-native-create-library cli and generate your project
By default, the cli will include both platforms, but I only want android for this example so I’m going to include the platforms flag. You’ll notice a directory was created and inside is a folder called android. All your android specific code will go in there. Notice also it named your package.json ‘react-native-one-plus-one’. It’s best to keep with the react native convention of prefixing your native modules with react-native and RN (android modules).
In this case, you’ll see inside the android folder a com > evanjmg folder. You can set your package identifier to whatever you like but it’s best to set it first using the cli (I used evanjmg my github handle). In that folder is RNOnePlusOneModule.java. You now have a working react native module!
But, it doesn’t do anything…yet. It simply exposes an Java class into Javascript code. No methods or properies are exposed right now. However, we’re all setup to become the next jedi.
Now, let’s do some jedi native stuff. As noted, we’re going to create a Singleton class that has a method that adds one. So no matter where I import this native module in my application it will always keep count. By default, this module is already a Singleton as it’s instantiated on application boot up and will hold it’s value until the application is shut down.
So let’s add variable called count and a method called ‘addOne’.
Notice that we added the decorator @ReactMethod in order to expose the method to Javascript. This allows one to access the addOne and getCount method.
Before writing the javascript, we always need to import our react native module. In order to do that for development, we need to do the following:
npm link
, this tells our local npm that hey we want to use this module locally somewhere.npm link react-native-one-plus-one
react native link react-native-one-plus-one
. It should give you a confirmation that your code is linked and you should be ready to use it in your react native app, by running react-native run-android
. It should show up in your build, if this build fails you’ll likely see a Java error that you can fix. After you have your application installed, you can add your native module to your javascript code.Now our Javascript, will look something like this…
Awesome. We’ve added natively! But there’s more you can do…
Now say I want to my app to listen to the change of this count somewhere else in my app. That sounds incredibly difficult, but it’s not! React native has so many awesome classes/event listeners etc that you can extend and hook into (see here). For now, the easiest to implement is a DeviceEventEmitter event.
Notice at the top I imported logging and the DeviceEventManagerModule. Then I created a private method ‘sendEvent’ that use the Device EventManager to emit an event with the name ‘ON_COUNT_CHANGE’. We can then use this method to send the amount to a Javascript listener like so…
Pretty awesome huh? React Native modules are a breeze…
If you want to check out one of my native modules, check out react-native-home-pressed