UNPKG

@vtaits/react-filterlist-router-5

Version:

Integration of `@vtaits/react-filterlist` with `react-router-dom` v5

40 lines 1.18 kB
// src/useCreateDataStore.ts import { createEmitter, createStringBasedDataStore } from "@vtaits/filterlist/datastore/string"; import { useLatest } from "@vtaits/use-latest"; import { useCallback, useEffect, useRef, useState } from "react"; import { useHistory, useLocation } from "react-router-dom"; function useCreateDataStore(options) { const history = useHistory(); const { pathname, search } = useLocation(); const [emitter] = useState(() => createEmitter()); const historyRef = useLatest(history); const pathnameRef = useLatest(pathname); const searchRef = useLatest(search); const isInitRef = useRef(true); useEffect(() => { if (isInitRef.current) { isInitRef.current = false; return; } emitter.emit(); }, [search]); const createDataStore = useCallback( () => createStringBasedDataStore( () => searchRef.current, (nextSearch) => { historyRef.current.push(`${pathnameRef.current}?${nextSearch}`); }, emitter, options ), [emitter, historyRef, options, pathnameRef, searchRef] ); return createDataStore; } export { useCreateDataStore }; //# sourceMappingURL=index.js.map