memoize debounced function

This commit is contained in:
keyan 2023-10-05 21:14:57 -05:00
parent d1a7dca46b
commit 0a35bca650
2 changed files with 9 additions and 2 deletions

View File

@ -18,6 +18,7 @@ export function debounce (fn, time) {
export default function useDebounceCallback (fn, time, deps = []) {
const [args, setArgs] = useState([])
useEffect(debounce(() => fn(...args), time), [fn, time, args, ...deps])
const memoFn = useCallback(fn, deps)
useEffect(debounce(() => memoFn(...args), time), [memoFn, time, args, ...deps])
return useCallback((...args) => setArgs(args), [])
}

View File

@ -302,6 +302,12 @@ export function LnAddrWithdrawal () {
const [formSchema, setFormSchema] = useState(lnAddrSchema())
const onAddrChange = useDebounceCallback(async (formik, e) => {
if (!e?.target?.value) {
setAddrOptions(defaultOptions)
setFormSchema(lnAddrSchema())
return
}
let options
try {
options = await lnAddrOptions(e.target.value)
@ -313,7 +319,7 @@ export function LnAddrWithdrawal () {
setAddrOptions(options)
setFormSchema(lnAddrSchema(options))
}, 500, [lnAddrOptions, lnAddrSchema])
}, 500, [])
return (
<>