0
據我所知,reducer中總是有一個靜態的initialState,現在我想異步從遠程服務器獲取初始狀態,我知道如何獲取它們,我可以在componentWillMount在相關的表示組件中,但它不是隻用於表示?在與存儲連接之前,我應該在哪裏放置提取代碼並獲取初始狀態?在reducer中反應native native async初始狀態
getInitailState =() => {
return (
fetch(apis.GETINITAILURL)
.then((response)=>response.json())
.then((responseJson) => {
return responseJson;
})
.catch(e=>e)
)
}
這裏是原來的完整代碼減速器:
import * as TYPES from '../actions/types.js';
import ApiUtils from '../utils/ApiUtils.js';
const initailTaskState = [{
"taskid": 1,
"priority": 3,
"desc": "Bug fix",
"timestamp": 5
}]
const tasks = (state = initailTaskState , action) => {
switch(action.type){
case TYPES.ADD_TASK:
return [
...state,
action.task
]
case TYPES.DELETE_TASK:
return state.filter(task => task.taskid !== action.taskid);
default:
return state
}
}
export default tasks;
謝謝,假設我需要使用thunk中間件? – inoutwhy
對不起,我從來沒有用過thunk。但我發現了一個很好的討論[這裏](http://stackoverflow.com/questions/34570758/why-do-we-need-middleware-for-async-flow-in-redux)。即使我現在正在消化它:p – Swapnil