React Native Quickstart
Learn how to query data from Convex in a React Native app.
- Create a React Native app
Create a React Native app using the
npx create-expo-app
command.npx create-expo-app my-app
- Install the Convex client and server library
To get started, install the
convex
package which provides a convenient interface for working with Convex from a React app.Navigate to your app and install
convex
.cd my-app && npm install convex
- Set up a Convex dev deployment
Next, run
npx convex dev
. This will prompt you to log in with GitHub, create a project, and save your production and deployment URLs.It will also create a
convex/
folder for you to write your backend API functions in. Thedev
command will then continue running to sync your functions with your dev deployment in the cloud.npx convex dev
- Create sample data for your database
Create a
sampleData.jsonl
file with some sample data.sampleData.jsonl{"text": "Buy groceries", "isCompleted": true}
{"text": "Go for a swim", "isCompleted": true}
{"text": "Integrate Convex", "isCompleted": false} - Add the sample data to your database
Now that your project is ready, add a
tasks
table with the sample data into your Convex database with theimport
command.npx convex import --table tasks sampleData.jsonl
- Expose a database query
Add a new file
tasks.ts
in theconvex/
folder with a query function that loads the data.Exporting a query function from this file declares an API function named after the file and the export name,
api.tasks.get
.convex/tasks.tsimport { query } from "./_generated/server";
export const get = query({
args: {},
handler: async (ctx) => {
return await ctx.db.query("tasks").collect();
},
}); - Reset the Expo project
If you haven't done so yet, reset the Expo project to get a fresh
app
directory.npm run reset-project
- Connect the app to your backend
In
_layout.tsx
, create aConvexReactClient
and pass it to aConvexProvider
wrapping your component tree.app/_layout.tsximport { ConvexProvider, ConvexReactClient } from "convex/react";
import { Stack } from "expo-router";
const convex = new ConvexReactClient(process.env.EXPO_PUBLIC_CONVEX_URL!, {
unsavedChangesWarning: false,
});
export default function RootLayout() {
return (
<ConvexProvider client={convex}>
<Stack>
<Stack.Screen name="index" />
</Stack>
</ConvexProvider>
);
} - Display the data in your app
In
index.tsx
use theuseQuery
hook to fetch from yourapi.tasks.get
API.app/index.tsximport { api } from "@/convex/_generated/api";
import { useQuery } from "convex/react";
import { Text, View } from "react-native";
export default function Index() {
const tasks = useQuery(api.tasks.get);
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}
>
{tasks?.map(({ _id, text }) => <Text key={_id}>{text}</Text>)}
</View>
);
} - Start the app
Start the app, scan the provided QR code with your phone, and see the serialized list of tasks in the center of the screen.
npm start
Are you trying to build a universal app targeting both browsers and mobile
devices? Use npm create tamagui
and apply the steps above to the apps/expo
directory and the steps in the Next.js Quickstart
to the apps/next
directory.