Next.js 'use client' with server component(glob)

piv4azn7  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(111)

嗨,我想使画廊不透明度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

ne5o7dgx

ne5o7dgx1#

虽然,我找到了一个答案(?可能是旁路)从youtube,https://www.youtube.com/watch?v=zwQs4wXr9Bg&ab_channel=JackHerrington
我回答我的问题的人谁可能有类似的问题。
新代码使用Route Handlers获取图像。

app/page.tsx

import Image from "next/image";
import Gallery from "./ui/Gallery";
import SearchBar from "./ui/SearchBar";
import GalleryWrapper from "./ui/GalleryWrapper";
import { useState } from "react";

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>
  );
}

字符串

ui/Gallery.tsx

import React, { use } from "react";
import Image from "next/image";

const getImages = async () => {
  let images = await fetch("http://localhost:3000/api");
  return images.json();
};

const fetchedImages = getImages();

const Gallery = ({ isSearching }: { isSearching: boolean }) => {
  const images = use(fetchedImages);
  return (
    <div className={`flex gap-2 ${isSearching ? "opacity-50" : ""}`}>
      {images.map((image: string) => (
        <Image key={image} src={image} width={200} height={200} alt={image} />
      ))}
    </div>
  );
};

export default Gallery;

ui/SearchBar.tsx

import React, { Dispatch, SetStateAction } from "react";

const SearchBar = ({
  setIsSearching,
}: {
  setIsSearching: Dispatch<SetStateAction<boolean>>;
}) => {
  return (
    <div>
      <input
        type="text"
        className="border rounded-md w-full mb-2"
        onFocus={() => setIsSearching(true)}
        onBlur={() => setIsSearching(false)}
      />
    </div>
  );
};

export default SearchBar;

API/route.ts

import { glob } from "glob";

export async function GET(request: Request) {
  let images = await glob("/images/*.png");
  return new Response(JSON.stringify(images));
}


欢迎您提出更好的想法或建议。

相关问题