简体   繁体   English

不能 map 获取数据作为输入字段自动完成

[英]can't map fetched data as an input field autocomplete

I'm trying to capture some suggestions for my input field, i can log them without a problem, but i can't map it and autocomplete on my input type field.我正在尝试为我的输入字段捕获一些建议,我可以毫无问题地记录它们,但我不能 map 它并在我的输入类型字段上自动完成。 trying to understand what i'm doing wrong.试图了解我做错了什么。

const Lista = () => {
  const [search, setSearch] = useState("");

  useEffect(() => {
    handleSearch();
  }, []);

  async function handleSearch() {
    const response = await fetch("/api/product");
    const search = await response.json();
    setSearch(search);
    console.log(search);
  }

  if (!search) return <p>Loading</p>;

  return (
    <>
      <section>
        <div className="listsContainer">
          <div className="cartContainer">
          <form className="formContainer" onSubmit={handleSubmit}>
            <div className="inputs">
              <div>
                <label>Product</label>
                <input
                  required
                  onChange={(e) => setSearch(e.target.value)}
                />
                <div>
                  {search
                    .filter((item) => {
                      return search.toLowerCase() === ""
                        ? null
                        : item.name.toLowerCase().includes(search);
                    })
                    .map((item) => (
                      <div key={item.id}>
                        <h1>{item.name}</h1>
                      </div>
                    ))}
                </div>

you are mapping 'search' as the response while it contains the user input value.您正在将“搜索”映射为响应,同时它包含用户输入值。 you should add a new locale state for storing the response and then use it for the mapping.您应该添加一个新的区域设置 state 来存储响应,然后将其用于映射。 you should notice that filter is an Array method and will not work on Json. You should filer Object.values(yourJson).你应该注意到 filter 是一个数组方法,不会对 Json 起作用。你应该 filer Object.values(yourJson)。 see example bellow:请参阅下面的示例:

const Lista = () => {
  const [search, setSearch] = useState("");
  const [productResponse, setproductResponse] = useState({});

  useEffect(() => {
    handleSearch();
  }, []);

  async function handleSearch() {
    const response = await fetch("/api/product");
    const resToJson= await response.json();
    setproductResponse(resToJson);
    console.log(resToJson);
  }

  if (!productResponse) return <p>Loading</p>;

  return (
    <>
      <section>
        <div className="listsContainer">
          <div className="cartContainer">
          <form className="formContainer" onSubmit={handleSubmit}>
            <div className="inputs">
              <div>
                <label>Product</label>
                <input
                  required
                  onChange={(e) => setSearch(e.target.value)}
                />
                <div>
                  {Object.values(productResponse)?.
                    .filter((item) => {
                      return search.toLowerCase() === ""
                        ? null
                        : item.name.toLowerCase().includes(search);
                    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM