componentDidMount() { eventProxy.on('event', async (id) => { const getList = (id) => { return $http.get('api/list' + id) .then(res => res.data.data) } const list = await getList(id) console.log(list) this.setState({ data: list}, () => console.log(this.state.data)) })}整個方法寫在componentDidMount()函數里,eventProxy會監聽一個event事件,并接收一個id參數,在回調函數中使用這個id參數發送請求獲取一個數組,再將這個數組賦值到this.state.data。然而,console.log(list)這句話已經成功打印出數組了,console.log(this.state.data)這句卻并沒有執行,請問是為什么呢?應該怎么解決?by the way, state.data 在初始化的時候是個空數組。
在事件監聽的回調函數里調用this.setState()函數無效
侃侃爾雅
2019-03-07 18:19:40