Skip to content

Commit 5ca5399

Browse files
committed
Add changesets
1 parent faaf76b commit 5ca5399

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
"@remix-run/router": minor
3+
---
4+
5+
Add support for a new `payload` parameter for `router.navigate` and `router.fetch` submissions. This allows you to submit data to an action without requiring serialization into a `FormData` instance. This `payload` value wil be passed unaltered to your action function.
6+
7+
```js
8+
router.navigate("/", { payload: { key: "value" } });
9+
10+
function action({ request, payload }) {
11+
// payload is `{ key: 'value' }`
12+
// request.body is `null`
13+
}
14+
```
15+
16+
You may also opt-into serialization of this `payload` into your `request` using the `formEncType` parameter:
17+
18+
```js
19+
router.navigate("/", {
20+
payload: { key: "value" },
21+
formEncType: "application/json",
22+
});
23+
24+
function action({ request, payload }) {
25+
// payload is `{ key: 'value' }`
26+
// await request.text() is '{"key":"value"}'
27+
}
28+
```
29+
30+
```js
31+
router.navigate("/", {
32+
payload: { key: "value" },
33+
formEncType: "application/x-www-form-urlencoded",
34+
});
35+
36+
function action({ request, payload }) {
37+
// payload is `{ key: 'value' }`
38+
// await request.formData() is a `FormData` instance with a single entry of key=value
39+
}
40+
```
41+
42+
```js
43+
router.navigate("/", {
44+
payload: "Plain ol' text",
45+
formEncType: "text/plain",
46+
});
47+
48+
function action({ request, payload }) {
49+
// payload is "Plain ol' text"
50+
// await request.text() is "Plain ol' text"
51+
}
52+
```

.changeset/raw-payload-submission.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
"react-router-dom": minor
3+
---
4+
5+
Support submission of raw payloads through `useSubmit`/`fetcher.submit` by opting out of serialization into `request.formData` using `encType: null`. When opting-out of serialization, your data will be passed to the action in a new `payload` parameter:
6+
7+
```jsx
8+
function Component() {
9+
let submit = useSubmit();
10+
submit({ key: "value" }, { encType: null });
11+
}
12+
13+
function action({ request, payload }) {
14+
// payload is `{ key: 'value' }`
15+
// request.body is `null`
16+
}
17+
```
18+
19+
Since the default behavior in `useSubmit` today is to serialize to `application/x-www-formn-urlencoded`, that will remain the behavior for `encType:undefined` in v6. But in v7, we plan to change the default behavior for `undefined` to skip serialization. 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:
20+
21+
```jsx
22+
function Component() {
23+
let submit = useSubmit();
24+
25+
// Change this:
26+
submit({ key: "value" });
27+
28+
// To this:
29+
submit({ key: "value" }, { encType: "application/x-www-form-urlencoded" });
30+
}
31+
```

0 commit comments

Comments
 (0)