diff --git a/src/useQueryParam.ts b/src/useQueryParam.ts index 328517f..859f89f 100644 --- a/src/useQueryParam.ts +++ b/src/useQueryParam.ts @@ -31,6 +31,24 @@ export const useQueryParam = ( ): [D2 | undefined, (newValue: D, updateType?: UrlUpdateType) => void] => { const { history, location } = React.useContext(QueryParamContext); + // ref for actually version history + const refHistory = React.useRef(history); + React.useEffect( + () => { + refHistory.current = history; + }, + [history] + ); + + // ref for actually version location + const refLocation = React.useRef(location); + React.useEffect( + () => { + refLocation.current = location; + }, + [location] + ); + // read in the raw query if (!rawQuery) { rawQuery = React.useMemo(() => parseQueryString(location.search) || {}, [ @@ -65,12 +83,12 @@ export const useQueryParam = ( updateUrlQuery( { [name]: newEncodedValue }, - location, - history, + refLocation.current, + refHistory.current, updateType ); }, - [location] + [] ); return [decodedValue, setValue];