简体   繁体   English

使用多个 axios 请求分派不同的操作

[英]Dispatching different actions with multiple axios requests

I have a get method sending different requests and i want to dispatch an action for any request i send.我有一个发送不同请求的 get 方法,我想为我发送的任何请求调度一个动作。

See the relevant code见相关代码

export function getTeamsStats(league, team, type) {
  return function(dispatch) {

    let url1 = "https://www.api-football.com/demo/api/v2/statistics/891/490/2020-01-09"
    let url2 = "https://www.api-football.com/demo/api/v2/statistics/891/490/2020-02-09"
    let url3 = "https://www.api-football.com/demo/api/v2/statistics/891/490/2020-03-09"

    let urlArray = [url1,url2,url3]

    let promiseArray = urlArray.map(url => axios.get(url));

    const api = true;

    if (api) {
      return axios({
        method: "get",
        headers: {
          "x-rapidapi-host": "api-football-v1.p.rapidapi.com",
          "x-rapidapi-key": ""
        }
      })
      .all(promiseArray)
      .then(res => {
        const {
          matchsPlayed: { home: teamsMatchsPlayedHome, away: teamsMatchsPlayedAway},
          wins: { home: teamsStatsWinHome, away: teamsStatsWinAway },
          draws: { home: teamsStatsDrawHome, away: teamsStatsDrawAway },
          loses: { home: teamsStatsLoseHome, away: teamsStatsLoseAway }
        } = res.data.api.statistics.matchs;
        const teamStats = {
          teamsMatchsPlayedHome,
          teamsMatchsPlayedAway,
          teamsStatsWinHome,
          teamsStatsWinAway,
          teamsStatsDrawHome,
          teamsStatsDrawAway,
          teamsStatsLoseHome,
          teamsStatsLoseAway
         }
        dispatch(receivedTeamsStat(teamStats, type));

For any of these 3 urls i want to do this dispatch dispatch(receivedTeamsStat(teamStats, type));对于这 3 个 url 中的任何一个,我都想做这个 dispatch dispatch(receivedTeamsStat(teamStats, type));

I was able to do the same dispatch with just a single url request and get the sates but now i do not know how to achieve the same thing doing the dispatch for each url.我只需一个 url 请求就可以进行相同的dispatch并获得状态,但现在我不知道如何为每个 url 实现相同的dispatch

This logic may help you.这个逻辑可以帮助你。 You can use forEach in .then()您可以在.then()中使用forEach

.then( res => res.forEach( result => dispatch({type: FETCH_DATA_SUCESS, payload:  result.data}))

Inside:里面:

...

if (api) {
  return axios({
    method: "get",
    headers: {
      "x-rapidapi-host": "api-football-v1.p.rapidapi.com",
      "x-rapidapi-key": ""
    }
  })
  .all(promiseArray)
  .then( res => {
        ...

        res.forEach( result => dispatch({type: FETCH_DATA_SUCESS, payload:  result.data}))

        ...
  })

  ...

You can initialize a state您可以初始化一个 state

this.state={
  myArray:[]
}

For each api call, you can modify the state as follows对于每个 api 调用,您可以修改 state 如下

get('URL').then((res)=>{
    //Some extra code here to create object to be appended and maybe to check if the get was successfull
    let x = this.state.myArray;
    x.push(object);
    this.setState(x);
})

As for date,至于日期,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM