2017-08-27 107 views
0

碼是這樣的:如何在反應componentDidMount中設置狀態與方法?

componentDidMount() { 
    this.setState(({getPublicTodosLength}, props) => ({ 
     getPublicTodosLength:() => this.getPublicTodosLengthForPagination() // no returned value 
    })); 
    } 
    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 result.data.viewer.publicTodos.edges.length; 
    } 

getPublicTodosLengthForPagination不被調用和返回值不是assigned.Also,當我調用它馬上例如無()=>它的賦值是一個承諾?我期待int/number,edge.length的返回值。幫幫我?

+0

控制檯中的任何錯誤? – stybl

+0

@stybl沒有錯誤,當我立即調用它時,例如without()=>它被分配給一個承諾? –

回答

0

返回的值未分配,因爲您沒有調用該函數而是分配它。

componentDidMount() { 
    this.setState(({getPublicTodosLength}, props) => ({ 
     getPublicTodosLength: this.getPublicTodosLengthForPagination() 
    })); 
    } 
0

我不知道你爲什麼要這樣設置狀態,也許你可以幫助解釋你在做什麼。在此期間不應該這樣寫: