React Native Supabase Integration: A Comprehensive Guide for Expo Projects
Fernando Chaves
August 4, 2024

npx create-expo-app my-supabase-react-native-app
cd my-supabase-react-native-app
npx expo install @supabase/supabase-js
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'YOUR_SUPABASE_PROJECT_URL';
const supabaseKey = 'YOUR_SUPABASE_PUBLIC_API_KEY';
export const supabase = createClient(supabaseUrl, supabaseKey);
import React, { useEffect, useState } from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';
import { supabase } from './supabase';
export default function UserList() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetchUsers();
}, []);
async function fetchUsers() {
try {
setLoading(true);
const { data, error } = await supabase
.from('users')
.select('*')
.limit(10);
if (error) throw error;
setUsers(data);
} catch (error) {
console.error('Error fetching users:', error.message);
} finally {
setLoading(false);
}
}
return (
<View style={styles.container}>
<Text style={styles.title}>User List</Text>
{loading ? (
<Text>Loading users...</Text>
) : (
<FlatList
data={users}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<Text style={styles.userItem}>{item.name}</Text>
)}
/>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#f0f0f0',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
},
userItem: {
fontSize: 16,
marginBottom: 10,
padding: 10,
backgroundColor: '#ffffff',
borderRadius: 5,
},
});
import { supabase } from './supabase';
async function signUp(email, password) {
try {
const { user, error } = await supabase.auth.signUp({
email: email,
password: password,
});
if (error) throw error;
console.log('User signed up successfully:', user);
return user;
} catch (error) {
console.error('Error signing up:', error.message);
return null;
}
}