Last active
September 21, 2020 01:49
-
-
Save sankargorthi/dced9e4d8e4db905033357ef57d1b4f6 to your computer and use it in GitHub Desktop.
Debounced searchbox with persistent state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const useSearchConfig = () => useContext(SearchContext); | |
export const useSetSearchConfig = () => useContext(SetSearchContext); | |
export function Application({ children, changingParam }) { | |
const [searchConfig, setSearchConfig] = usePersistentState( | |
`MyComponent${changingParam}`, | |
{ searchTerm: '', sortDirection: ASC }, | |
) | |
return ( | |
<SearchContext.Provider value={searchConfig}> | |
<SetSearchContext.Provider value={setSearchConfig}> | |
<SlowSearchbox /> | |
<SearchDependent /> | |
</SetSearchContext.Provider> | |
</SearchContext.Provider> | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function SlowSearchbox() { | |
const { searchTerm } = useSearchConfig() | |
const setSearchConfig = useSetSearchConfig() | |
return <Searchbox value={term} onChange={(e, v) => setSearchConfig(c => ({...c, searchTerm: v})} /> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function SearchDependent() { | |
const { searchConfig: fast } = useSearchConfig(); | |
// Debounce here instead of in the searchbox | |
const searchConfig = useDebounce(slow); | |
const filtered = filter(data, searchConfig); | |
return <MainContent list={filtered} /> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function useDebounce(value, delay = 300 /* default delay */) { | |
const [debounced, setDebounced] = useState(value) | |
useEffect(() => { | |
const handler = setTimeout(() => setDebouncedValue(value), delay) | |
return () => clearTimeout(handler) | |
}, [value, delay]) | |
return debounced | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// returns `null` if no stored value exists | |
const readFromStorage = (key) => … | |
export default function usePersistentState(key, initialValue) { | |
const [value, setValue] = useState(() => readFromStorage(key) || initialValue) | |
useEffect(() => { | |
const saved = readFromStorage(key) | |
if (saved) setValue(saved) | |
}, [key]) | |
useEffect(() => { | |
writeToStorage(key, value) | |
}, [key, value]) | |
return [value, setValue] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment