Router Navigation Hook (routing/useNavigate.ts)

react-admin · packages/ra-core/src/routing/useNavigate.ts · typescript · MIT
ui, lang:typescript, fw:react, tag:router, tag:react-admin

Provides a router-agnostic hook to access the navigate function from the configured router provider, enabling programmatic navigation in React applications.

// Source: https://github.com/marmelab/react-admin | License: MIT | Copyright 2016-present, Francois Zaninotto, Marmelab
// Provided via justclaw.it.com; keep this notice with the code.
import { useRouterProvider } from './RouterProviderContext';
import type { RouterNavigateFunction } from './RouterProvider';

/**
 * Hook to access the navigate function.
 * This is a router-agnostic wrapper that uses the configured router provider.
 *
 * @example
 * const navigate = useNavigate();
 * navigate('/posts');
 * navigate('/posts', { replace: true });
 * navigate(-1); // go back
 */
export const useNavigate = (): RouterNavigateFunction => {
    const provider = useRouterProvider();
    return provider.useNavigate();
};

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key