Archived
- Add mobile/ directory with Expo React Native project - Create API client with JWT auth and token refresh using AsyncStorage - Implement AuthContext for login/register/logout flow - Add screens: Login, Register, Library, Search, Settings - Set up React Navigation with AuthStack and MainTabs - Create packages/shared/ with shared types and utilities - Add shared validation utilities (email, password strength) - Update root package.json workspaces to include mobile + shared - Add spec document docs/backend/009-expo-integration.md
33 lines
845 B
TypeScript
33 lines
845 B
TypeScript
import { useState, useEffect, useCallback } from "react";
|
|
|
|
/**
|
|
* Generic async data fetching hook for mobile screens.
|
|
*/
|
|
export function useAsyncData<T>(
|
|
fetcher: () => Promise<T>,
|
|
deps: unknown[] = [],
|
|
) {
|
|
const [data, setData] = useState<T | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<Error | null>(null);
|
|
|
|
const execute = useCallback(async () => {
|
|
setLoading(true);
|
|
setError(null);
|
|
try {
|
|
const result = await fetcher();
|
|
setData(result);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err : new Error(String(err)));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, deps);
|
|
|
|
useEffect(() => {
|
|
execute();
|
|
}, [execute]);
|
|
|
|
return { data, loading, error, refetch: execute };
|
|
} |