mirror of
https://github.com/supabase/supabase.git
synced 2026-05-11 02:20:29 +08:00
## I have read the CONTRIBUTING.md file. YES ## What kind of change does this PR introduce? Docs update ## What is the current behavior? - The React quickstart example does not handle errors when fetching data. - The local development guide uses inconsistent capitalization for "docker". ## What is the new behavior? - Adds basic error handling (`error` check) in the React quickstart example to improve reliability. - Fixes capitalization from "docker" → "Docker" for consistency with official naming. ## Additional context These are small improvements to enhance clarity and developer experience in the docs. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Enhanced React quickstart guide with error handling for database queries to prevent invalid data processing * Corrected capitalization in local development guide <!-- end of auto-generated comment: release notes by coderabbit.ai -->
154 lines
3.8 KiB
Plaintext
154 lines
3.8 KiB
Plaintext
---
|
|
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'
|
|
hideToc: true
|
|
---
|
|
|
|
<StepHikeCompact>
|
|
|
|
<StepHikeCompact.Step step={1}>
|
|
|
|
<$Partial path="quickstart_db_setup.mdx" />
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={2}>
|
|
|
|
<StepHikeCompact.Details title="Create a React app">
|
|
|
|
- Create a React app using a [Vite](https://vitejs.dev/guide/) template.
|
|
|
|
<$Partial path="uiLibCta.mdx" />
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```bash name=Terminal
|
|
npm create vite@latest my-app -- --template react
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={3}>
|
|
<StepHikeCompact.Details title="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`.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```bash name=Terminal
|
|
cd my-app && npm install @supabase/supabase-js
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={4}>
|
|
<StepHikeCompact.Details title="Declare Supabase Environment Variables">
|
|
|
|
Create a `.env.local` file and populate with your Supabase connection variables:
|
|
|
|
<ProjectConfigVariables variable="url" />
|
|
<ProjectConfigVariables variable="publishable" />
|
|
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
<$CodeTabs>
|
|
|
|
```text name=.env.local
|
|
VITE_SUPABASE_URL=<SUBSTITUTE_SUPABASE_URL>
|
|
VITE_SUPABASE_PUBLISHABLE_KEY=<SUBSTITUTE_SUPABASE_PUBLISHABLE_KEY>
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
<$Partial path="api_settings_steps.mdx" variables={{ "framework": "react", "tab": "frameworks" }} />
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={5}>
|
|
<StepHikeCompact.Details title="Query data from the app">
|
|
|
|
Replace the contents of `App.jsx` to add a `getInstruments` function to fetch the data and display the query result to the page using a Supabase client.
|
|
|
|
</StepHikeCompact.Details>
|
|
<StepHikeCompact.Code>
|
|
|
|
```js name=src/App.jsx
|
|
import { useEffect, useState } from "react";
|
|
import { createClient } from "@supabase/supabase-js";
|
|
|
|
const supabase = createClient(import.meta.env.VITE_SUPABASE_URL, import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY);
|
|
|
|
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.name}>{instrument.name}</li>
|
|
))}
|
|
</ul>
|
|
);
|
|
}
|
|
|
|
export default App;
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={6}>
|
|
<StepHikeCompact.Details title="Start the app">
|
|
|
|
Run the development server, go to http://localhost:5173 in a browser and you should see the list of instruments.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```bash name=Terminal
|
|
npm run dev
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
</StepHikeCompact>
|
|
|
|
## 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)
|