If you are not familiar with expo, it's a client that helps you build React Native apps with less build complexity. It also helps you deal with the stress of installing and setting up your environment to run React Native.
In this tutorial, we will be building a simple camera app in which the user can take pictures, see previews of their pictures, use flash mode, and switch between the front and the back cameras.
On Xcode 11, it got stuck after installing expo, but app didn't install. Work around: Open expo, click on the + sign on the top right and enter the URL of the app manually. It works perfectly and syncs real-time. I downgraded to Xcode 10.3 (latest stable version) and iOS 12 simulator. Expo installed and process got hung after that. Click here to download the Expo client app to your Android device. Open Expo then click the 'Scan QR Code' and Scan the QR code in our terminal. As soon as the QR code got scanned, it'll automatically load and bundle/compile your project to run it in your device. Expo Project Productivity. Add to Wishlist. Start building rich experiences with just your Android device and your computer. Expo is a developer tool for creating experiences with interactive gestures and graphics, using JavaScript and React Native. To learn more visit Some programming experience is recommended. Expo-cli will automatically update your apps in simulators if you delete the existing apps, or you can run expo client:install:ios and expo client:install:android. If you built a standalone app previously, remember that you'll need to create a new build in order to update the SDK version.
Prerequisites
Expo doesn't require much to start building your first React Native app. You can learn more about installing expo and the expo-cli here in the docs.
Note: in this tutorial, I will be using macOS and iOS. You can use Android as well, there is not much difference when using expo at this point.
You can install expo and expo-cli globally by running the following command:
Expo requires Nodejs in order to run. You can run the latest version on the official website here.
Getting Started
After you've installed Expo and Nodejs, you can start bootstrapping a new Expo project with the command below:
How to install the packages and run the app
Expo provides us with a client app where we can run and see the preview of the app we are building. It's available on both the App Store and Google Play to download.
This is the interface of the app.
How to init an expo project
Go the app directory and run the app.
You will be asked a few questions to select the default template for the app. In this tutorial we simply select a blank (TypeScript) option, but again you are free to choose what is right for you.
Run the app
After bootstrapping the project, we can run the app with expo run
This will open a window in your browser where you can see the logs. It will also generate a QR code that you can scan to run the app on your device.
The good thing about expo is that you don't need to install and configure the simulators to run the app. It still gives you the option to run expo on the simulator, but you have to install and configure the simulator by yourself.
Back to our app. Assuming you've successfully run the app on the device, this will be the default screen:
Open the app directory in your favorite code editor. I'm using VS Code.
The App.tsx
will look like this:
Install Expo Client
How to create the UI
After having the project running, now it's time to start creating some UI.
Install expo-camera
The next step is to install expo-camera, like this:
We will create a simple UI that will allow the user to start the process of using the camera.
It's a simple UI: we import TouchableOpacity
for the button and do some simple styling. If you are wondering how styling works in React Native, you can check out my two articles here:
Now we have to use a useState
hook to manage the state and display the camera view when the user press the take picture button.
There are two important things we have to do when the user presses the button:
- Ask permission to access the camera. In mobile development, accessing many native APIs and mobile features is often restricted by the user's permissions and the privacy. It's just something that you have to get used to when developing mobile apps.
- Change the state and present the camera.
Let's import the camera module from expo-camera
with this command:
And add the camera view, like this:
We can use ref
to access the camera's methods:
When the take picture
button is pressed the __startCamera
function will be called:
The function will ask for permission first. If the user grant access to the camera, we can proceed and open the camera. If not, we show a simple alert.
Add the camera component
Let's display the camera when the user grants access to the device's camera.
Install Expo Client Login
We have to make some changes to the UI and add a conditional rendering. We display the camera only when the user requests it, otherwise we display the default screen.
Cool, now we need to add a button so we can take the actual picture.
Add the capture button
This is a simple View
inside the camera view that has an absolute position. So we make sure that it is always on the top of the camera.
How to take a picture
The app should take a picture when capture button is pressed. Os x on android. That function will look like the below:
Let's display the camera when the user grants access to the device's camera.
Install Expo Client Login
We have to make some changes to the UI and add a conditional rendering. We display the camera only when the user requests it, otherwise we display the default screen.
Cool, now we need to add a button so we can take the actual picture.
Add the capture button
This is a simple View
inside the camera view that has an absolute position. So we make sure that it is always on the top of the camera.
How to take a picture
The app should take a picture when capture button is pressed. Os x on android. That function will look like the below:
Internet plus sur. First, we check that we have access to the Camera
component using ref
:
Then we take the picture by calling the takePictureAsync
method. It returns a promise and an object that contains the picture's details. The result will look like this:
We are only interested in the Picture URL uri
. After we take a picture, we have to show the photo preview and hide the camera view. To do that we will use two hooks to change the state:
setPreviewVisible
to show the previewsetCapturedImage(photo)
to store the object result
Then we display the preview like this:
The CameraPreview
component looks like this:
And the result looks like this:
How to re-take a picture
We can add some buttons to the preview that will allow the user to perform more actions. For example, they could re-take the photo or save it.
Add the savePhoto
and retakePicture
props to the CameraPreview
component like this:
When the Re-take
button is pressed, we will have to hide the preview, remove the current picture, and show the camera again. Do that with the following code:
How to add other options – back camera, flash, and more
3d character creator for mac. expo-camra offers many options for customizing the camera, like FlashMode, setting the Camera type (front/back), zooming, and so on.
How to add FlashMode
Let's add an option so the user can turn FlashMode on and off:
We simply create a small button to switch off/on the flash, like this:
And we just change the state when the button is pressed:
And then we add FlashMode props:
How to access the front and the back camera
We will add a button that switches between the back and front camera.
We can get the default camera type directly from the camera module like below:
Add type
props like this:
And add the switch button:
And switch function:
Here is the result:
You can find the full source code on GitHub.
Wrapping up
Install Client Sccm
In general, Expo is an amazing tool that can save you a lot of time. It helps you start building directly and saves you the pain of environment setup.
Sometimes you may want to build a native extension and handle using native features in your own way. In this case I'd recommend using the react-native CLI so you can modify and play with native code easily.
Hi, my name is Said Hayani. I created subscribi.io to help creators, bloggers and influencers grow their audience through the newsletter.
Expo Client Ios
Join my Mailing list if you are interested in reading more about React Native.