-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Docs]: Returning json from loader #12865
Comments
For RestAPI JSON response you could use https://reactrouter.com/how-to/headers#1-wrap-your-return-value-in-data For Rest API: export function loader() {
return Response.json({ foo: "bar" });
} For page rendering: export function loader() {
return { foo: "bar" };
} |
But isnt it also possible to return Response.json with headers? |
Hi guys, Thanks for pointing to the documentation. I changed my code from Still the error message from the framework is misleading when using the
Message says that it's a bug in React Router, but it was actually me using it incorrectly. Should we change the error message in that case and link to a guide on how to use it? Something like:
|
I 've found this pain points:
|
Describe what's incorrect/missing in the documentation
Not sure if this should be a docs, bug, or another type of issue. It is about an error message that seems to be misleading.
I started using React Router (as a framework) for a personal project and one of my routes is an API endpoint that returns JSON, intended for other developers to consume.
My first instinct was to return
data({ ... })
from my loader (based on my usage ofdata
with actions), so I tried:When doing that, I got the following error in my terminal:
Special highlight about the log "The following error is a bug in React Router", while this doesn't seem to actually be a bug.
I eventually discovered that unlike
action
that can returndata
, on theloader
I needed to return an instance ofResponse
and manually apply thecontent-type
header toapplication/json
.loader
accept returningdata
?Response
The text was updated successfully, but these errors were encountered: