next.js 实时搜索栏:如何更新显示在不同组件中的数据,而不是输入?

wbgh16ku  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(106)

我正在尝试使用Next.js实现一个实时搜索栏。我能够处理输入的变化,但我似乎不能根据输入过滤我的结果。
layout.jsx:

import './globals.css';
import SearchBar from './SearchBar';

export const metadata = {
  title: 'My App',
  description: 'Generated by create next app',
}

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body className='text-center bg-sky-800'>
        <h1 className='text-4xl mt-4 text-white'>My App</h1>
        <SearchBar />
        {children}
      </body>
    </html>
  )
}

SearchBar组件:

'use client'
import { useState } from "react"

export default function SearchBar() {
    const [searchInput, setSearchInput] = useState('');
    
    function handleChange(e) {
        setSearchInput(e.target.value);
    }

    return (
        <input 
        className="p-3 border rounded bg-blue-100"
        type="search" 
        placeholder="Search Robots"
        onChange={handleChange}
        />
    )
}

包含要过滤的项目的代码的组件(它们是卡片):

import Card from './Card'
import { robots } from './robots'

export default function RobotList() {
  return (
    <main>
      {robots.map(robot => {
          return (
            <Card key={robot.id} id={robot.id} name={robot.name} email={robot.email} />
          )
        })
      }
    </main>
  )
}

page.jsx:

import RobotList from './RobotList'

export default function Home() {
  return (
    <main>
        <RobotList />
    </main>
  )
}

卡组件:

export default function Card({ id, name, email }) {
    return (
        <div className="text-center bg-green-300 p-3 rounded inline-block m-2 hover:scale-105 transition ease-in duration-300 drop-shadow-xl">
            <img src={`https://robohash.org/${id}?200x200`} alt="robot" />
            <div>
                <h2>{name}</h2>
                <p>{email}</p>
            </div>
        </div>
    )
}

我使用的是Next.js v13。任何帮助将不胜感激。Thanks:)

2guxujil

2guxujil1#

在当前代码中,显示robots列表的组件根本没有绑定到input。实现搜索的一种方法是将SearchBar转换为上下文提供程序,它将返回过滤后的robots,这种方法不会对当前组件的结构造成太大的改变。
我称之为RobotsContextProvider

// RobotsContextProvider.js

"use client";

// Make sure this is the correct path
import { robots } from "./robots";
import { useState, createContext, useContext } from "react";

const RobotsContext = createContext();
export function useRobotsContext() {
  return useContext(RobotsContext);
}

export default function RobotsContextProvider({ children }) {
  const [searchInput, setSearchInput] = useState("");

  function handleChange(e) {
    setSearchInput(e.target.value);
  }

  return (
    <RobotsContext.Provider
      value={{ robots: searchInput ? robots.filter((r) => r.name.includes(searchInput)) : robots }}
    >
      <input
        className="p-3 border rounded bg-blue-100"
        type="search"
        placeholder="Search Robots"
        onChange={handleChange}
      />
      {children}
    </RobotsContext.Provider>
  );
}

你可以在RootLayout中将它包裹在children周围:

import "./globals.css";
import RobotsContextProvider from "./RobotsContextProvider";

export const metadata = {
  title: "My App",
  description: "Generated by create next app",
};

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body className="text-center bg-sky-800">
        <h1 className="text-4xl mt-4 text-white">My App</h1>
        <RobotsContextProvider>{children}</RobotsContextProvider>
      </body>
    </html>
  );
}

RobotList中,您可以从上下文使用robots,而不是导入它:

"use client";

import Card from "./Card";
// Make sure this is the correct path
import { useRobotsContext } from "./RobotsContextProvider";

export default function RobotList() {
  const { robots } = useRobotsContext();
  return (
    <main>
      {robots.map((robot) => {
        return <Card key={robot.id} id={robot.id} name={robot.name} email={robot.email} />;
      })}
    </main>
  );
}

确保你读了评论。

相关问题