Code Organization
Best practices for organizing your Fireact.dev codebase.
This section covers best practices for building production-ready SaaS applications with Fireact.dev.
Always use TypeScript for type safety:
// ✅ Good: Explicit types
interface UserData {
name: string;
email: string;
role: 'admin' | 'user';
}
const updateUser = async (userId: string, data: UserData): Promise<void> => {
// Implementation
};
// ❌ Bad: Using 'any'
const updateUser = async (userId: any, data: any): Promise<any> => {
// Implementation
};
Always handle potential errors:
// ✅ Good: Proper error handling
try {
const result = await functionCall();
return result;
} catch (error) {
console.error('Operation failed:', error);
throw new Error('User-friendly error message');
}
// ❌ Bad: No error handling
const result = await functionCall();
return result;
Use React best practices for performance:
// ✅ Good: Memoization
const ExpensiveComponent = React.memo(({ data }) => {
const processedData = useMemo(() => {
return expensiveCalculation(data);
}, [data]);
return <div>{processedData}</div>;
});
// ❌ Bad: No optimization
const ExpensiveComponent = ({ data }) => {
const processedData = expensiveCalculation(data); // Runs on every render
return <div>{processedData}</div>;
};
Implement proper security measures:
// ✅ Good: Check permissions
const deleteSubscription = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('unauthenticated', 'Must be logged in');
}
const isOwner = await checkSubscriptionOwnership(
context.auth.uid,
data.subscriptionId
);
if (!isOwner) {
throw new functions.https.HttpsError('permission-denied', 'Not authorized');
}
// Proceed with deletion
});
// ❌ Bad: No permission checks
const deleteSubscription = functions.https.onCall(async (data, context) => {
await admin.firestore().collection('subscriptions').doc(data.subscriptionId).delete();
});
Keep code clean and maintainable:
// ✅ Good: Small, focused functions with clear names
const validateEmail = (email: string): boolean => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
const sendWelcomeEmail = async (userEmail: string): Promise<void> => {
if (!validateEmail(userEmail)) {
throw new Error('Invalid email address');
}
// Send email logic
};
// ❌ Bad: Large function doing multiple things
const processUser = async (data: any) => {
// Validation
if (!data.email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email)) {
throw new Error('Invalid email');
}
// Database operation
await db.collection('users').doc(data.id).set(data);
// Send email
await emailService.send(data.email, 'Welcome!');
// Log event
console.log('User processed:', data.id);
};
Have best practices to share? See our Contributing Guide to submit your suggestions.
Best practices for organizing your Fireact.dev codebase.
Best practices for optimizing performance in Fireact applications.
Best practices for error handling and recovery in Fireact applications.
Best practices for testing Fireact applications including unit, integration, and E2E tests.
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.