所以快速回答是否定的,没有必要。为什么 ?好吧,您还没有真正使用 redux。如果您查看您在替换props getAjax 中所做的 ajax 调用,我在您的代码库中检查了它,并看到您在那里收到请求后在组件中调用 setState。
使用 redux,你更愿意使用 action 和 reducer。该操作将被处理,调用 api,并在接收到此数据后使用 reducer 设置 redux “store”中的状态。
好的,一个完整的示例将类似于以下内容,只需首先添加redux-thunk,它肯定会帮助您继续前进,请务必阅读自述文件中的示例以更好地了解如何以及为什么。
function startLoading() {
return {
type: 'LOADING_STARTED',
isLoading: true
}
}
function doneLoading(){
return {
type: 'LOADING_ENDED',
isLoading: false
}
}
function setActivity(result) {
let lastGist = result[0];
let activity = {
identifier: result.display.identifier,
title: (result.display.hasOwnProperty('advisorTypeCategory'))? result.display.advisorTypeCategory : '',
name: (result.hasOwnProperty('details') && result.details.hasOwnProperty('activityName')) ? result.details.activityName : '',
desc: (result.hasOwnProperty('details') && result.details.hasOwnProperty('activityDescription')) ? result.details.activityDescription : '',
backgroundImg: (result.display.hasOwnProperty('image')) ? 'http://bungie.net' + result.display.image : '',
modifiers: (result.hasOwnProperty('extended') && result.extended.hasOwnProperty('skullCategories')) ? result.extended.skullCategories : [],
bosses: (result.hasOwnProperty('bosses')) ? result.bosses : [],
items: (result.hasOwnProperty('items') && result.display.identifier == "xur") ? result.items : [],
bounties: (result.hasOwnProperty('bounties')) ? result.bounties : []
}
return {
type: 'SET_ACTIVITY',
activity: activity
}
}
export function findActivity(activity_id) {
return dispatch => {
dispatch(startLoading())
$.get(activity_id, (result)=>{
dispatch(doneLoading())
if(response.status == 200){
dispatch(setActivity(response.json))
}else {
dispatch(errorHere)
}
})
}
}
所以一开始它可能看起来有点吓人,但经过一两次之后,以这种方式做事会感觉更自然,而不是在组件中。