"use client"; import React, { useEffect, useState } from "react"; import Link from "next/link"; import Image from "next/image"; import { useRouter } from "next/navigation"; export default function ShopPage() { const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchTerm, setSearchTerm] = useState(""); const [currentPage, setCurrentPage] = useState(1); const [productsPerPage] = useState(10); // 每页显示的商品数量 const [activeTab, setActiveTab] = useState("all"); // 新增的状态 const router = useRouter(); useEffect(() => { const fetchProducts = async () => { try { const res = await fetch("/api/products"); if (!res.ok) { throw new Error("获取商品列表失败"); } const data = await res.json(); setProducts(data); } catch (err) { setError(err.message); } finally { setLoading(false); } }; fetchProducts(); }, []); const handleSearch = (e) => { setSearchTerm(e.target.value); }; const filteredProducts = products.filter((product) => product.title.toLowerCase().includes(searchTerm.toLowerCase()) ); const indexOfLastProduct = currentPage * productsPerPage; const indexOfFirstProduct = indexOfLastProduct - productsPerPage; const currentProducts = filteredProducts.slice( indexOfFirstProduct, indexOfLastProduct ); const paginate = (pageNumber) => setCurrentPage(pageNumber); if (loading) return
加载中...
; if (error) return
错误: {error}
; return (
{/* 店铺信息 */} 店铺头像
宠物用品专卖店

官方正品旗舰店{" >"}

{/* Tab 切换栏 */}
{currentProducts.map((product) => (
{product.title}

{product.title}

¥{product.price}

))}
{[ ...Array(Math.ceil(filteredProducts.length / productsPerPage)).keys(), ].map((number) => ( ))}
); }