Skip to main content

useNavigate

declare function useNavigate(): NavigateFunction;

interface NavigateFunction {
(to: To, options?: NavigateOptions): void;

(delta: number): void;
}

interface NavigateOptions {
replace?: boolean;
state?: any;
statusCode?: HttpCode.CODE_301 | HttpCode.CODE_302;
}

The useNavigate hook returns a function that lets you navigate programmatically, for example in an effect:

import { useNavigate } from '@archibald/core';

function useLogoutTimer() {
const userIsInactive = useFakeInactiveUser();
const navigate = useNavigate();

useEffect(() => {
if (userIsInactive) {
fake.logout();
navigate('/session-timed-out');
}
}, [userIsInactive]);
}

The navigate function has two signatures:

  • Either pass a To value (same type as <RouterLink to>) with an optional second { replace, state } arg or
  • Pass the delta you want to go in the history stack. For example, navigate(-1) is equivalent to hitting the back button.

If using replace: true, the navigation will replace the current entry in the history stack instead of adding a new one.