Jean-Paul Abi-Ghosn
Jean-Paul Abi-Ghosn

Reputation: 191

Convert array of Data into React components on server side but I want the React components to be rendered on the Client side

Let's say I am using the Carousel and Carousel.Item from react-bootstrap. This can only be rendered on the client side.

I want to transform the data on my server into HTML, but still I do not want to render the component on the server side.

Why do I want to do it this way? To improve SEO of the website.

const data = [1,2,3,4];

// I want this to be rendered on the server and return 
const items = data.map(i => <Carousel.Item>i</Carousel.Item>);

// I want this to return to the client and the client should be responsible of rendering the Carousel.
/*
// <Carousel>
//     <Carousel.Item>1</Carousel.Item>
//     <Carousel.Item>2</Carousel.Item>
//     <Carousel.Item>3</Carousel.Item>
//     <Carousel.Item>4</Carousel.Item>
// </Carousel>
*/
return <Carousel>{items}</Carousel>

Upvotes: 0

Views: 75

Answers (1)

Yilmaz
Yilmaz

Reputation: 49681

but still I do not want to render the component on the server side.

client components are first rendered on the server. if your component has client side api functions (useState, useEffect, onClick), since those functions are not available on the server, your component will throw an error. by using use client it just says ignore those api, send this to client and client will take care of. that is what hydration is

Hydration is the process of attaching event listeners to the DOM, to make the static HTML interactive. Behind the scenes, hydration is done with the hydrateRoot React API.

Subsequent Navigations

On subsequent navigations, Client Components are rendered entirely on the client, without the server-rendered HTML.

Upvotes: 0

Related Questions