Loading...
Loading...
Compare original and translation side by side
undefinedundefined
**Red flags:**
- `"use client"` + `useEffect` + `fetch()` = slow initial load
- `useState(true)` for `isLoading` = user sees spinner
- `useStore()` or `useContext` for initial page data = waterfall fetching
**危险信号:**
- `"use client"` + `useEffect` + `fetch()` = 初始加载缓慢
- `useState(true)` 设置 `isLoading` = 用户会看到加载spinner
- `useStore()` 或 `useContext` 用于初始页面数据 = 瀑布式数据获取useInViewuseStateonClick// components/data-section.tsx
"use client";
interface DataSectionProps {
data: Item[]; // Receive data as props
}
export function DataSection({ data }: DataSectionProps) {
const [ref, inView] = useInView(); // Client-side animation OK
return <div ref={ref}>...</div>;
}useInViewuseStateonClick// components/data-section.tsx
"use client";
interface DataSectionProps {
data: Item[]; // 以props形式接收数据
}
export function DataSection({ data }: DataSectionProps) {
const [ref, inView] = useInView(); // 客户端动画可正常使用
return <div ref={ref}>...</div>;
}// app/page.tsx - NO "use client"
import { getData } from "@/lib/actions/data";
import { DataSection } from "@/components/data-section";
export default async function Page() {
const data = await getData(); // Fetch on server
return <DataSection data={data} />;
}// app/page.tsx - 不要添加"use client"
import { getData } from "@/lib/actions/data";
import { DataSection } from "@/components/data-section";
export default async function Page() {
const data = await getData(); // 在服务端获取数据
return <DataSection data={data} />;
}import type { Item as DBItem } from "@/lib/database.types";
import type { Item } from "@/lib/types";
function adaptDBToFrontend(db: DBItem): Item {
return {
id: db.id,
name: db.name,
description: db.description ?? "",
createdAt: new Date(db.created_at),
};
}
export default async function Page() {
const dbItems = await getItems();
const items = dbItems.map(adaptDBToFrontend);
return <ItemList items={items} />;
}import type { Item as DBItem } from "@/lib/database.types";
import type { Item } from "@/lib/types";
function adaptDBToFrontend(db: DBItem): Item {
return {
id: db.id,
name: db.name,
description: db.description ?? "",
createdAt: new Date(db.created_at),
};
}
export default async function Page() {
const dbItems = await getItems();
const items = dbItems.map(adaptDBToFrontend);
return <ItemList items={items} />;
}"use client""use client"