简体   繁体   English

如何使用useState声明一个字符串数组,然后在使用Typescript时更新?

[英]How to use useState to declare an array of strings, and then update it when using Typescript?

I have the following我有以下

interface ISTx {
    tx: Array<string>,
    setTx: any
}

const [tx, setTx]  = useState<ISTx>([]) //Argument of type 'never[]' is not assignable to parameter of type 'ISTx

setTx(oldArr  => [...oldArr , txHash]) //Argument of type '(oldArr: ISTx) => any[]' is not assignable to parameter of type 'SetStateAction<ISTx>

But im receiving the errors that are commented in the blockcode above.但是我收到了上面块代码中注释的错误。

Im basically trying to create a state that is an array of strings, and push new strings into it.我基本上试图创建一个 state ,它是一个字符串数组,并将新字符串推入其中。

const [tx, setTx] = useState<string[]>([]);

You declare the type of your state between the angled brackets (<>).您在尖括号 (<>) 之间声明 state 的类型。 React will provide the necessary types for tx and setTx . React 将为txsetTx提供必要的类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM