简体   繁体   English

ReactJS:“(bullets:never[])=&gt; string[]”类型的参数不可分配给“SetStateAction”类型的参数<never[]> '</never[]>

[英]ReactJS: Argument of type '(bullets: never[]) => string[]' is not assignable to parameter of type 'SetStateAction<never[]>'

I am fetching data from API and populating the input texts from the fretched email.我正在从 API 获取数据,并从经过处理的 email 填充输入文本。 I'm using bullet and setBullet to maintain the state.我正在使用 bullet 和 setBullet 来维护 state。 The problem is that I must define an initial item into the use state like this:问题是我必须在使用 state 中定义一个初始项目,如下所示:

const [bullet, setBullet] = useState(['Temp']);

But defining it like this it obviously adds "Temp" as the first input and then all emails follow.但是像这样定义它显然会添加“Temp”作为第一个输入,然后是所有电子邮件。 But if I define the state like this:但是,如果我这样定义 state :

const [bullet, setBullet] = useState([]);

I get errors in onChangeHandler and map function in useEffect.我在 useEffect 中的 onChangeHandler 和 map function 中出现错误。

const onChangeHandler = (index: number, value: string) => {
    setBullet((bullets) =>
      bullets.map((bullet, i) => (i === index ? value : bullet))
    );
  };

  useEffect(() => {
    fetch(
      "https://reqres.in/api/users?page=2")
        .then((res) => res.json())
        .then((json) => {
          json.data.map((data: any) => {
            setBullet((bullets) => [...bullets, data.email]);
            return console.log(data.email);
          })
        })
  }, [])

Error comes in setState function: setState function 出现错误:

Argument of type '(bullets: never[]) => string[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
  Type '(bullets: never[]) => string[]' is not assignable to type '(prevState: never[]) => never[]'.
    Type 'string[]' is not assignable to type 'never[]'.
      Type 'string' is not assignable to type 'never'

To summarise this is what I'm getting.总结这就是我得到的。 I want to remove the 'Temp' bullet from it.我想从中删除“临时”项目符号。

在此处输入图像描述

Any help will be much appreciated.任何帮助都感激不尽。

Thank You谢谢你

You can pass whichever type you want to useState using generics.您可以使用useState传递您想要使用的任何类型的状态。 If you don't pass in the type, useState will try to infer it from the initial value.如果你不传入类型, useState将尝试从初始值推断它。 Since you're passing in an empty array, the type can't be inferred automatically so you have to do it manually:由于您传递的是一个空数组,因此无法自动推断类型,因此您必须手动进行:

const [bullet, setBullet] = useState<string[]>([]);

暂无
暂无

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

相关问题 Typescript never[] 类型的参数不可分配给 SetStateAction 类型的参数<never[]></never[]> - Typescript Argument of type never[] is not assignable to parameter of type SetStateAction<never[]> 类型参数不可分配给“SetStateAction”类型的参数<never[]> &#39;。 在反应中 - Argument of type is not assignable to parameter of type 'SetStateAction<never[]>'. in React “void”类型的参数不可分配给“SetStateAction”类型的参数<never[]> '.ts(2345)</never[]> - Argument of type 'void' is not assignable to parameter of type 'SetStateAction<never[]>'.ts(2345) 类型错误:“字符串”类型的参数不可分配给“SetStateAction”类型的参数<number> '?</number> - Type error: Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'? &#39;{ identifier: string; 类型的参数 }&#39; 不可分配给类型为 &#39;ConcatArray 的参数<never> &#39; - Argument of type '{ identifier: string; }' is not assignable to parameter of type 'ConcatArray<never>' ReactJS:未知类型的参数不可分配给 SetStateAction 类型的参数<string>来自文件阅读器</string> - ReactJS: Argument of type unknown is not assignable to parameter of type SetStateAction<string> from filereader 'string' 类型的参数不可分配给 typeScript 中的 'never' 类型的参数 - Argument of type 'string' is not assignable to parameter of type 'never' in typeScript “状态”类型的参数不能分配给“从不”类型的参数 - Argument of type 'State' is not assignable to parameter of type 'never' 类型 &#39;string&#39; 不可分配给类型 &#39;never&#39; - Type 'string' is not assignable to type 'never' “AnyAction”类型的参数不能分配给“never”类型的参数。 无法追踪 - Argument of type 'AnyAction' is not assignable to parameter of type 'never'. unable to track it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM