我希望狀態依賴於服務器數據。我想用componentWillMount的:this.setState不能在componentWillMount()上工作?
componentWillMount() {
this.setState(async ({getPublicTodosLength}, props) => {
const result = await this.getPublicTodosLengthForPagination();
console.log("result = ", result) // returns the length but not assigned on this.state.getPublicTodosLength
return { getPublicTodosLength: result+getPublicTodosLength }
});
}
getPublicTodosLengthForPagination = async() => { // get publicTodos length since we cannot get it declared on createPaginationContainer
const getPublicTodosLengthQueryText = `
query TodoListHomeQuery {# filename+Query
viewer {
publicTodos {
edges {
node {
id
}
}
}
}
}`
const getPublicTodosLengthQuery = { text: getPublicTodosLengthQueryText }
const result = await this.props.relay.environment._network.fetch(getPublicTodosLengthQuery, {})
return await result.data.viewer.publicTodos.edges.length;
}
沒有價值,但它沒有分配我getPublicTodosLength狀態?我覺得我沒有,因爲結果返回,我想對getPublicTodosLength狀態分配數據在這裏綁定
的價值是存在的,我希望它在getPublicTodosLength轉讓,但我失敗了,無法弄清楚,爲什麼@ 82Tuskers –
如果刪除'會發生什麼async'嵌套在' .setState(...'?(不是具有'async'和'await'結構的專家..:/) – 82Tuskers