Skip to content

Help with fetching data from API ( New to Remix ) #9645

Closed Answered by admirsaheta
banedevv asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, this should be pretty simple

Create a loader function: This function will run on the server-side and fetch the data.

Use the useLoaderData hook: This hook will allow you to access the data fetched by the loader function within your component.

Here's a simple example:

import { json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";

// 1. Define the loader function
export const loader = async () => {
  const response = await fetch("https://api.example.com/data");
  const data = await response.json();

  // Return the data using the `json` helper
  return json(data);
};

export default function Index() {
  // 2. Use the `useLoaderData` hook to access the data
  c…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@banedevv
Comment options

Answer selected by banedevv
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants