diff --git a/docs/Buttons.md b/docs/Buttons.md index 8274bfd0a9..7b88af04cf 100644 --- a/docs/Buttons.md +++ b/docs/Buttons.md @@ -599,6 +599,14 @@ const PostList = () => ( It also supports [all the other `; }; -``` \ No newline at end of file +``` + +## Redirect function + +`useRedirect` allows you to redirect to the result of a function as follows: + +```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); +```