useLayoutEffect, useRef, and simply deps

Signed-off-by: Chris Suich <csuich2@gmail.com>
This commit is contained in:
Chris Suich
2025-05-21 14:45:59 -04:00
parent 5476046fc5
commit fcd29a338e
@@ -14,7 +14,13 @@
* limitations under the License.
*/
import { useCallback, useEffect, useState } from 'react';
import {
useCallback,
useEffect,
useLayoutEffect,
// useRef,
useState,
} from 'react';
import useMediaQuery from '@material-ui/core/useMediaQuery';
import { useTheme } from '@material-ui/core/styles';
@@ -55,23 +61,17 @@ const MOBILE_MEDIA_QUERY = 'screen and (max-width: 76.1875em)';
// current location in the history. This should only happen on the initial load so
// navigating to the root of the docs doesn't also redirect.
const useInitialRedirect = (defaultPath?: string) => {
const [hasRun, setHasRun] = useState(false);
// const hasRun = useRef(false);
const location = useLocation();
const navigate = useNavigate();
const { '*': currPath = '' } = useParams();
useEffect(() => {
// Only run once
if (hasRun) {
return;
}
setHasRun(true);
useLayoutEffect(() => {
if (currPath === '' && defaultPath !== '') {
navigate(`${location.pathname}${defaultPath}`, { replace: true });
}
}, [hasRun, currPath, defaultPath, location, navigate]);
}, []); // eslint-disable-line react-hooks/exhaustive-deps
};
/**