-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[Doc] Impove scrollToTop in buttons doc and document _scrollToTop in useRedirect
#10449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
3ac4153
df90a5a
fdaf387
71e12c1
b24856a
bdc97be
466f2b8
15aa2b1
eaa085d
b19a98d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ title: "useRedirect" | |
|
|
||
| This hook returns a function that redirects the user to another page. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```jsx | ||
| import { useRedirect } from 'react-admin'; | ||
|
|
||
|
|
@@ -20,13 +22,14 @@ const DashboardButton = () => { | |
| ``` | ||
|
|
||
| The callback takes 5 arguments: | ||
| - The page to redirect the user to ('list', 'create', 'edit', 'show', a function or a custom path) | ||
| - The current `resource` | ||
| - The `id` of the record to redirect to (if any) | ||
| - A record-like object to be passed to the first argument, when the first argument is a function | ||
| - A `state` to be set to the location | ||
|
|
||
| Here are more examples of `useRedirect` calls: | ||
| - The page to redirect the user to ('list', 'create', 'edit', 'show', a function or a custom path) | ||
| - The current `resource` | ||
| - The `id` of the record to redirect to (if any) | ||
| - A record-like object to be passed to the first argument, when the first argument is a function | ||
| - A `state` to be set to the location | ||
|
|
||
| Here are more examples of `useRedirect` calls: | ||
|
|
||
| ```jsx | ||
| // redirect to the post list page | ||
|
|
@@ -35,14 +38,8 @@ redirect('list', 'posts'); | |
| redirect('edit', 'posts', 1); | ||
| // redirect to the post creation page: | ||
| redirect('create', 'posts'); | ||
| // redirect to the result of a function | ||
| redirect((resource, id, data) => { | ||
| return data.hasComments ? '/comments' : '/posts'; | ||
| }, 'posts', 1, { hasComments: true }); | ||
| // redirect to edit view with state data | ||
| redirect('edit', 'posts', 1, {}, { record: { post_id: record.id } }); | ||
| // do not redirect (resets the record form) | ||
| redirect(false); | ||
|
Comment on lines
-38
to
-45
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove those?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To isolate it in their own chapter |
||
| ``` | ||
|
|
||
| Note that `useRedirect` allows redirection to an absolute URL outside the current React app. | ||
|
|
@@ -68,4 +65,48 @@ const MyPageButton = () => { | |
| } | ||
| return <button onClick={handleClick}>My page</button>; | ||
| }; | ||
| ``` | ||
| ``` | ||
|
|
||
| ## Redirect function | ||
|
|
||
| `useRedirect` allows you to redirect to the result of a function as follows: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This deserves more examples as you can also return an object as expected by |
||
|
|
||
| ```jsx | ||
| redirect((resource, id, data) => { | ||
| return data.hasComments ? '/comments' : '/posts'; | ||
| }, 'posts', 1, { hasComments: true }); | ||
| ``` | ||
|
|
||
| Your function can also return an object containing a `pathname` and optionally some keys of [a `NavigateOptions` object](https://api.reactrouter.com/dev/interfaces/react_router.NavigateOptions.html). | ||
|
|
||
| ```jsx | ||
| redirect((resource, id, data) => { | ||
| return { | ||
| pathname: `/${resource}/1`, | ||
| state: { record: { id: 1, foo: 'bar' } }, | ||
| flushSync: true, | ||
| preventScrollReset: true, | ||
| replace: false, | ||
| viewTransition: true, | ||
| }; | ||
| }); | ||
| ``` | ||
|
|
||
| ## Disable Scroll To Top | ||
|
|
||
| By default, react-admin scrolls to top on each redirection. You can disable it by passing a `_scrollToTop: false` option in the 5th argument: | ||
|
|
||
| ```jsx | ||
| redirect(`/deals/${deal.id}/show`, undefined, undefined, undefined, { | ||
| _scrollToTop: false, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Reset the record form | ||
|
|
||
| `useRedirect` resets the record form, so you can use the `redirect` function to reset it without redirecting as follows: | ||
|
|
||
| ```jsx | ||
| // do not redirect (resets the record form) | ||
| redirect(false); | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.