gpt4 book ai didi

javascript - React 中的 typescript : State will not be placed in the constructor will cause an error

转载 作者:行者123 更新时间:2023-12-03 01:06:33 37 4
gpt4 key购买 nike

enter image description here如图所示,State不会被放置在构造函数中会导致错误。但是,如果状态放置在构造函数内,则不会。

但是,如果你为状态声明一个类型,也可以。

enter image description here

最佳答案

当你这样做

state = {
quota: null
}

替换继承的state类型定义。正确的做法是添加注释

state: IState = {
quota: null
}

这就是你已经想出来的。

当您在构造函数中执行此操作时,您并不是重新定义,只是进行分配,因此会自动使用继承的 IState 类型。

关于javascript - React 中的 typescript : State will not be placed in the constructor will cause an error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52378283/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com