Complete the code to export a loader function that returns JSON data.
export async function loader() {
return [1]({ message: "Hello from loader" });
}The json function from Remix is used to return JSON responses from loader functions.
Complete the code to import the correct Remix function for returning JSON in a loader.
import { [1] } from "@remix-run/node";
useLoaderData which is a React hook, not a loader helper.redirect which is for navigation, not data.The json function is imported from @remix-run/node to return JSON responses in loader functions.
Fix the error in the loader function to correctly fetch and return JSON data.
export async function loader() {
const response = await fetch("https://api.example.com/data");
const data = await response.json();
return [1](data);
}json.console.log instead of returning data.The loader must return data wrapped with the json function to send a proper JSON response.
Fill both blanks to create a loader that fetches data and returns it as JSON.
import { [1] } from "@remix-run/node"; export async function loader() { const res = await fetch("https://api.example.com/items"); const items = await res.json(); return [2](items); }
useLoaderData instead of json.json.Both blanks use the json function: one to import it, one to return data wrapped in it.
Fill all three blanks to create a loader that fetches user data, checks status, and returns JSON.
import { [1] } from "@remix-run/node"; export async function loader() { const response = await fetch("https://api.example.com/user"); if (!response.ok) { throw new Response("Not Found", { status: 404 }); } const user = await response.json(); return [2]({ user: [3] }); }
redirect instead of json.user.name instead of the full user object.The json function is imported and used to return the data. The user object is passed inside the JSON response.