From ef0e475b04caf804793647ced5f71d62ec818d12 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Tue, 26 Apr 2022 13:26:37 +0200 Subject: [PATCH] Migrate `usePersistedState` hook to Typescript (#7739) * Migrate `usePersistedState` hook to Typescript * Use React types for setter functions Co-authored-by: Alf Eaton GitOrigin-RevId: e9ddf6e2ab45e0b0739c850a89ec7be0f14a7506 --- ...sisted-state.js => use-persisted-state.ts} | 23 +++++++++++-------- ...e.test.js => use-persisted-state.test.tsx} | 0 2 files changed, 13 insertions(+), 10 deletions(-) rename services/web/frontend/js/shared/hooks/{use-persisted-state.js => use-persisted-state.ts} (79%) rename services/web/test/frontend/shared/hooks/{use-persisted-state.test.js => use-persisted-state.test.tsx} (100%) diff --git a/services/web/frontend/js/shared/hooks/use-persisted-state.js b/services/web/frontend/js/shared/hooks/use-persisted-state.ts similarity index 79% rename from services/web/frontend/js/shared/hooks/use-persisted-state.js rename to services/web/frontend/js/shared/hooks/use-persisted-state.ts index e01abb97b8..44236577a0 100644 --- a/services/web/frontend/js/shared/hooks/use-persisted-state.js +++ b/services/web/frontend/js/shared/hooks/use-persisted-state.ts @@ -1,15 +1,18 @@ -import { useState, useCallback, useEffect } from 'react' +import { + useState, + useCallback, + useEffect, + SetStateAction, + Dispatch, +} from 'react' import localStorage from '../../infrastructure/local-storage' -/** - * @param {string} key - * @param {any} [defaultValue] - * @param {boolean} [listen] - * - * @returns {[any, function]} - */ -function usePersistedState(key, defaultValue, listen = false) { - const [value, setValue] = useState(() => { +function usePersistedState( + key: string, + defaultValue?: T, + listen = false +): [T, Dispatch>] { + const [value, setValue] = useState(() => { return localStorage.getItem(key) ?? defaultValue }) diff --git a/services/web/test/frontend/shared/hooks/use-persisted-state.test.js b/services/web/test/frontend/shared/hooks/use-persisted-state.test.tsx similarity index 100% rename from services/web/test/frontend/shared/hooks/use-persisted-state.test.js rename to services/web/test/frontend/shared/hooks/use-persisted-state.test.tsx