From 2b532c7472b384c5ff6544ea02e0f7310053a7ba Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Sat, 18 Apr 2026 12:21:41 +0700 Subject: [PATCH] feat: add addTransaction function - Add addTransaction callback to hook - Use useCallback for performance - Save to AsyncStorage on add --- hooks/useTransactions.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/hooks/useTransactions.ts b/hooks/useTransactions.ts index 54760be..ba43690 100644 --- a/hooks/useTransactions.ts +++ b/hooks/useTransactions.ts @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useCallback } from 'react'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { Transaction } from '../types'; @@ -8,7 +8,7 @@ export const useTransactions = () => { const [transactions, setTransactions] = useState([]); const [loading, setLoading] = useState(true); - const loadTransactions = async () => { + const loadTransactions = useCallback(async () => { try { setLoading(true); const stored = await AsyncStorage.getItem(STORAGE_KEY); @@ -20,15 +20,24 @@ export const useTransactions = () => { } finally { setLoading(false); } - }; + }, []); useEffect(() => { loadTransactions(); + }, [loadTransactions]); + + const addTransaction = useCallback((transaction: Transaction) => { + setTransactions((prev) => { + const updated = [transaction, ...prev]; + AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(updated)); + return updated; + }); }, []); return { transactions, loading, + addTransaction, refresh: loadTransactions, }; }; \ No newline at end of file