Skip to content

Commit

Permalink
Update docs for useNavigate() (#10749)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jul 31, 2023
1 parent 868e515 commit a0d6c4f
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions docs/hooks/use-navigate.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ title: useNavigate

# `useNavigate`

<details>
<summary>Type declaration</summary>

```tsx
declare function useNavigate(): NavigateFunction;

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

interface NavigateOptions {
replace?: boolean;
state?: any;
preventScrollReset?: boolean;
relative?: RelativeRoutingType;
}

type RelativeRoutingType = "route" | "path";
```

</details>

<docs-warning>It's usually better to use [`redirect`][redirect] in [`loaders`][loaders] and [`actions`][actions] than this hook</docs-warning>

The `useNavigate` hook returns a function that lets you navigate programmatically, for example in an effect:
Expand All @@ -24,31 +47,47 @@ function useLogoutTimer() {
}
```

## Type Declaration
The `navigate` function has two signatures:

```tsx
declare function useNavigate(): NavigateFunction;
- Either pass a `To` value (same type as `<Link to>`) with an optional second `options` argument (similar to the props you can pass to [`<Link>`][link]), or
- Pass the delta you want to go in the history stack. For example, `navigate(-1)` is equivalent to hitting the back button

interface NavigateFunction {
(
to: To,
options?: {
replace?: boolean;
state?: any;
relative?: RelativeRoutingType;
}
): void;
(delta: number): void;
}
```
## `options.replace`

The `navigate` function has two signatures:
Specifying `replace: true` will cause the navigation will replace the current entry in the history stack instead of adding a new one.

## `options.state`

- Either pass a `To` value (same type as `<Link 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.
You may include an optional state value in to store in [history state][history-state]

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

When using the [`<ScrollRestoration>`][scrollrestoration] component, you can disable resetting the scroll to the top of the page via `options.preventScrollReset`

## `options.relative`

By default, navigation is relative to the route hierarchy, so `..` will go up one `Route` level. Occasionally, you may find that you have matching URL patterns that do not make sense to be nested, and you'd prefer to use relative _path_ routing. You can opt into this behavior with `relative: true`:

```jsx
// Contact and EditContact do not share additional UI layout
<Route path="/" element={<Layout />}>
<Route path="contacts/:id" element={<Contact />} />
<Route
path="contacts/:id/edit"
element={<EditContact />}
/>
</Route>;

function EditContact() {
// Since Contact is not a parent of EditContact we need to go up one level
// in the path, instead of one level in the Route hierarchy
navigate("..", { relative: "path" });
}
```

[link]: ../components/link
[redirect]: ../fetch/redirect
[loaders]: ../route/loader
[actions]: ../route/action
[history-state]: https://developer.mozilla.org/en-US/docs/Web/API/History/state
[scrollrestoration]: ../components/scroll-restoration

0 comments on commit a0d6c4f

Please sign in to comment.