|
| 1 | +--- |
| 2 | +"react-router-dom": minor |
| 3 | +--- |
| 4 | + |
| 5 | +- 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`). |
| 6 | + |
| 7 | +The default behavior will still serialize into `FormData`: |
| 8 | + |
| 9 | +```jsx |
| 10 | +function Component() { |
| 11 | + let submit = useSubmit(); |
| 12 | + submit({ key: "value" }); |
| 13 | + // navigation.formEncType => "application/x-www-form-urlencoded" |
| 14 | + // navigation.formData => FormData instance |
| 15 | + // navigation.payload => { key: "Value" } |
| 16 | +} |
| 17 | + |
| 18 | +function action({ request, payload }) { |
| 19 | + // request.headers.get("Content-Type") => "application/x-www-form-urlencoded" |
| 20 | + // request.formData => FormData instance |
| 21 | + // payload => { key: 'value' } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +You may opt out of this default serialization using `encType: null`: |
| 26 | + |
| 27 | +```jsx |
| 28 | +function Component() { |
| 29 | + let submit = useSubmit(); |
| 30 | + submit({ key: "value" }, { encType: null }); |
| 31 | + // navigation.formEncType => null |
| 32 | + // navigation.formData => undefined |
| 33 | + // navigation.payload => { key: "Value" } |
| 34 | +} |
| 35 | + |
| 36 | +function action({ request, payload }) { |
| 37 | + // request.headers.get("Content-Type") => null |
| 38 | + // request.formData => undefined |
| 39 | + // payload => { key: 'value' } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +_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:_ |
| 44 | + |
| 45 | +```jsx |
| 46 | +function Component() { |
| 47 | + let submit = useSubmit(); |
| 48 | + |
| 49 | + // Change this: |
| 50 | + submit({ key: "value" }); |
| 51 | + |
| 52 | + // To this: |
| 53 | + submit({ key: "value" }, { encType: "application/x-www-form-urlencoded" }); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +- You may now also opt-into different types of serialization of this `payload` into your `request` using the `formEncType` parameter: |
| 58 | + |
| 59 | +```js |
| 60 | +function Component() { |
| 61 | + let submit = useSubmit(); |
| 62 | + submit({ key: "value" }, { encType: "application/json" }); |
| 63 | + // navigation.formEncType => "application/json" |
| 64 | + // navigation.formData => undefined |
| 65 | + // navigation.payload => { key: "Value" } |
| 66 | +} |
| 67 | + |
| 68 | +function action({ request, payload }) { |
| 69 | + // request.headers.get("Content-Type") => "application/json" |
| 70 | + // request.json => { key: 'value' } |
| 71 | + // payload => { key: 'value' } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```js |
| 76 | +function Component() { |
| 77 | + let submit = useSubmit(); |
| 78 | + submit({ key: "value" }, { encType: "application/x-www-form-urlencoded" }); |
| 79 | + // navigation.formEncType => "application/x-www-form-urlencoded" |
| 80 | + // navigation.formData => FormData instance |
| 81 | + // navigation.payload => { key: "Value" } |
| 82 | +} |
| 83 | + |
| 84 | +function action({ request, payload }) { |
| 85 | + // request.headers.get("Content-Type") => "application/x-www-form-urlencoded" |
| 86 | + // request.formData => { key: 'value' } |
| 87 | + // payload => { key: 'value' } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +```js |
| 92 | +function Component() { |
| 93 | + let submit = useSubmit(); |
| 94 | + submit("Plain ol' text", { encType: "text/plain" }); |
| 95 | + // navigation.formEncType => "text/plain" |
| 96 | + // navigation.formData => undefined |
| 97 | + // navigation.payload => "Plain ol' text" |
| 98 | +} |
| 99 | + |
| 100 | +function action({ request, payload }) { |
| 101 | + // request.headers.get("Content-Type") => "text/plain" |
| 102 | + // request.text => "Plain ol' text" |
| 103 | + // payload => "Plain ol' text" |
| 104 | +} |
| 105 | +``` |
0 commit comments