2017-02-25 39 views
0

如何在第二次獲取使用第一次使用數據時獲取鏈式異步操作?我需要獲取倉庫列表(GitHub API),然後從這些倉庫中獲取用戶。我做了這個:如何使用存儲中的數據獲取動作(react-redux)

export function reposListFetchData(url) { 
    return (dispatch) => { 
    dispatch(reposListIsLoading(true)) 

    fetch(url) 
     .then((response) => { 
     if(!response.ok){ 
      throw Error(response.statusText) 
     } 

     dispatch(reposListIsLoading(false)) 

     return response 
     }) 
     .then((response) => response.json()) 
     .then((repos) => dispatch(reposListFetchSuccess(repos))) 
     .then(this.props.repos.map(
    repo=>this.props.fetchContributorsData(`https://api.github.com/repos/angular/${repo.name}/contributors?per_page=100`) 
    )) 
    .catch(()=> dispatch(reposListHasErrored(true))) 
    } 
} 

但我當然不能在那裏使用this.props。有什麼建議麼?

回答

0

假設fetchContributorsData是一個動作是與reposListFetchData頗爲相似,你應該能夠做到這一點...

export function reposListFetchData(url) { 
    return dispatch => { 
    dispatch(reposListIsLoading(true)); 
    fetch(url) 
     .then(response => { 
     if (!response.ok) { 
      throw Error(response.statusText); 
     } 
     dispatch(reposListIsLoading(false)); 
     return response; 
     }) 
     .then(response => response.json()) 
     .then(repos => { 
     dispatch(reposListFetchSuccess(repos)); 
     // where repos is an array of repo 
     repos.map(repo => 
      dispatch(fetchContributorsData(`https://api.github.com/repos/angular/${repo.name}/contributors?per_page=100`)) 
     ); 
     }) 
     .catch(() => dispatch(reposListHasErrored(true))); 
    }; 
} 
+0

謝謝了很多,它的作品! ;) – Alwox

相關問題