Fix excessive re-rendering due to route changesd

This commit is contained in:
DaneEveritt 2022-06-20 13:19:40 -04:00
parent 7b0e2ce99d
commit 8bd518048e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
7 changed files with 51 additions and 27 deletions

View file

@ -0,0 +1,30 @@
import { useLocation } from 'react-router';
import { useMemo } from 'react';
export default () => {
const location = useLocation();
const getHashObject = (value: string): Record<string, string> =>
value
.substring(1)
.split('&')
.reduce((obj, str) => {
const [ key, value = '' ] = str.split('=');
return !str.trim() ? obj : { ...obj, [key]: value };
}, {});
const pathTo = (params: Record<string, string>): string => {
const current = getHashObject(location.hash);
for (const key in params) {
current[key] = params[key];
}
return Object.keys(current).map(key => `${key}=${current[key]}`).join('&');
};
const hash = useMemo((): Record<string, string> => getHashObject(location.hash), [ location.hash ]);
return { hash, pathTo };
};