Next.js Discord

Discord Forum

I'm using fetch and await inside use client. can anyone give me solution

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
"use client";

import React from "react";
import { usePathname } from "next/navigation";
import Bb from "./bb";

import Header from "./Header";
import Headertop from "./Headertop";
import CategoryHeader from "./Category/CategoryHeader";

const ClientLayout: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const pathname = usePathname();

const excludedPaths = ["/configurateur", "/signin"];

if (excludedPaths.includes(pathname)) {
return #Unknown Channel{children}</>;
}

return (
<div className=" flex flex-col h-full">
{/* <Headertop />
<Header /> /}
<CategoryHeader />
{children}
{/
<Bb /> */}
</div>
);
}; import React from 'react';
import Headerbottom from '@/components/Headerbottom';

interface Category {
_id: string;
name: string;
imageUrl?: string;
}

// Function to fetch category data
const fetchCategories = async (): Promise<Category[]> => {
try {

const res = await fetch('http://localhost:3000/api/category/getAllCategory' ,{ cache: 'no-store' });
if (!res.ok) {
throw new Error('Failed to fetch categories');
}
return res.json();
} catch (error) {
console.error('Failed to fetch categories:', error);
return [];
}
};

// Server-side component to fetch categories and pass them to the client-side component
const CategoryHeader = async () => {
const categories = await fetchCategories();

return <Headerbottom categories={categories} />;
};

export default CategoryHeader;

11 Replies

@Cape lion "use client"; import React from "react"; import { usePathname } from "next/navigation"; import Bb from "./bb"; import Header from "./Header"; import Headertop from "./Headertop"; import CategoryHeader from "./Category/CategoryHeader"; const ClientLayout: React.FC<{ children: React.ReactNode }> = ({ children, }) => { const pathname = usePathname(); const excludedPaths = ["/configurateur", "/signin"]; if (excludedPaths.includes(pathname)) { return <>{children}</>; } return ( <div className=" flex flex-col h-full"> {/* <Headertop /> <Header /> */} <CategoryHeader /> {children} {/* <Bb /> */} </div> ); }; import React from 'react'; import Headerbottom from '@/components/Headerbottom'; interface Category { _id: string; name: string; imageUrl?: string; } // Function to fetch category data const fetchCategories = async (): Promise<Category[]> => { try { const res = await fetch('http://localhost:3000/api/category/getAllCategory' ,{ cache: 'no-store' }); if (!res.ok) { throw new Error('Failed to fetch categories'); } return res.json(); } catch (error) { console.error('Failed to fetch categories:', error); return []; } }; // Server-side component to fetch categories and pass them to the client-side component const CategoryHeader = async () => { const categories = await fetchCategories(); return <Headerbottom categories={categories} />; }; export default CategoryHeader;
use a clientside fetching library like swr or react query, to fetch data clientside and handle it
@B33fb0n3 use a clientside fetching library like swr or react query, to fetch data clientside and handle it
Cape lionOP
after using this libray swr this Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding 'use client' to a module that was originally written for the server.
@Cape lion I add but same Error
can you share the current code?
@B33fb0n3 can you share the current code?
Cape lionOP
import React from 'react';
import useSWR from 'swr';
import Headerbottom from '@/components/Headerbottom';

interface Category {
_id: string;
name: string;
imageUrl?: string;
}

// Function to fetch category data
const fetchCategories = async (): Promise<Category[]> => {
const res = await fetch('http://localhost:3000/api/category/getAllCategory', { cache: 'no-store' });
if (!res.ok) {
throw new Error('Failed to fetch categories');
}
return res.json();
};

// Client-side component to fetch categories using SWR
const CategoryHeader = () => {
const { data: categories, error } = useSWR<Category[]>('/api/category/getAllCategory', fetchCategories);

if (error) return <div>Failed to load categories</div>;
if (!categories) return <div>Loading...</div>;

return <Headerbottom categories={categories} />;
};

export default CategoryHeader;
@B33fb0n3 it looks like you havent added 'use client' on top?
Cape lionOP
is work problem use client in CategoryHeader and Headerbottom compoment i remove use client is working
@Cape lion is work problem use client in CategoryHeader and Headerbottom compoment i remove use client is working
I just tried it myself see here: https://codesandbox.io/p/devbox/great-dubinsky-nf39yt
And it looks like there are more problems in your code. So please create a repro via jsfiddle or https://codesandbox.io/ so we will be able to help you better
Ping me in this thread, when you created the reproduction