Is this code well-written ? (Small, beginner)
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
async function getData(): Promise<Container[] | null> {
const response = await fetch(`http://localhost:5236/api/v1/Flowers?all=true`, {
cache: "force-cache",
});
if (!response.ok) {
throw new Error(`Request failed with status code ${response.status}`);
}
return response.json();
}
export default async function ContainerDataTableComponent() {
const data = await getData();
return <div>
{
data
? <DataTable columns={columns} data={data}/>
: <div>Loading...</div>
}
</div>
}In my back, I got 5* requests to my controller
GetFlowersThanks! â¤ï¸
66 Replies
lgtm
does the problem persist if you try to fetch via CURL/Postman
@aardani lgtm
Spectacled bearOP
Can it be improved ?
via Postman, it sent only one request, and it working like a charm.
via next, it's sending a lot of requests and I don't know, may be caused due to shadcn datatable
via browser?
OH
OH
this is a client component
bruh
Its good if its a server component
but for client component, put it inside a
useEffectSpectacled bearOP
I mean
There's no any
"use client" in this filethere does not need to
if you import this component to a "use client" file, then it will be acted as a client component
Spectacled bearOP
Someone told me that everything should be server, instead of hooks, etc.
he meant that you should less side effects as much as possible and prerender stuff
that doesn't mean client-side stuff is bad
@Spectacled bear tsx
async function getData(): Promise<Container[] | null> {
const response = await fetch(`http://localhost:5236/api/v1/Flowers?all=true`, {
cache: "force-cache",
});
if (!response.ok) {
throw new Error(`Request failed with status code ${response.status}`);
}
return response.json();
}
export default async function ContainerDataTableComponent() {
const data = await getData();
return <div>
{
data
? <DataTable columns={columns} data={data}/>
: <div>Loading...</div>
}
</div>
}
In my back, I got 5* requests to my controller `GetFlowers`
Thanks! â¤ï¸
if
getData() is initiall fetched, then you are right: it should be done in the serverSpectacled bearOP
They told me that client-side is prefered for client-interaction
yes
he's right
Spectacled bearOP
Well then for my thing, only fetching and create a
DataTable, why it should be client ?i didnt say that it should be client
but since you imported the component to a shadcn component or a "use client" component it BECOMES a client component
Spectacled bearOP
What
I mean, I'm using a shadcn component inside a server component
DataTable is a client componentyes, and where is
ContainerDataTableComponent() is being used?Spectacled bearOP
Inside a server component
can I see that "server component"
Spectacled bearOP
import ContainerDataTableComponent from "@/app/containers/page";
export default function DockerContainers() {
return <section className="flex flex-col gap-10">
<h1 className="text-3xl font-semibold">Your containers</h1>
<ContainerDataTableComponent/>
</section>;
}where is DockerContainers() being used
Spectacled bearOP
Inside a server component (page.tsx)
import {Inter} from 'next/font/google'
import DockerContainers from "@/components/DockerContainersComponent";
const inter = Inter({subsets: ['latin']});
export default function Home() {
return <main>
<h1 className="text-red-500">DockSweeper</h1>
<DockerContainers/>
</main>
}That's weird right
pretty weird
try recreate the bug in a minimal reproduction repository
Spectacled bearOP
But I think that it could come from the datatable component from Shadcn/ui
i dont think so
did you try removing it
Spectacled bearOP
Like every interaction or I don't know, fetch
that shouldn't happen as
ContainerDataTableComponent is not a client componentSpectacled bearOP
Yeah and it does the same
so its not DataTable...
Spectacled bearOP
It's spamming request and I got that :
Unhandled Runtime Error
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.Spectacled bearOP
That's my file project structure if it can help
can you tell me
where is
ContainerDataTableComponent located using the Search functioni dont have enough information
Spectacled bearOP
Going to the main page got that :
what file caused that error
Spectacled bearOP
I now having that :
For this page :
import {columns, Container} from "./columns"
import {DataTable} from "@/app/containers/data-table";
async function getContainers(): Promise<Container[] | null> {
const response = await fetch(`http://localhost:5236/api/v1/DockerContainer?all=true`, {
cache: "force-cache",
});
if (!response.ok) {
throw new Error(`Request failed with status code ${response.status}`);
}
return await response.json();
}
export default async function ContainerDataTableComponent() {
const data = await getContainers();
return <div>
{
data
? <DataTable columns={columns} data={data}/>
: <div>Loading...</div>
}
</div>
}@Spectacled bear I now having that :
so do you know what file causes this error
Spectacled bearOP
Not really
I'm supposing
The call stack is not really helpful
Call Stack
resolveModelToJSON
node_modules\.pnpm\next@13.4.19_react-dom@18.2.0_react@18.2.0\node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2102:12)
resolveModelToJSON
node_modules\.pnpm\next@13.4.19_react-dom@18.2.0_react@18.2.0\node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1399:13)
stringify
<anonymous>
stringify
node_modules\.pnpm\next@13.4.19_react-dom@18.2.0_react@18.2.0\node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2546:13)
processModelChunk
node_modules\.pnpm\next@13.4.19_react-dom@18.2.0_react@18.2.0\node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2252:25)
retryTask
node_modules\.pnpm\next@13.4.19_react-dom@18.2.0_react@18.2.0\node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (2299:6)
performWork
node_modules\.pnpm\next@13.4.19_react-dom@18.2.0_react@18.2.0\node_modules\next\dist\compiled\react-server-dom-webpack\cjs\react-server-dom-webpack-server.edge.development.js (1694:13)
listOnTimeout
node:internal/timers (569:17)
process.processTimers
node:internal/timers (512:7)try to troubleshoot first then
have you tried it on a minimal reproduction repository?
does the bug persist on a new project?
Spectacled bearOP
I just put a shadcn class into client and it's working now. I don't know why