From time to time, you'll want to install a third party library for React Native that uses native capabilities. Those are platform-specific APIs, usually having to do with media or more complicated APIs.
For example, today we're going to install the React Native video player with "npm install -- save react native video."
Now that that's installed, let's go ahead and try and add this to our application and see what happens. I have a pretty simple example here. I'm going to start by importing the video component from react-native-video.
I'm going to replace our render method here, and then I'm going to try and render a video component. This takes a couple of props. The first one is going to be a source, for which media we want to use. I've downloaded a simple video of some clouds which you can find in the GitHub repo.
I'm going to require a ./assets/clouds.mp4 and then I'm also going to set some other props. I'm going to set "resizeMode" to be "cover," I'm going to set "repeat" to be "true," so I want this video to play on a loop. I'm going to set "pause" to be "false," so that this video begins playing automatically.
Then I'm also going to set some styles. "style =" and then we'll say "styles.backgroundvideo." Now we'll go down into our StyleSheet here.
Start a new one. We're going to say "backgroundVideo." I want to take up the whole screen, so I'm going to say "position 'absolute.'" Then I'm going to set the coordinates "top" to all be zeros, "top, 0," "left, 0," "right, 0," "bottom, 0."
We have all this stuff. This should be all that we need to render. I'm going to save this file, and now let's try to view it in the simulator. One last thing. Let's not forget our closing "." I'm going to save this.
Let's view it in the simulator. I'm going to reload this. We'll see that I'm getting, "Undefined is not an object." It's looking for React Native native modules, and then this "RCTVideo.Constants" file which it doesn't have. This indicates that we're missing a library, that we need to link to an actual native library.
These error messages can vary a lot. Really, you should be looking at the documentation for the module you are installing to figure out whether or not linking libraries is necessary.
You can see that, on Android, I also get the same issue, "Undefined is not an object." Something is not right here.
Fortunately, linking in these required libraries is simple if the modules were packaged correctly.
I'm going to hop over to my terminal, and I'm going to type "react-native link" and then the name of the module, so "react-native video" in this case.
I'm going to run that. It's going to tell me that there is an Android dependency and an iOS dependency, and it has linked both of them successfully.
First, I'm going to do that for iOS by going into XCode, hitting stop, and then pushing "Play" again. Now let's open up the emulator there.
Awesome. You can see that now, instead of that terrible red error screen, we're actually getting the video and it's playing. Now let's fix Android as well.
I'm going to hide XCode and the iOS emulator. I'm going to go into here. I'm going to hit "react-native run android" again. Once the build is successful, let's check out our emulator again, and you can see that video is also working on Android now.
This is great. Whenever you need to install a module that includes both native code for iOS and Android, or either of them, just remember that you not only have to do "npm install -- save," you also need to do "react-native link" and then the module name.