Hello, my name is Thomas Vietas and I'm a Capacitor Developer Experience Engineer working at Ionic and 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. Ahh! 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 plug-ins, 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 and we have tons of docs on how to build your own plug-ins as well as a huge community of existing plug-ins out there.
Using the Capacitor plug-in is pretty straightforward, so let's go over how to use the camera plug-in. We import our camera from the Capacitor camera package, and 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. Let's take a look at our camera object. A camera has a function object called getphoto. Getphoto 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 and make it feel really native. So let's hop on back to the codes. So 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 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 capacity of 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, and 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 use callback, which is a native React hook, and we have use camera, which is our capacity of community camera React hook. We have our result type that we had before from our capacitor camera package. We call our use camera hook to get our photo, which is our photo object, and our get photo, which calls that camera dot get photo function I was showing in the other example. You can think of this most similar to, like, use state, where we have our state and then our set state. Then we have this trigger camera function, which calls use callback, calls our camera dot get photo with the allow editing and what we want the result type to be.
Comments