From 2657469964e24ffbeb905024532120395f6e797c Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sun, 30 Jun 2024 18:55:39 -0400 Subject: move to Donetick Org, First commit frontend --- src/hooks/useStickyState.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/hooks/useStickyState.js (limited to 'src/hooks') diff --git a/src/hooks/useStickyState.js b/src/hooks/useStickyState.js new file mode 100644 index 0000000..237362a --- /dev/null +++ b/src/hooks/useStickyState.js @@ -0,0 +1,16 @@ +import { useEffect, useState } from 'react' + +const useStickyState = (defaultValue, key) => { + const [value, setValue] = useState(() => { + const stickyValue = window.localStorage.getItem(key) + return stickyValue !== null ? JSON.parse(stickyValue) : defaultValue + }) + + useEffect(() => { + window.localStorage.setItem(key, JSON.stringify(value)) + }, [key, value]) + + return [value, setValue] +} + +export default useStickyState -- cgit