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

React MUI Autocomplete点击选项后未实时更新useState值求助

MUI Autocomplete选中选项后useState无法实时更新问题

我实现了一个支持自定义输入的MUI Autocomplete组件,用户可选择预设选项或自行输入内容,但点击选项后useState并未立即存储选中值,仅在输入框失去焦点时才会更新。对比教程代码并无明显差异,但教程中的组件可在选中选项时实时更新useState值。

<Autocomplete
    key={"casa"+index}
    freeSolo
    autoSelect
    fullWidth
    value={i.casa}
    onChange={e=>ponervalor(e,index)}
    options={casasNamesList}
    id={"casasNamesList"+index}
    size="small"
    color="primary"
    autoComplete={false}
    renderOption={(props, option) => {
       return (
       <span {...props} style={{ backgroundColor: "primary", border:"0.5px solid black"}}>{option}</span>);
    }}
      renderInput={(params) => <TextField {...params} label="Casa" name="casa" variant="outlined" />}
  />

补充说明:我认为autoSelect属性可能与此问题相关,其官方描述为:

If true, the selected option becomes the value of the input when the Autocomplete loses focus unless the user chooses a different option or changes the character string in the input.

但将autoSelect设为false后,选中选项的值始终无法被存储。

内容的提问来源于stack exchange,提问作者Germán

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 13:05:35