Expo
Prerequisites
To use LiveStore with Expo, ensure your project has the New Architecture enabled. This is required for transactional state updates.
Option A: Quick start
For a quick start we recommend using our template app following the steps below.
For existing projects see Existing project setup.
-
Set up project from template
Replace
my-app
with your desired app name. -
Install dependencies
It’s strongly recommended to use
bun
for the simplest and most reliable dependency setup.bun install
You can ignore the peer-dependency warnings.
pnpm install --node-linker=hoisted
Make sure to use
--node-linker=hoisted
when installing dependencies in your project or add it to your.npmrc
file.Hopefully Expo will also support non-hoisted setups in the future.
Given
npm
doesn’t automatically install peer dependencies you’ll also need to also install the following peer dependencies:Going forward you can use
npm install
again to install the dependencies.When using
yarn
, make sure you’re using Yarn 4 or higher with thenode-modules
linker.Additionally, given
yarn
doesn’t automatically install peer dependencies you’ll also need to install the following peer dependencies:Pro tip: You can use direnv to manage environment variables.
-
Run the app
bun ios
orbun android
pnpm ios
orpnpm android
npm run ios
ornpm run android
yarn ios
oryarn android
Option B: Existing project setup
-
Install dependencies
-
Add Vite meta plugin to babel config file
LiveStore Devtools uses Vite. This plugin emulates Vite’s
import.meta.env
functionality.bun add -d babel-plugin-transform-vite-meta-env
pnpm add -D babel-plugin-transform-vite-meta-env
yarn add -D babel-plugin-transform-vite-meta-env
npm install --save-dev babel-plugin-transform-vite-meta-env
In your
babel.config.js
file, add the plugin as follows: -
Update Metro config
Add the following code to your
metro.config.js
file:
Define your schema
To define the data structure for your app, set up a schema that specifies the tables and fields your app uses.
-
In your project root, create a schema folder and inside it create a file named index.ts. This file defines the tables and data structures for your app.
-
In
index.ts
, define a table to represent a data model, such as atodos
.
Here’s an example:
Mutations
Create a file named mutations.ts
inside the schema
folder. This file stores the mutations your app uses to interact with the database.
A “mutation” is a function that encapsulates raw database queries. It ensures updates to your LiveStore are made in a safe, consistent, and reliable way. Mutations help prevent errors and keep your database operations robust.
Use the Schema
module from effect
along with defineMutation
and sql
from @livestore/livestore
to define these functions. These tools let you create fully typed mutations for secure and efficient database interactions.
Here’s an example:
Add the LiveStore Provider
To make the LiveStore available throughout your app, wrap your app’s root component with the LiveStoreProvider
component from @livestore/react
. This provider manages your app’s data store, loading, and error states.
Here’s an example:
Use a mutation
After wrapping your app with the LiveStoreProvider
, you can use the useStore
hook from any component to execute mutations.
Here’s an example:
Queries
To retrieve data from the database, first define a query using querySQL
from @livestore/livestore
. Then, execute the query with the useQuery
hook from @livestore/react
.
Consider abstracting queries into a separate file to keep your code organized, though you can also define them directly within components if preferred.
Here’s an example:
Devtools
To open the devtools, run the app and from your terminal press shift + m
, then select LiveStore Devtools and press Enter
.
This will open the devtools in a new tab in your default browser.
Use the devtools to inspect the state of your LiveStore database, execute mutations, track performance, and more.
Database location
With Expo Go
To open the database in Finder, run the following command in your terminal:
With development builds
For development builds, the app SQLite database is stored in the app’s Library directory.
Example:
/Users/<USERNAME>/Library/Developer/CoreSimulator/Devices/<DEVICE_ID>/data/Containers/Data/Application/<APP_ID>/Documents/SQLite/app.db
To open the database in Finder, run the following command in your terminal:
Replace [APP_BUNDLE_ID]
with your app’s bundle ID. e.g. dev.livestore.livestore-expo
.
Further notes
- LiveStore doesn’t yet support Expo Web (see #130)