From 94e9f7977ff592f61593ccce8e7e0f7c3df27979 Mon Sep 17 00:00:00 2001 From: rare-magma Date: Tue, 6 Aug 2024 21:59:46 +0200 Subject: [PATCH] fix: useffect dependencies on saveBudget() Signed-off-by: rare-magma --- src/hooks/useDB.ts | 56 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/hooks/useDB.ts b/src/hooks/useDB.ts index e27aa807..4983f22e 100644 --- a/src/hooks/useDB.ts +++ b/src/hooks/useDB.ts @@ -1,6 +1,6 @@ import { produce } from "immer"; import Papa from "papaparse"; -import React, { useEffect, useState } from "react"; +import React, { useCallback, useEffect, useState } from "react"; import { Option } from "react-bootstrap-typeahead/types/types"; import { useParams } from "react-router-dom"; import { Budget } from "../components/Budget/Budget"; @@ -422,34 +422,34 @@ export function useDB() { }); } - function saveBudget(budget: Budget | undefined) { - if (!budget) return; - let list: Budget[] = []; - budgetsDB - .setItem(budget.id, budget) - .then(() => { - budgetsDB - .iterate((value) => { - list = list.concat(value as Budget); - }) - .then(() => { - setBudgetList(list); - setBudgetNameList(createBudgetNameList(list)); - setNeedReload(true); - }) - .catch((e: unknown) => { - throw e; - }); - }) - .catch((e: unknown) => { - throw e; - }); - } + const saveBudget = useCallback( + (budget: Budget | undefined) => { + if (!budget) return; + let list: Budget[] = []; + budgetsDB + .setItem(budget.id, budget) + .then(() => { + budgetsDB + .iterate((value) => { + list = list.concat(value as Budget); + }) + .then(() => { + setBudgetList(list); + setBudgetNameList(createBudgetNameList(list)); + setNeedReload(true); + }) + .catch((e: unknown) => { + throw e; + }); + }) + .catch((e: unknown) => { + throw e; + }); + }, + [setBudgetList, setBudgetNameList, setNeedReload], + ); - // biome-ignore lint/correctness/useExhaustiveDependencies: - useEffect(() => { - saveBudget(budget); - }, [budget]); + useEffect(() => void saveBudget(budget), [budget, saveBudget]); return { createBudget,