输入'字符串| null' 不能分配给类型 'SetStateAction<string>' 的参数。类型 'null' 不能分配给类型 'SetStateAction<string>'

苏巴·雷迪
<Autocomplete
  value={value}
  onChange={(event, newValue) => {
    setValue(newValue);
  }}
  inputValue={inputValue}
  onInputChange={(event, newInputValue) => {
    setInputValue(newInputValue);
  }}
  id="controllable-states-demo"
  options={options}
  style={{ width: 300 }}
  renderInput={(params) => <TextField {...params} label="Controllable" variant="outlined" />}
/>

请帮帮我,我是打字稿的新手...在下图中,您将看到错误。

谢谢。

在此处输入图片说明

根旅行者

正如错误所说,newValue可以是字符串或空值。它需要一个字符串,因此您应该newValue在尝试使用它之前测试 的值

例如:

 if newValue !== nil {
   setValue(newValue)
 } 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章