useMemo是常量警告



这是我得到的,它运行良好。

const loadingList = useMemo(() => [], [])

然而,我得到警告使用Chrome浏览器说

'loadingList'是常量

当我把它改成let时,警告就消失了,但是我不明白为什么它不能被let…似乎很奇怪。我使用的所有React钩子都是const。

这是它是如何使用

function addLoadingProcess(process) {
loadingList.push(process)
}
function clearLoadingProcess() {
loadingList = []
setAppIsLoaded(true)
}
function removeLoadingProcess(process) {
const index = loadingList.indexOf(process)
if (index > -1) {
loadingList.splice(index, 1)
}
if (loadingList.length === 0) {
setAppIsLoaded(true)
}
}

指向这个函数

function clearLoadingProcess() {
loadingList = []
setAppIsLoaded(true)

}

不能修改const变量的引用。
这就是为什么编译器抛出错误

'loadingList' is constant 

在你的例子中,你有两个选项:

  • 使用让
  • loadingList = []改为loadingList.length = 0。在这种情况下,我们仍然保持相同的引用,您将能够使用const

p。我不明白你为什么要做这样的事。你的代码闻起来很糟糕。
你能解释一下你想用这个loadingList实现什么吗?

最新更新