为了账号安全,请及时绑定邮箱和手机立即绑定

TypeError:无法读取未定义的属性“groupsUrl”

TypeError:无法读取未定义的属性“groupsUrl”

慕雪6442864 2022-10-08 10:11:25
试图让我的状态每 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);  }
查看完整描述

1 回答

?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

这是绑定问题,改变这个

reSetStates(){

至:

reSetStates = () => {


查看完整回答
反对 回复 2022-10-08
  • 1 回答
  • 0 关注
  • 74 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信