Very confused with single fetch api (json, data, response, redirect) #10238
Replies: 1 comment 1 reply
-
Single fetch is used for the loader data. To understand the methods like Before single fetch, remix would send separate request for all the active routes up to the root and then provide the results via useLoaderData to all the individual active routes. Single fetch instead sends one request with the list of the routes it wants to have the data for. And it gets a single response with the returned data for each of the routes listed in the request. You can not simply return new Response() from each route, because there can only be one response. What is supposed to happen with all the responses returned? This is why you would only return data, the Response is created automatically for you to wrap all the data from each route's loader. On the client the useLoaderData hook then unpacks the data for its route from the single response. So when you just want to return data, you just return the data like: return {foo: "bar"} But what if you want to set status or a header? This is where you use the You only use the What about Single fetch also introduces the use of a transformer that simply allows you to return promises without any helper like When to use new Response: for example in resource routes. When you return an image, pdf or other data directly. Its obvious that for a single URL, no matter how many parent routes are above the resource route, you only return one response and its the resource you want to be returned in this URL ... Basically the leaf (resource) route dictates the returned response. You don't want to render you PDF in a React layout, right? :D TLDR, above just explains why:
|
Beta Was this translation helpful? Give feedback.
-
I want to do something like this:
export async function action({ request }: ActionFunctionArgs) {
return new Response(null, {
status: 400
});
}
Saw that json() is deprecated now.
What about Response object? Should I use this or data()? And is redirect affected by this migration to single fetch api?
Beta Was this translation helpful? Give feedback.
All reactions