You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-Admin v4中AutocompleteInput如何获取选中项?

React-Admin v4 中 AutocompleteInput 获取选中项的替代方案

在React-Admin v3中,AutocompleteInput的onSelect回调直接接收选中的选项对象作为参数,但升级到v4后,该组件改用MUI Autocomplete实现,onSelect的参数签名发生了变化:第一个参数是SyntheticEvent对象,第二个参数才是选中的选项值,这也是你遇到Property 'id' does not exist on type 'SyntheticEvent<HTMLDivElement, Event>'错误的原因。

要实现v3中类似的功能,只需调整onSelect的参数接收逻辑,从第二个参数中获取选中项:

<AutocompleteInput
  onSelect={(event, value) => {
    // 可选:判断value是否存在,避免未选中时的错误
    if (value) {
      setCurrentCategory({ reference: value.id });
    }
  }}
/>

另外,如果你需要监听选中值的变更,也可以直接使用MUI Autocomplete的onChange属性(React-Admin的AutocompleteInput会透传所有MUI Autocomplete的props),效果一致:

<AutocompleteInput
  onChange={(event, value) => {
    if (value) {
      setCurrentCategory({ reference: value.id });
    }
  }}
/>

内容的提问来源于stack exchange,提问作者André Ricardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 16:45:21