#how to fetch large data in react component and have it pre-rendered so that it is visible instantly
3 messages · Page 1 of 1 (latest)
Hi @tender sandal. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can fetch data in your React component and have it pre-rendered for instant visibility. Here's how you can do it:
- Fetch the data in your component. The
fetch()function is globally available to any framework components. Here's an example of how you can use it in a React component:
import type { FunctionalComponent } from 'preact';
const data = await fetch('<https://example.com/movies.json'>).then((response) => response.json());
console.log(data);
const Movies: FunctionalComponent = () => {
return <div>{JSON.stringify(data)}</div>;
};
export default Movies;
(Source: Astro Documentation (https://docs.astro.build/en/guides/data-fetching/#fetch-in-framework-components))
- To have the component pre-rendered, you can use the
prerenderexport. For a mostly server-rendered app configured asoutput: server, addexport const prerender = trueto any page or route to pre-render a static page or endpoint:
export const prerender = true;
(Source: Astro Documentation (https://docs.astro.build/en/guides/server-side-rendering/#opting-in-to-pre-rendering-in-server-mode))
- To hydrate the component on load, you can use the
client:loaddirective in Astro. This will load and hydrate the component JavaScript once the HTML has loaded:
<Movies client:load />