2017-08-27 108 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

我不知道你为什么要这样设置状态,也许你可以帮助解释你在做什么。在此期间不应该这样写: