Documentation
Configure the Firebase client SDK and realtime Fireclass hooks in a React application.
This guide uses @dharayush7/fireclass-react with the Firebase client SDK.
Register a Firebase Web app, then add its public configuration to
.env.local:
VITE_FIREBASE_API_KEY=your-api-key
VITE_FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
VITE_FIREBASE_PROJECT_ID=your-project-id
VITE_FIREBASE_APP_ID=your-app-idThese values identify the web app and are safe in the client bundle. Security comes from Firebase Authentication and Firestore Security Rules.
Create the Firebase entry before running the CLI:
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID,
};
const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);$ npx fireclass init
Choose React, keep src/lib/firebase.ts, and select the db export. The
CLI detects the existing file and leaves it untouched.
npx fireclass doctor$ npm install @dharayush7/fireclass-react firebase class-validator class-transformer reflect-metadata
import "reflect-metadata";
import { createFireclass } from "@dharayush7/fireclass-react";
import { db } from "./firebase";
export const { BaseModel, useQuery, useDoc, adapter } = createFireclass(db);Vite applications normally compile src with tsconfig.app.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}import { Collection } from "@dharayush7/fireclass-react";
import { IsBoolean, IsString } from "class-validator";
import { BaseModel } from "../lib/fireclass";
@Collection("todos")
export class Todo extends BaseModel<Todo> {
@IsString()
title!: string;
@IsBoolean()
done!: boolean;
constructor(data?: Partial<Todo>) {
super(data);
Object.assign(this, data);
}
}Use the initialized hooks with the model:
import { useQuery } from "./lib/fireclass";
import { Todo } from "./models/todo";
export function TodoList() {
const { data: todos, loading, error } = useQuery(Todo, {
orderBy: { title: "asc" },
});
if (loading) return <p>Loading...</p>;
if (error) return <p>{error.message}</p>;
return <ul>{todos.map((todo) => <li key={todo.id}>{todo.title}</li>)}</ul>;
}npx fireclass doctor
npm run build