【问题标题】:Mui Autocomplete says onChange value can be stringMui Autocomplete 说 onChange 值可以是字符串
【发布时间】:2022-06-10 17:13:10
【问题描述】:

我正在使用我的自定义类型呈现 MUI 自动完成功能,如下所示:

type Option = { label: string; value: string };
return (
  <MuiAutocomplete<Option, true, any, true>
    freeSolo={true}
    onChange   // says value can be type Option | string
  />
);

但是,onChange 签名表明值的类型可以是 Option | string 而不仅仅是 Option

请看这里:https://codesandbox.io/s/freesolo-demo-material-ui-forked-sn8l5i?file=/demo.tsx

有人知道这是为什么吗?

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    你还没有定义 onChange 函数

    type Option = { label: string; value: string };
    const handleOnChange = (e) => {
      console.log('Changed')
    }
    return (
      <MuiAutocomplete<Option, true, any, true>
        freeSolo={true}
        onChange={handleOnChange}   // says value can be type Option | string
      />
    );
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 2012-06-28
      • 2020-04-02
      • 2010-12-11
      • 2022-01-10
      • 2020-07-27
      • 1970-01-01
      • 2015-02-26
      • 2013-02-04
      相关资源
      最近更新 更多