Using React Hooks + Capacitor to Access Native Mobile APIs


Using Capacitor, you can easily create native mobile iOS and Android applications using all of the React web tools you know and love. But how do we access the native APIs? With the @capacitor-community/react-hooks package, we can quickly and easily access native device APIs, like Camera, Filesystem, and Geolocation, using React Hooks!



Hello, my name is Thomas Vitis and I'm the Capacitor Developer Experience Engineer working at Ionic. Today, I'm going to be going over how you can use React Hooks and Capacitor together to access native mobile APIs. So first of all, you might be thinking, what the heck is Capacitor? I've never heard of this before. Don't worry. Capacitor is a tool built by Ionic that allows you to take your web applications and compile it down to a native iOS project or a native Android project. It's open source, it's MIT licensed. You can think of it kind of like React Native or think of it like Flutter. The big difference is we're taking your HTML, your CSS, your JavaScript and putting it in an iOS or Android project. If it works on the web, it'll work with Capacitor. It's very powerful. Capacitor comes with a set of plugins so you can access stuff like the storage, the file system, the device information, or in this example, the camera. We also provide an API so you can communicate via our JavaScript bridge. So you can do stuff on the web, call our bridge, and that'll do stuff on native. It's pretty straightforward. We have tons of docs on how to build your own plugins as well as a huge community of existing plugins out there. Using the Capacitor plugin is pretty straightforward. So let's go over how to use the camera plugin. We import our camera from the Capacitor camera package. We have a camera object in this result type, which just the result type just tells us is this a URI, is this base64, things like that. So let's take a look at our camera object. A camera has a function object called get photo. Get photo allows us to take a new photo or to access our gallery via a single function call. And that allows us to set the quality or if we want to open editing in the native editor. This is something we can't do in the web or on a PWA. So it's a very powerful tool to open the native editor, make it feel really native. So let's hop on back to the codes. Camera.getPhoto returns an image. And from there, we look up the HTML image element, like an image tag. And we set that image HTML element, we set it up to the image data we've selected from the gallery. This works great in any React app or any web app. But this might not feel very Reacty or very hooky if you're using hooks really heavily in your project. So we have a great community, our Capacitor community GitHub organization that's built a React hooks package. The React hooks wraps all of our official plugins. So the ones I mentioned before, the storage, the file system, in this case, the camera. It makes them feel right at home in your React application that's heavily using hooks. So let's take a look at the React hooks camera plugin API. We have useCallback, which is a native React hook. And we have useCamera, which is our Capacitor community camera React hook. And we have our result type that we have before from our Capacitor camera package. We call our useCamera hook to get our photo, which is our photo object, and our getPhoto, which calls that camera.getPhoto function I was showing in the other example. You can think of this most similar to, like, useState, where we have our state and then our setState. Then we have this triggerCamera function, which calls useCallback, calls our camera.getPhoto with the quality, with the allowEditing, and what we want the result type to be. Feels much more at home in a React hooks-heavy application. Let's go on over to a demo I pre-prepared. So there's two big components here. We have our app, which is just our big app.tsx that most applications have. We have this other custom component called our camera hook button, which let's hop on over there. And this is just a component that has a div that conditionally renders our image if we selected a photo, and a button that we can click that would trigger our camera that says click me. So let's hop on over to the goodies. We have, like we have in our slides example, we have photo and getPhoto, which calls our useCamera hook. Then from there, we have triggerCamera, which calls our useCallback hook, which calls the, you can think of this like camera.getPhoto, but getting this from the hook. So it's just getPhoto. It's 100% quality. We don't want to open the native editor with our result type. That's a data URL. So once we've triggered our camera, so we've clicked this button, we've selected something that will set this photo to a value, which will then conditionally render this image tag. So let's go ahead and see this in action. I've gone ahead and already compiled the iOS application and I put it on this iOS simulator. This is running the iOS simulator. It comes bundled with Xcode. You can get it for free on any Mac, Apple provides it. Super great for texting, even just your web application to see what it looks like on iOS. So we have our hello React Summit application. This is a native application. So right here, React Summit. It says hello React Summit and this click me button from what we have right here, click me. We're going to click it so we hit the trigger camera, use callback hook. We'll select from our photos and we'll pick this beautiful waterfall. And from there, ta-da, we have this waterfall. It's super great. This code feels much more at home. I love React hooks. This feels much more at home with writing a lot of the other code. We could set this other with use effect. So when we first load it, it will automatically trigger something. Opens up with use state so we can select multiple photos. It's really powerful and it feels right at home with any other React code. So I'm going to hop on and plug our slash capacitor community. This is maintained by Ionic, but all of these developers and all these plugins are maintained by our great Ionic community. We have some great capacitor plugins, stuff like integrated with Stripe, stuff like our React hooks plugin, integrated with native audio or native video players, native HTTP. It's really powerful. Go ahead and check it out if you have a React web application that you want to make an Android and iOS application for. Again, I'm Thomas Vitis with Ionic and thank you so much for staying for my lightning talk and I'll be in the chat if you guys have any questions. Thanks.
7 min
21 Jun, 2022

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Workshops on related topic