嗨,我想使画廊不透明度50%,当我键入搜索栏。但我觉得我不能使用'使用客户端'与glob库。
以下是我的代码
app/page.tsx
"use client";
import { useState } from "react";
import Gallery from "./ui/Gallery";
import SearchBar from "./ui/SearchBar";
export default function Home() {
const [isSearching, setIsSearching] = useState(false);
return (
<div className="max-w-5xl m-auto mt-10">
<SearchBar setIsSearching={setIsSearching} />
<Gallery isSearching={isSearching} />
</div>
);
}
字符串
app/ui/Gallery.tsx
import React from "react";
import { glob } from "glob";
import Image from "next/image";
import { randomUUID } from "crypto";
const Gallery = async ({ isSearching }) => {
let images = await glob("public/images/*");
images = images.map((image) => image.split("public")[1]);
return (
<div className={`flex ${isSearching ? "opacity-50" : ""}`}>
{images.map((image) => (
<Image
key={randomUUID()}
src={image}
width={300}
height={300}
alt="gallery image"
/>
))}
</div>
);
};
export default Gallery;
型
app/ui/SearchBar.tsx
import React from "react";
const SearchBar = ({ setIsSearching }) => {
return (
<div>
<input
type="text"
className="w-full mb-4 border-2 border-gray-300 bg-white h-10 px-5 pr-16 rounded-lg text-sm focus:outline-none"
onFocus={() => setIsSearching(true)}
/>
</div>
);
};
export default SearchBar;
型
以下为错误
error
我看到了Next.js的官方文档
1.数据提取-https://nextjs.org/docs/app/building-your-application/data-fetching
1.渲染-https://nextjs.org/docs/app/building-your-application/rendering
1条答案
按热度按时间ne5o7dgx1#
虽然,我找到了一个答案(?可能是旁路)从youtube,https://www.youtube.com/watch?v=zwQs4wXr9Bg&ab_channel=JackHerrington
我回答我的问题的人谁可能有类似的问题。
新代码使用Route Handlers获取图像。
app/page.tsx
字符串
ui/Gallery.tsx
型
ui/SearchBar.tsx
型
API/route.ts
型
欢迎您提出更好的想法或建议。