Next.js Discord

Discord Forum

Nothing is getting rendered on making api call in server.

Unanswered
ayush posted this in #help-forum
Open in Discord
Here in this code.
import React from "react";
import { Sidebar, ProductCard } from "components";
import { useSelector } from "react-redux";
import { StoreState } from "store";

const Listing = async () => {
  const response: any = await fetch(
    `${process.env.NEXT_PUBLIC_ENDPOINT}/api/products`,
    { method: "GET" }
  );
  const data = await response.json();
  console.log(data);
  console.log(process.env.NEXT_PUBLIC_ENDPOINT, "localhost>>");

  return (
    <div className="p-4 flex items-center justify-center  overflow-scroll">
      <div className="flex gap-x-[50px] ">
        <Sidebar />
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-3 overflow-scroll">
          {data?.products?.map((value: any) => {
            return (
              <ProductCard
                key={`${value.id}${value.title}`}
                cardDetails={value}
              />
            );
          })}
        </div>
      </div>
    </div>
  );
};

export default Listing;


what I am trying to do is I am fetching data from backend api which is in next itself. the data is getting fetch successfully but not passed to the client. (we need this as sidebar and product card are both client side. so they cant be rendered in server) I am using latest next version. so how do I use the functionality of serversideprops here.

2 Replies

@Silver Marten
The only solution i have is to use wrap the fetch logic to the client side. but want to make call in server and then use the data in the client. how can that be possible here.