Ant Design 动态数据未绑定到 Select

Ant Design Dynamic Data does not binds to Select

我正在为一个学校项目使用 React。动态数据不绑定到 Ant Design 的 Options,react js。结果是一个空的选项列表。实际上,数据来自外部 API。出于测试目的,我将数据分配给状态变量。数据以二维数组形式出现,因此我对数据进行了两次映射。

结果是:

import { useEffect, useState } from "react";
import { Select } from "antd";
const { Option } = Select;

const Complete = () => {
    const [list, setPersons] = useState([
        [
            {
                id: 1,
                personName: "Owan",
            },
            {
                id: 2,
                personName: "More",
            },
            {
                id: 3,
                personName: "Jaila",
            },
            {
                id: 4,
                personName: "Eerov",
            },
        ],
        [
            {
                id: 5,
                personName: "Rell",
            },
            {
                id: 6,
                personName: "Juko",
            }
        ]
    ]);


    

    useEffect(() => {
        console.log(list);
    }, []);

    return (
        <Select
            showSearch
            style={{ width: 200 }}
            placeholder="Select a person"
            optionFilterProp="children"
            
            filterOption={(input, option) =>
                option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
            }
        >
            {list.map((l) => {

                l.map((person) => {
                    console.log(person);
                    <Option value={person.id}>
                        {person.personName}
                    </Option>;
                });
            })}
        </Select>
    );
};

export default Complete;

尝试使用以下代码:

import { useEffect, useState } from "react";
import { Select } from "antd";
const { Option } = Select;

const Complete = () => {
  const [list, setPersons] = useState([
    [
      {
        id: 1,
        personName: "Owan"
      },
      {
        id: 2,
        personName: "More"
      },
      {
        id: 3,
        personName: "Jaila"
      },
      {
        id: 4,
        personName: "Eerov"
      }
    ],
    [
      {
        id: 5,
        personName: "Rell"
      },
      {
        id: 6,
        personName: "Juko"
      }
    ]
  ]);

  useEffect(() => {
    console.log(list);
  }, [list]);

  return (
    <Select
      showSearch
      style={{ width: 200 }}
      placeholder="Select a person"
      optionFilterProp="children"
      filterOption={(input, option) =>
        option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
      }
    >
      {list.map((l) => (
        <>
          {l.map((person) => (
            <Option value={person.id}>{person.personName}</Option>
          ))}
        </>
      ))}
    </Select>
  );
};

export default Complete;

您只需要使用 ES6 功能 更新小的,这里的脚本将 2 维数组合并为单个数组

{[].concat(...list).map((l) => <Option value={l.id}>{l.personName}</Option>)}

这里是完整的脚本:

import { useEffect, useState } from "react";
import { Select } from "antd";
const { Option } = Select;

const Complete = () => {
    const [list, setPersons] = useState([
        [
            {
                id: 1,
                personName: "Owan",
            },
            {
                id: 2,
                personName: "More",
            },
            {
                id: 3,
                personName: "Jaila",
            },
            {
                id: 4,
                personName: "Eerov",
            },
        ],
        [
            {
                id: 5,
                personName: "Rell",
            },
            {
                id: 6,
                personName: "Juko",
            }
        ]
    ]);


    

    useEffect(() => {
        console.log(list);
    }, []);

    return (
        <Select
            showSearch
            style={{ width: 200 }}
            placeholder="Select a person"
            optionFilterProp="children"
            
            filterOption={(input, option) =>
                option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
            }
        >
            {[].concat(...list).map((l) => <Option value={l.id}>{l.personName}</Option>)}
        </Select>
    );
};

export default Complete;

访问实例demo