【发布时间】:2021-07-08 18:26:22
【问题描述】:
我正在尝试将特定索引处的新项目插入到数组中。
我有这个代码:
const insertAt = (index: number, ...element: QC[]) => {
const arrCopy = [...qt]
return arrCopy.splice(index, 0, ...element)
}
const newQC: QC[] = [
{
id: cuid(),
order: qt.length + 1,
}
]
questionIndex !== undefined &&
setQt(qt => {
return insertAt(questionIndex, ...newQC)
})
这样做的目的是从setQt 返回的数组中删除所有内容。
setQt 是一个useState 函数,因此它必须返回一个新状态。
我的代码可能有什么问题?
【问题讨论】:
标签: javascript arrays reactjs typescript ecmascript-6