reactjs 无法正确重置react-select的值和标签

kuarbcqp  于 2023-04-11  发布在  React
关注(0)|答案(1)|浏览(174)

我有一个带有react-select标签的react组件来过滤表中的数据。我想在某个事件发生时清除select标签的值和标签(我指的是select元素中显示的文本)。
所以我尝试创建一个按钮来触发resetSelect(),但是它不起作用。另一个问题是,当我从select中选择一个选项时,标签不会改变。

export default function Filters({ onFilterChange }) {

  const [types, setTypes] = useState([{}]);
  const [selectedType, setSelectedType] = useState(null);

  const fromJsonToOptions = (json) => {
    const options = [
      { label: "all", value: null },
      ...json.map((obj) => ({
        label: obj.name,
        value: obj._id,
      })),
    ];
    return options;
  };

  useEffect(() => {
    //request to get types to put in select options
    const responseData = response.data;
    const options = fromJsonToOptions(responseData);
    setTypes(options);
  }, []);

  const handleFilterChange = (filterName, filterValue) => {
    if (filterName === "type") {
      setSelectedType(filterValue);
    }
    onFilterChange(filterName, filterValue); //pass data to another component to render the table
  };

  const resetSelect = () => {
    setSelectedType(null);
  };

  return (
      <div>
        <Select
          options={types}
          placeholder="all"
          onChange={(selectedOption) => {
            handleFilterChange("type", selectedOption.value);
          }}
          value={selectedType}
        />
        <button onClick={resetSelect}> reset </button>
    </div>
  );
}
rkue9o1l

rkue9o1l1#

若要清除select标记的value和label,可以将select标记的value和defaultValue属性设置为null。
试试这个:

export default function Filters({ onFilterChange }) {

  const [types, setTypes] = useState([{}]);
  const [selectedType, setSelectedType] = useState(null);

  const fromJsonToOptions = (json) => {
    const options = [      { label: "all", value: null },      ...json.map((obj) => ({        label: obj.name,        value: obj._id,      })),    ];
    return options;
  };

  useEffect(() => {
    //request to get types to put in select options
    const responseData = response.data;
    const options = fromJsonToOptions(responseData);
    setTypes(options);
  }, []);

  const handleFilterChange = (filterName, filterValue) => {
    if (filterName === "type") {
      setSelectedType(filterValue);
    }
    onFilterChange(filterName, filterValue); //pass data to another component to render the table
  };

  const resetSelect = () => {
    setSelectedType(null);
  };

  return (
      <div>
        <Select
          options={types}
          placeholder="all"
          onChange={(selectedOption) => {
            handleFilterChange("type", selectedOption.value);
          }}
          value={selectedType}
          isClearable={true} // Add this line to enable clearing the select
          defaultValue={null} // Add this line to set the default value to null
        />
        <button onClick={resetSelect}> reset </button>
    </div>
  );
}

然后,更新您的fromJsonToOptions

const fromJsonToOptions = (json) => {
    const options = [
      { label: "all", value: null },
      ...json.map((obj) => ({
        label: obj.name,
        value: obj._id,
      })),
    ];
    return options;
};

确保在“选择”组件中也包含标签 prop :

<Select
  options={types}
  placeholder="all"
  onChange={(selectedOption) => {
    handleFilterChange("type", selectedOption.value);
  }}
  value={selectedType}
  isClearable={true}
  defaultValue={null}
  label={selectedType ? selectedType.label : ''}
/>

相关问题