use*_*642 5 javascript reactjs jhipster react-redux react-thunk
用例:我有一个用 JHipster 生成的 React 应用程序。我需要从 API 获取数据,映射到表单合同,然后提交表单。
问题:JHipster 生成的减速器代码不返回承诺,那么我如何知道减速器操作何时完成?获取实体更新状态后如何调用函数?
获取实体并返回 ICrudGetAction:
export interface IPayload<T> { // redux-action-type.ts (JHipster Code from node_modules)
type: string;
payload: AxiosPromise<T>;
meta?: any;
}
export type IPayloadResult<T> = ((dispatch: any) => IPayload<T> | Promise<IPayload<T>>);
export type ICrudGetAction<T> = (id: string | number) => IPayload<T> | ((dispatch: any) => IPayload<T>);
// Start of my reducer code
export default (state: AdState = initialState, action): AdState => {
switch (action.type) {
case REQUEST(ACTION_TYPES.FETCH_MYENTITY_LIST):
return {
...state,
errorMessage: null,
updateSuccess: false,
loading: true
};
case FAILURE(ACTION_TYPES.FETCH_MYENTITY_LIST):
return {
...state,
loading: false,
updating: false,
updateSuccess: false,
errorMessage: action.payload
};
case SUCCESS(ACTION_TYPES.FETCH_MYENTITY_LIST): {
const links = parseHeaderForLinks(action.payload.headers.link);
return {
...state,
loading: false,
links,
entities: loadMoreDataWhenScrolled(state.entities, action.payload.data, links),
totalItems: parseInt(action.payload.headers['x-total-count'], 10)
};
}
case ACTION_TYPES.RESET:
return {
...initialState
};
default:
return state;
}
};
export const getEntity: ICrudGetAction<IMyEntity> = id => {
const requestUrl = `${apiUrl}/${id}`;
return {
type: ACTION_TYPES.FETCH_ENTITY,
payload: axios.get<IMyEntity>(requestUrl)
};
};
Run Code Online (Sandbox Code Playgroud)
我尝试这样做并且它有效但给了我编译错误:
this.props.getEntity(this.props.match.params.id).then((response) => {
// Map to form attributes and call setState
});
Run Code Online (Sandbox Code Playgroud)
我收到错误消息: TS2339:类型 'IPayload | 上不存在属性'then' ((调度:任何)=> IPayload)'。类型“IPayload”上不存在属性“then”。
这是有道理的,因为我们没有返回承诺。但是我如何更新代码以返回一个承诺,这样我就不会破坏所有自动生成的东西,同时保持 redux 存储的更新?
| 归档时间: |
|
| 查看次数: |
406 次 |
| 最近记录: |