好的,所以我们才刚刚开始使用Redux和Sagas,并正在使用它们从服务器获取一些异步数据(一个JsonSchema)。
获取后,状态树如下所示:
{
"forms": {
"form-url": {
"isLoading": false,
"schema": { ... }
}
}
}
Reducer大致如下:
const reducer = (state = {}, action) => {
switch (action.type) {
case FETCH:
const form = {};
form[action.url] = {
isFetching: true,
schema: {},
};
return Object.assign({}, state, form);
case FETCH_SUCCESS:
const sform = {};
sform[action.url] = {
isFetching: false,
schema: action.schema,
};
return Object.assign({}, state, sform);
}
return state;
}
现在,当将状态映射到prop且尚未获取数据时,我们需要一个三元函数使其起作用:
const mapStateToProps = (state, ownProps) => {
return {
schema: state.forms[ownProps.sourceUrl] ? state.forms[ownProps.sourceUrl].schema : {}
}
}
即使这行得通,但我的间谍意识却告诉我这是一种代码味道。有没有建议的模式来解决缺失的初始状态?