【发布时间】:2017-12-26 15:40:43
【问题描述】:
在服务器上,我有 JSON 模式,用于收集枚举或最小值/最大值。然后数据以表格或其他方式显示。
componentDidMount() {
this.props.dispatch(genders());
this.props.dispatch(races());
this.props.dispatch(ages());
}
在每个功能(性别、种族、年龄)中都是这样的:
export const genders = () => (dispatch) => {
dispatch(requestedProperty('genders'));
dispatch(schema())
.then(schema => dispatch(receivedProperty('genders', schema.properties.general.properties.gender.enum)));
};
架构函数访问 JSON 架构的地方如下:
const schema = (method = 'GET') => (dispatch) => {
dispatch(requestedSchema(method.toUpperCase()));
return axios.get(`schema/v1/demand/${method.toLowerCase()}.json`)
.then((response) => {
dispatch(receivedSchema(response.data));
return response.data;
});
};
单个文件有多个请求。我想只下载一次文件并将内容存储到 Redux 商店。然后,我想通过getState访问接收到的数据。
我尝试将getState 传递给schema 函数并下载文件以防万一我在 Redux 商店中还没有任何东西,但我每次都得到空对象。
为了完整起见,我有这个减速器:
export const schema = (state = { fetching: false }, action) => {
switch (action.type) {
case REQUESTED_DEMAND_SCHEMA:
return { ...state, fetching: true };
case RECEIVED_DEMAND_SCHEMA:
return { ...state, schema: action.schema, fetching: false };
case RECEIVED_DEMAND_SCHEMA_PROPERTY:
return { ...state, [action.property]: action.value };
default:
return state;
}
};
对于这个特定问题我应该使用什么模式?
【问题讨论】:
标签: json reactjs redux redux-thunk