【问题标题】:type 'string | null' is not assignable to parameter of type 'SetStateAction<string>'.Type 'null' is not assignable to type 'SetStateAction<string>'输入'字符串 | null' 不可分配给“SetStateAction<string>”类型的参数。类型“null”不可分配给“SetStateAction<string>”类型
【发布时间】:2021-07-14 08:50:03
【问题描述】:
<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" />}
/>

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

谢谢。

【问题讨论】:

    标签: reactjs typescript autocomplete material-ui


    【解决方案1】:

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

    例如:

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

    【讨论】:

    • 谢谢@roottraveller :)
    猜你喜欢
    • 2021-06-12
    • 2021-08-01
    • 2021-09-06
    • 1970-01-01
    • 2020-04-17
    • 2022-11-28
    • 2018-04-05
    • 2021-07-21
    • 2021-09-30
    相关资源
    最近更新 更多