Migrate usePersistedState hook to Typescript (#7739)

* Migrate `usePersistedState` hook to Typescript

* Use React types for setter functions

Co-authored-by: Alf Eaton <alf.eaton@overleaf.com>
GitOrigin-RevId: e9ddf6e2ab45e0b0739c850a89ec7be0f14a7506
This commit is contained in:
Miguel Serrano 2022-04-26 13:26:37 +02:00 committed by Copybot
parent ddb94d159a
commit ef0e475b04
2 changed files with 13 additions and 10 deletions

View file

@ -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<T>(
key: string,
defaultValue?: T,
listen = false
): [T, Dispatch<SetStateAction<T>>] {
const [value, setValue] = useState<T>(() => {
return localStorage.getItem(key) ?? defaultValue
})