試圖讓我的狀態每 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); }
TypeError:無法讀取未定義的屬性“groupsUrl”
慕雪6442864
2022-10-08 10:11:25