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!
Using React Hooks + Capacitor to Access Native Mobile APIs
AI Generated Video Summary
Capacitor is a powerful tool built by Ionic that allows web applications to be compiled into native iOS and Android projects. It comes with a set of plugins, including the camera plugin, and has a React hooks package for seamless integration with React applications. The Talk includes a demo showcasing the camera hook button and the rendering of selected photos. The presenter emphasizes the use of useEffect and useState for additional functionality. The Talk concludes with a mention of the Capacitor community and gratitude for attending.
1. Introduction to Capacitor and React Hooks
Hello, my name is Thomas Vietas and I'm a Capacitor Developer Experience Engineer working at Ionic. 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 very powerful and comes with a set of plug-ins, including the camera plug-in. We also have a React hooks package that wraps all the official plugins, making them feel right at home in your React application.
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.
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.
2. Demo of Capacitor and React Hooks
Feels much more at home in a React hooks heavy application. Let's go on over to a demo I pre-prepared. We have our app and a custom component called our camera hook button. We trigger the camera and render the selected photo. I've compiled the iOS application and put it on the iOS simulator. We have our Hello React Summit application. Let's click the button and select a photo. We can set this up with use effect or use state for more functionality. Check out the github.com/capacitor community for more capacitor plugins. Thank you for staying for my Lightning Talk!
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 dot TSX that most applications have. We have this other custom component called our camera hook button, which let's hop on over there. 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. Let's hop on over to the goodies. We have in our slides example, we have photo and get photo, which calls our use camera hook. And from there we have trigger camera, which calls our use callback hook, which calls the, you can think of this like camera.getphoto, but getting this from the hook. It's just get photo, it's a hundred percent quality. You don't want to open the native editor with our result type, that's a data URL. Once we've selected, once we trigger our camera, we've flipped 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. So let's go ahead and click it, so we hit the trigger camera use callback hook. I'll select for more photos, and we'll pick this beautiful waterfall. And from there, tada! 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 can set this up with use effect so when we first load it, it will automatically trigger something. Or we can set it 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 github.com slash capacitor community. This is maintained by Ionic, but all these developers and all these plugins are maintained by our great Ionic community. We have some great capacitor plugins, stuff like Integrate with Stripe. Stuff like a React hooks plugin, 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 Betus 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.