-
-
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 "Add better control over submission serialization (#10342
)"" This reverts commit f92aa2e.
- Loading branch information
1 parent
1f294a3
commit 4d58d0e
Showing
14 changed files
with
1,115 additions
and
140 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,20 @@ | ||
--- | ||
"@remix-run/router": minor | ||
--- | ||
|
||
Add support for a new `payload` parameter for `router.navigate`/`router.fetch` submissions. This allows you to submit data to an `action` without requiring serialization into a `FormData` instance. This `payload` value will be passed unaltered to your `action` function. | ||
|
||
```js | ||
router.navigate("/", { payload: { key: "value" } }); | ||
|
||
function action({ request, payload }) { | ||
// payload => { key: 'value' } | ||
// request.body => null | ||
} | ||
``` | ||
|
||
You may also opt-into serialization of this `payload` into your `request` using the `formEncType` parameter: | ||
|
||
- `formEncType: "application/x-ww-form-urlencoded"` => serializes into `request.formData()` | ||
- `formEncType: "application/json"` => serializes into `request.json()` | ||
- `formEncType: "text/plain"` => serializes into `request.text()` |
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,105 @@ | ||
--- | ||
"react-router-dom": minor | ||
--- | ||
|
||
- Support better submission and control of serialization of raw payloads through `useSubmit`/`fetcher.submit`. The default `encType` will still be `application/x-www-form-urlencoded` as it is today, but actions will now also receive a raw `payload` parameter when you submit a raw value (not an HTML element, `FormData`, or `URLSearchParams`). | ||
|
||
The default behavior will still serialize into `FormData`: | ||
|
||
```jsx | ||
function Component() { | ||
let submit = useSubmit(); | ||
submit({ key: "value" }); | ||
// navigation.formEncType => "application/x-www-form-urlencoded" | ||
// navigation.formData => FormData instance | ||
// navigation.payload => { key: "Value" } | ||
} | ||
|
||
function action({ request, payload }) { | ||
// request.headers.get("Content-Type") => "application/x-www-form-urlencoded" | ||
// request.formData => FormData instance | ||
// payload => { key: 'value' } | ||
} | ||
``` | ||
|
||
You may opt out of this default serialization using `encType: null`: | ||
|
||
```jsx | ||
function Component() { | ||
let submit = useSubmit(); | ||
submit({ key: "value" }, { encType: null }); | ||
// navigation.formEncType => null | ||
// navigation.formData => undefined | ||
// navigation.payload => { key: "Value" } | ||
} | ||
|
||
function action({ request, payload }) { | ||
// request.headers.get("Content-Type") => null | ||
// request.formData => undefined | ||
// payload => { key: 'value' } | ||
} | ||
``` | ||
|
||
_Note: we plan to change the default behavior of `{ encType: undefined }` to match this "no serialization" behavior in React Router v7. In order to better prepare for this change, we encourage developers to add explicit content types to scenarios in which they are submitting raw JSON objects:_ | ||
|
||
```jsx | ||
function Component() { | ||
let submit = useSubmit(); | ||
|
||
// Change this: | ||
submit({ key: "value" }); | ||
|
||
// To this: | ||
submit({ key: "value" }, { encType: "application/x-www-form-urlencoded" }); | ||
} | ||
``` | ||
|
||
- You may now also opt-into different types of serialization of this `payload` into your `request` using the `formEncType` parameter: | ||
|
||
```js | ||
function Component() { | ||
let submit = useSubmit(); | ||
submit({ key: "value" }, { encType: "application/json" }); | ||
// navigation.formEncType => "application/json" | ||
// navigation.formData => undefined | ||
// navigation.payload => { key: "Value" } | ||
} | ||
|
||
function action({ request, payload }) { | ||
// request.headers.get("Content-Type") => "application/json" | ||
// request.json => { key: 'value' } | ||
// payload => { key: 'value' } | ||
} | ||
``` | ||
|
||
```js | ||
function Component() { | ||
let submit = useSubmit(); | ||
submit({ key: "value" }, { encType: "application/x-www-form-urlencoded" }); | ||
// navigation.formEncType => "application/x-www-form-urlencoded" | ||
// navigation.formData => FormData instance | ||
// navigation.payload => { key: "Value" } | ||
} | ||
|
||
function action({ request, payload }) { | ||
// request.headers.get("Content-Type") => "application/x-www-form-urlencoded" | ||
// request.formData => { key: 'value' } | ||
// payload => { key: 'value' } | ||
} | ||
``` | ||
|
||
```js | ||
function Component() { | ||
let submit = useSubmit(); | ||
submit("Plain ol' text", { encType: "text/plain" }); | ||
// navigation.formEncType => "text/plain" | ||
// navigation.formData => undefined | ||
// navigation.payload => "Plain ol' text" | ||
} | ||
|
||
function action({ request, payload }) { | ||
// request.headers.get("Content-Type") => "text/plain" | ||
// request.text => "Plain ol' text" | ||
// payload => "Plain ol' text" | ||
} | ||
``` |
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.