v6.14.0
What's Changed
JSON/Text Submissions
6.14.0
adds support for JSON and Text submissions via useSubmit
/fetcher.submit
since it's not always convenient to have to serialize into FormData
if you're working in a client-side SPA. To opt-into these encodings you just need to specify the proper formEncType
:
Opt-into application/json
encoding:
function Component() {
let navigation = useNavigation();
let submit = useSubmit();
submit({ key: "value" }, { method: "post", encType: "application/json" });
// navigation.formEncType => "application/json"
// navigation.json => { key: "value" }
}
async function action({ request }) {
// request.headers.get("Content-Type") => "application/json"
// await request.json() => { key: "value" }
}
Opt-into text/plain
encoding:
function Component() {
let navigation = useNavigation();
let submit = useSubmit();
submit("Text submission", { method: "post", encType: "text/plain" });
// navigation.formEncType => "text/plain"
// navigation.text => "Text submission"
}
async function action({ request }) {
// request.headers.get("Content-Type") => "text/plain"
// await request.text() => "Text submission"
}
Please note that to avoid a breaking change, the default behavior will still encode a simple key/value JSON object into a FormData
instance:
function Component() {
let navigation = useNavigation();
let submit = useSubmit();
submit({ key: "value" }, { method: "post" });
// navigation.formEncType => "application/x-www-form-urlencoded"
// navigation.formData => FormData instance
}
async function action({ request }) {
// request.headers.get("Content-Type") => "application/x-www-form-urlencoded"
// await request.formData() => FormData instance
}
This behavior will likely change in v7 so it's best to make any JSON object submissions explicit with formEncType: "application/x-www-form-urlencoded"
or formEncType: "application/json"
to ease your eventual v7 migration path.
Minor Changes
- Add support for
application/json
andtext/plain
encodings foruseSubmit
/fetcher.submit
. To reflect these additional types,useNavigation
/useFetcher
now also containnavigation.json
/navigation.text
andfetcher.json
/fetcher.text
which include the json/text submission if applicable. (#10413)
Patch Changes
- When submitting a form from a
submitter
element, prefer the built-innew FormData(form, submitter)
instead of the previous manual approach in modern browsers (those that support the newsubmitter
parameter) (#9865)- For browsers that don't support it, we continue to just append the submit button's entry to the end, and we also add rudimentary support for
type="image"
buttons - If developers want full spec-compliant support for legacy browsers, they can use the
formdata-submitter-polyfill
- For browsers that don't support it, we continue to just append the submit button's entry to the end, and we also add rudimentary support for
- Call
window.history.pushState/replaceState
before updating React Router state (instead of after) so thatwindow.location
matchesuseLocation
during synchronous React 17 rendering (#10448)⚠️ Note: generally apps should not be relying onwindow.location
and should always referenceuseLocation
when possible, aswindow.location
will not be in sync 100% of the time (due topopstate
events, concurrent mode, etc.)
- Avoid calling
shouldRevalidate
for fetchers that have not yet completed a data load (#10623) - Strip
basename
from thelocation
provided to<ScrollRestoration getKey>
to match theuseLocation
behavior (#10550) - Strip
basename
from locations provided tounstable_useBlocker
functions to match theuseLocation
behavior (#10573) - Fix
unstable_useBlocker
key issues inStrictMode
(#10573) - Fix
generatePath
when passed a numeric0
value parameter (#10612) - Fix
tsc --skipLibCheck:false
issues on React 17 (#10622) - Upgrade
typescript
to 5.1 (#10581)
Full Changelog: https://github.com/remix-run/react-router/compare/react-router@6.13.0...react-router@6.14.0