javascript - TypeError:无法读取未定义的属性“groupsUrl”
问题描述
试图让我的状态每 5 秒从我的 db api 更新一次,但我收到一个错误,即即使它是状态也没有定义。
我知道 setState 是异步的,但是在 componentDidMount 中它可以读取状态,因此按照我的逻辑,如果 componentDidMount 调用 reSetStates 它肯定能够读取状态。
class Groups extends Component {
state = {
groups : {},
categories : ["id", "name", "last fault", "active"],
groupsInfoList : [],
enabledGroupsInfoList : [],
disabledGroupsInfoList : [],
groupsUrl : 'http://my.api.server/groups'
}
componentDidMount() {
fetch(this.state.groupsUrl)
.then(res => res.json())
.then((data) => {
var groups_object = data['result']
this.setStates(groups_object)
this.reSetStates()
})
.catch(console.log)
}
reSetStates(){
fetch(this.state.groupsUrl)
.then(res => res.json())
.then((data) => {
var groups_object = data['result']
this.setStates(groups_object)
})
.catch(console.log)
setTimeout(this.reSetStates, 3000);
}
解决方案
推荐阅读
- javascript - 获取简单 GET 请求的 NS_BINDING_ABORTED 错误
- c++ - 将数组传递给类方法以进行选择排序
- python - 显示 SyntaxError: invalid syntax 的交互式 Shell 打印函数
- amazon-cognito - 使用 Cognito 用户池对 AppSync 查询控制台进行身份验证
- amazon-web-services - 如何在 Cypress 中合并 Junit XML 报告以与 AWS CB 集成
- python - 如何将 Unicode 字典键和值转换为字符串值?
- r - 多项式回归的ggplot2有问题
- c++ - 为什么在没有 typname 关键字的情况下使用模板参数的类型成员作为函数的返回类型,但在其他地方却不起作用?
- cypress - 当赛普拉斯文档说“在每个测试之间”时,“每个测试”的定义是什么?
- python - 将 dtype=object 转换为二进制值