From 9e41d16f8ae7ea91b8a1efc90e5f01a5afa0a83d Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Sat, 18 Apr 2026 12:21:28 +0700 Subject: [PATCH] feat: create useTransactions hook - Add useState for transactions array - Add useEffect to load from AsyncStorage - Add loading state --- hooks/useTransactions.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 hooks/useTransactions.ts diff --git a/hooks/useTransactions.ts b/hooks/useTransactions.ts new file mode 100644 index 0000000..54760be --- /dev/null +++ b/hooks/useTransactions.ts @@ -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([]); + 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, + }; +}; \ No newline at end of file