-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "Support direct handlers in useSubmit/fetcher.submit/f…
- Loading branch information
1 parent
4d58d0e
commit 97ec257
Showing
10 changed files
with
822 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
"@remix-run/router": minor | ||
--- | ||
|
||
- Add support for direct `action` functions to be passed to `router.navigate`. This allows you to skip the creation of a new route to handle the `action` , or you can also override the defined route `action` at the call-site. | ||
|
||
**Defining an `action` at the callsite:** | ||
|
||
```jsx | ||
let routes = [{ path: '/' }]; // No action on route | ||
|
||
// Custom actions will behave as a submission navigation to the current location | ||
router.navigate(null, { | ||
formMethod "post", | ||
formData: new FormData(), | ||
action() { | ||
// You may now define your custom action here | ||
} | ||
}) | ||
``` | ||
|
||
**Overriding an `action` at the call-site:** | ||
|
||
```jsx | ||
let routes = [{ path: '/', action: someAction }]; | ||
router.navigate(null, { | ||
formMethod "post", | ||
formData: new FormData(), | ||
action() { | ||
// This will be called instead of `someAction` | ||
} | ||
}) | ||
``` | ||
|
||
- Add support for direct `action`/`loader` functions to be passed to `router.fetch`. This allows you to skip the creation of a new route to handle the `loader` or `action`, or you can also override the defined route `loader` or `action` at the call-site. | ||
|
||
**Fetching to a direct loader without a defined route:** | ||
|
||
```jsx | ||
let routes = [{ path: "/", action: someAction }]; | ||
// Note no location required | ||
router.fetch("key", "0", null, { | ||
loader() { | ||
// Call this loader for the fetcher and avoid the need for a resource route | ||
}, | ||
}); | ||
``` | ||
|
||
**Fetching to a direct action without a defined route:** | ||
|
||
```jsx | ||
let routes = [{ path: '/', action: someAction }]; | ||
// Note no location required | ||
router.fetch("key", "0", null, { | ||
formMethod "post", | ||
formData: new FormData(), | ||
action() { | ||
// Call this action for the fetcher and avoid the need for a resource route | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
"react-router-dom": minor | ||
--- | ||
|
||
Add direct `action` function support to `useSubmit`/`fetcher.submit` and direct `loader` support to `fetcher.load`. This allows you to skip the creation of a new route to handle the `action` or `loader`. If both a call-site handler and a route-defined handler exist, the call-site handler will be used. | ||
|
||
**`useSubmit:`** | ||
|
||
```jsx | ||
let router = createBrowserRouter([ | ||
{ | ||
path: "/", | ||
Component() { | ||
let submit = useSubmit(); | ||
|
||
submit(data, { | ||
formMethod: "post", | ||
encType: null, | ||
action({ payload }) { | ||
// You may now define your action here | ||
}, | ||
}); | ||
}, | ||
}, | ||
]); | ||
``` | ||
|
||
**`fetcher.load`/`fetcher.submit`:** | ||
|
||
```jsx | ||
let router = createBrowserRouter([ | ||
{ | ||
path: "/", | ||
Component() { | ||
let fetcher = useFetcher(); | ||
|
||
fetcher.load(() => { | ||
// You may now define a loader here | ||
}); | ||
|
||
fetcher.submit(data, { | ||
formMethod: "post", | ||
encType: null, | ||
action({ payload }) { | ||
// You may now define your action here | ||
}, | ||
}); | ||
}, | ||
}, | ||
]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.