feat: create useTransactions hook
- Add useState for transactions array - Add useEffect to load from AsyncStorage - Add loading state
This commit is contained in:
parent
0f9a502289
commit
9e41d16f8a
34
hooks/useTransactions.ts
Normal file
34
hooks/useTransactions.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { Transaction } from '../types';
|
||||
|
||||
const STORAGE_KEY = '@finance_transactions';
|
||||
|
||||
export const useTransactions = () => {
|
||||
const [transactions, setTransactions] = useState<Transaction[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const loadTransactions = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const stored = await AsyncStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
setTransactions(JSON.parse(stored));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading transactions:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadTransactions();
|
||||
}, []);
|
||||
|
||||
return {
|
||||
transactions,
|
||||
loading,
|
||||
refresh: loadTransactions,
|
||||
};
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user