Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

3.2 KB
Raw Blame
---
title: 'Use Supabase with React'
subtitle: 'Learn how to create a Supabase project, add some sample data to your database, and query the data from a React app.'
breadcrumb: 'Framework Quickstarts'
---

<AiPrompt id="reactjs" />

<$Partial path="quickstart_db_setup.mdx" />

## 3. Create a React app

Create a React app using a [Vite](https://vitejs.dev/guide/) template.

```bash
npm create vite@latest my-app -- --template react
```

## 4. Set up AI tooling (optional)

<$Partial path="quickstart_ai_tooling.mdx" />

## 5. Install the Supabase client library

The fastest way to get started is to use the `supabase-js` client library, which provides a convenient interface for working with Supabase from a React app.

Navigate to the React app and install `supabase-js`.

```bash
cd my-app && npm install @supabase/supabase-js
```

## 6. Declare Supabase environment variables

Create a `.env.local` file and populate it with your Supabase URL and publishable key that you can get from the helper below, or [from the project **Connect** panel](/dashboard/project/_?showConnect=true&framework=react&connectTab=frameworks)

<Button variant="primary" asChild>
  <a href="/dashboard/project/_?showConnect=true&connectTab=frameworks&framework=react">
    Open Connect panel
  </a>
</Button>

```dotenv name=.env.local
VITE_SUPABASE_URL=<SUBSTITUTE_SUPABASE_URL>
VITE_SUPABASE_PUBLISHABLE_KEY=<SUBSTITUTE_SUPABASE_PUBLISHABLE_KEY>
```

<$Partial path="api_settings.mdx" variables={{ "framework": "react", "tab": "frameworks" }} />

## 7. Create the Supabase client

Create a `src/lib` directory in your React app, create a file called `supabaseClient.js`, and add the following code to initialize the Supabase client:

```js name=src/lib/supabaseClient.js
import { createClient } from '@supabase/supabase-js'

const supabaseUrl = import.meta.env.VITE_SUPABASE_URL
const supabasePublishableKey = import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY

export const supabase = createClient(supabaseUrl, supabasePublishableKey)
```

## 8. Query data from the app

Replace the contents of `App.jsx` with a `getInstruments` function that fetches the data and displays the query result on the page.

```js name=src/App.jsx
import { useEffect, useState } from 'react'

import { supabase } from './lib/supabaseClient'

function App() {
  const [instruments, setInstruments] = useState([])

  useEffect(() => {
    getInstruments()
  }, [])

  async function getInstruments() {
    const { data, error } = await supabase.from('instruments').select()

    if (error) {
      console.error(error)
      return
    }

    setInstruments(data)
  }

  return (
    <ul>
      {instruments.map((instrument) => (
        <li key={instrument.id}>{instrument.name}</li>
      ))}
    </ul>
  )
}

export default App
```

## 9. Start the app

Run the development server, go to http://localhost:5173 in a browser, and you should see the list of instruments.

```bash
npm run dev
```

<$Partial path="quickstart_going_to_production.mdx" />

## Next steps

- Set up [Auth](/docs/guides/auth) for your app
- [Insert more data](/docs/guides/database/import-data) into your database
- Upload and serve static files using [Storage](/docs/guides/storage)
- Explore [drop-in UI components](/ui) for your Supabase app