-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alternate submission encodings (#10413)
- Loading branch information
1 parent
f4c7c80
commit c8c417d
Showing
21 changed files
with
1,811 additions
and
384 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,44 @@ | ||
--- | ||
"@remix-run/router": minor | ||
--- | ||
|
||
Add support for `application/json` and `text/plain` encodings for `router.navigate`/`router.fetch` submissions. To leverage these encodings, pass your data in a `body` parameter and specify the desired `formEncType`: | ||
|
||
```js | ||
// By default, the encoding is "application/x-www-form-urlencoded" | ||
router.navigate("/", { | ||
formMethod: "post", | ||
body: { key: "value" }, | ||
}); | ||
|
||
function action({ request }) { | ||
// request.formData => FormData instance with entry [key=value] | ||
// request.text => "key=value" | ||
} | ||
``` | ||
|
||
```js | ||
// Pass `formEncType` to opt-into a different encoding | ||
router.navigate("/", { | ||
formMethod: "post", | ||
formEncType: "application/json", | ||
body: { key: "value" }, | ||
}); | ||
|
||
function action({ request }) { | ||
// request.json => { key: "value" } | ||
// request.text => '{ "key":"value" }' | ||
} | ||
``` | ||
|
||
```js | ||
router.navigate("/", { | ||
formMethod: "post", | ||
formEncType: "text/plain", | ||
body: "Text submission", | ||
}); | ||
|
||
function action({ request }) { | ||
// request.text => "Text submission" | ||
} | ||
``` |
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,55 @@ | ||
--- | ||
"react-router-dom": minor | ||
--- | ||
|
||
Add support for `application/json` and `text/plain` encodings for `useSubmit`/`fetcher.submit`. To reflect these additional types, `useNavigation`/`useFetcher` now also contain `navigation.json`/`navigation.text` and `fetcher.json`/`fetcher.text` which are getter functions mimicking `request.json` and `request.text`. Just as a `Request` does, if you access one of these methods for the incorrect encoding type, it will throw an Error (i.e. accessing `navigation.formData` when `navigation.formEncType` is `application/json`). | ||
|
||
```jsx | ||
// The default behavior will still serialize as FormData | ||
function Component() { | ||
let navigation = useNavigation(); | ||
let submit = useSubmit(); | ||
submit({ key: "value" }); | ||
// navigation.formEncType => "application/x-www-form-urlencoded" | ||
// navigation.formData => FormData instance | ||
// navigation.text => "key=value" | ||
} | ||
|
||
function action({ request }) { | ||
// request.headers.get("Content-Type") => "application/x-www-form-urlencoded" | ||
// request.formData => FormData instance | ||
// request.text => "key=value" | ||
} | ||
``` | ||
|
||
```js | ||
// Opt-into JSON encoding with `encType: "application/json"` | ||
function Component() { | ||
let submit = useSubmit(); | ||
submit({ key: "value" }, { encType: "application/json" }); | ||
// navigation.formEncType => "application/json" | ||
// navigation.json => { key: "value" } | ||
// navigation.text => '{"key":"value"}' | ||
} | ||
|
||
function action({ request }) { | ||
// request.headers.get("Content-Type") => "application/json" | ||
// request.json => { key: "value" } | ||
// request.text => '{"key":"value"}' | ||
} | ||
``` | ||
|
||
```js | ||
// Opt-into JSON encoding with `encType: "application/json"` | ||
function Component() { | ||
let submit = useSubmit(); | ||
submit("Text submission", { encType: "text/plain" }); | ||
// navigation.formEncType => "text/plain" | ||
// navigation.text => "Text submission" | ||
} | ||
|
||
function action({ request }) { | ||
// request.headers.get("Content-Type") => "text/plain" | ||
// request.text => "Text submission" | ||
} | ||
``` |
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
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
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.