我正在尝试将 JSX 代码转换为打字稿(TSX),并且在使用useState挂钩时难以传递接口。我想要的只是在我的状态下有一个空数组接口,并且在更新时我将对象一一推送到数组中。
我在更新计数时收到类型错误“‘any[]’类型的参数不可分配给‘SetStateAction’类型的参数。‘any[]’类型不可分配给‘(prevState: IData) => IData”类型.类型“any[]”与签名“(prevState: IData): IData”不匹配
这是我所做的代码:
样本.tsx
const interface IData {
id: number
value: string
}
const Sample = () => {
let [count, setCount] = useState<IData>([]); <------------ Want to know how to pass the blank interface or Array and also how to update interface once values are pushed to the Array
const addCount = () => {
setCount([...count, {
id: count.length,
value: 'Condition ' + count.length
}])
}
const handleRemove = (id:number) => { …Run Code Online (Sandbox Code Playgroud)