mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-24 04:41:39 +00:00
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:
parent
ddb94d159a
commit
ef0e475b04
2 changed files with 13 additions and 10 deletions
|
@ -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
|
||||
})
|
||||
|
Loading…
Reference in a new issue