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
FAQ
Yes, Capacitor is open source and MIT licensed.
Unlike React Native and Flutter, Capacitor takes your HTML, CSS, and JavaScript and puts it in an iOS or Android project. If it works on the web, it'll work with Capacitor.
Capacitor offers plugins for accessing storage, the file system, device information, and the camera, among others. It also provides an API for communication via a JavaScript bridge.
You can use the camera plugin in Capacitor by importing the camera from the Capacitor camera package and using the getPhoto function to take a new photo or access the gallery.
The useCamera hook is part of the Capacitor community's React hooks package. It wraps the Capacitor camera plugin to make it feel right at home in a React application that uses hooks heavily.
The getPhoto function in the Capacitor camera plugin allows you to take a new photo or access the gallery, and set parameters like quality and whether to open the native editor.
The Capacitor community GitHub organization is maintained by Ionic and various developers. It hosts a variety of plugins like Integrate with Stripe, native audio/video players, and native HTTP.
Yes, you can test Capacitor applications on an iOS simulator, which comes bundled with Xcode and is available for free on any Mac.
Thomas Vietas is 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 them down to a native iOS project or a native Android project.
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.
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.
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.
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
Contents: This workshop is aimed at beginner developers that have an existing web application, or are interested in mobile development. We will go over:- What is Capacitor- How does it compare to other cross-platform solutions- Using Capacitor to build a native application using your existing web code- Tidying up our application for distribution on mobile app stores with naming conventions, icons, splash screens and more
Table of contents:- Intro to Neo4j AuraDB- Building GraphQL APIs with the Neo4j GraphQL Library- Intro to SwiftUI- SwiftUI + GraphQL
PrerequisitesTo follow along during the workshop attendees will need a Mac laptop with a recent version of Xcode installed. Some familiarity with Swift and iOS app development will be helpful, although not required.
This workshop is aimed at intermediate developers that have an existing React application, or are interested in mobile development with React. We will go over:
What is CapacitorHow does it compare to other cross-platform solutionsUsing Capacitor to build a native application using your existing web codeTidying up our application for distribution on mobile app stores with naming conventions, icons, splashscreens and more.
Comments