私の状態に2つのオブジェクトがあり、要求にid
を渡してデータを取得するための共通APIを呼び出します。reduxで複数のアクションクリエーターをディスパッチするための共通APIの使い方
私のは、私の状態でsectionAを更新するためにreceiveSectionAを起動する最初のセクションを呼び出します。
export function fetchSection(sectionCode){
return(dispatch,getState,api)=>{
const endPoint = 'url/?sectionCode='+sectionCode
const method = 'GET'
const isAuth = true
const promise = api(endPoint,method,isAuth)
promise
.then(response =>
response.json().then(json => ({
status:response.status ,
json
})
))
.then(
({ status, json }) => {
if(status >= 200 && status < 300) {
const sectionDictionary = utils.convertSectionsToDictionary(camelizeKeys(json))
dispatch(receiveSectionA(sectionDictionary))
}
if (status >= 400) {
//throw error
}
},
err => {
console.log("error"+err);
}
);
}
}
は今、私は、次の火災sectionBに同じ電話をかける: -
dispatch(receiveSectionB(sectionDictionary))
今すぐ上記
fetch
呼び出しが同じであるため、私は、これは一般的なことができますどのような方法があります。あまりにも多くのコードリピートがあると私は感じています。
sectionCodeに基づいて異なるアクションを送信するスイッチケースについて考えていましたが、私は約20のセクションを持ち、コードが実際に複雑になると思います。
これを処理する方が良いですか?
これは興味深いようです。しかし予期しない結果はどういう意味ですか? –
答えがリンクで更新されました..読む、学ぶ、忘れる...:D –