2016-07-29 3 views
0

に私はAPIに私のアプリを結ぶことだし、アクションはこのの解析データは、アクション

export function getDayRoutes(date){ 
const request = api().get(`/rest/routes/${date}`); 
let routes = null; 

//code here to transform the data 
request.then((response)=>{ 
    routes = response.data; 
}); 

return { 
    type: GET_DAYROUTES, 
    payload: routes 
} 
} 

のように見える私はAPIからの受信および送信したデータを解析して変換することです生データを持たずにレデューサーに送信してください。

ここにアクションでコンポーネント内で行うのではなく、これを行う方法はありますか?または私は減速機に変換機能を移動する必要がありますか?

+1

私は実際にあなたが求めているものは得られません。 'response.data'が返されました。そこに何らかの変換を行い、結果を 'payload'で与えることができます – xiaofan2406

答えて

1

はい、可能です。 thunk middleware for reduxを使用してください。あなたがあなたの反応を得るとき、それを必要に応じてフィートに変更して、それを減速機に送ります。ベローは例です:

export function getDayRoutes(date){ 
    return (dispatch) => { 
     fetch('/rest/routes/' + date).then((response)=>{ 
      routes = response.data; 
      dispatch({ 
       type: GET_DAYROUTES, 
       payload: parseResponse(response); 
      }); 
     }); 
    } 
} 

const parseResponse = (response) => { 
    //return desired modified response 
} 
0

あなたの話はasynchronous actionsです。

テストされていない
export function getDayRoutes(date){ 
    const request = api().get(`/rest/routes/${date}`); 
    let routes = null; 

    //code here to transform the data 
    return request.then((response)=>{ 
    routes = response.data; 
    return { 
     type: GET_DAYROUTES, 
     payload: routes 
    }; 
    }); 
} 

、あなたのニーズに正確にそれを適用する方法をReduxのドキュメントに表示されます:あなたの例から

それは次のようになります。