简体   繁体   English

输入'字符串 | null' 不可分配给“SetStateAction”类型的参数<string> '.Type 'null' 不可分配给类型 'SetStateAction<string> '</string></string>

[英]type 'string | null' is not assignable to parameter of type 'SetStateAction<string>'.Type 'null' is not assignable to type '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" />}
/>

Please, help me out, I am new to typescript... In the below image you will see the error.请帮帮我,我是 typescript 的新手...在下图中,您将看到错误。

Thankyou.谢谢你。

在此处输入图像描述

As the error says, newValue can be either a string or null.正如错误所说, newValue可以是字符串或 null。 It requires a string, so you should test the value of newValue before you try to use it.它需要一个字符串,因此您应该在尝试使用它之前测试newValue的值。

For example:例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:“字符串”类型的参数不可分配给“SetStateAction”类型的参数<number> '?</number> - Type error: Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'? Typescript/React-Native:'string | 类型的参数 null' 不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>' “文件”类型的参数不可分配给“SetStateAction”类型的参数<string> '</string> - Argument of type 'File' is not assignable to parameter of type 'SetStateAction<string>' “未知”类型的参数不可分配给“SetStateAction”类型的参数<string> '</string> - Argument of type 'unknown' is not assignable to parameter of type 'SetStateAction<string>' '用户 | 类型的参数 null”不能分配给类型为“SetStateAction”的参数<user | null> '</user> - Argument of type 'User | null' is not assignable to parameter of type 'SetStateAction<User | null>' '日期 | 类型的参数 null”不能分配给类型为“SetStateAction”的参数<date> '</date> - Argument of type 'Date | null' is not assignable to parameter of type 'SetStateAction<Date>' TypeScript 错误:类型“void”不可分配给类型“SetStateAction”<string> '</string> - TypeScript error: Type 'void' is not assignable to type 'SetStateAction<string>' 输入“调度<setstateaction<any[]> >' 不可分配给类型 '(values?: string) => void' </setstateaction<any[]> - Type 'Dispatch<SetStateAction<any[]>>' is not assignable to type '(values?: string) => void' “响应”类型的参数不可分配给“SetStateAction”类型的参数 - Argument of type 'Response' is not assignable to parameter of type 'SetStateAction` 与类型 &#39;{ id: string; 的参数混淆; 名称:字符串; }[]&#39; 不可分配给类型为 &#39;SetStateAction 的参数<never[]> &#39; - Confusion with Argument of type '{ id: string; name: string; }[]' is not assignable to parameter of type 'SetStateAction<never[]>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM