检查此解决方案:
我的组件:
export function submitEdit(dataPath: string, payload: ActionWithCallback<number>): ActionWithDataPathBase<ActionWithCallback<number>> {
return {
type: constants.EDIT_SUBMIT,
dataPath,
payload
};
}
行动:
export function submitEdit(dataPath: string, payload: ActionWithCallback<number>): ActionWithDataPathBase<ActionWithCallback<number>> {
return {
type: constants.EDIT_SUBMIT,
dataPath,
payload
};
}
ActionWithCallBack:
export interface ActionWithCallback<T> {
data: T,
callback?: ((errors?: SubmissionErrors | undefined) => void)
}
传奇:
function* update(action: ActionWithDataPathBase<ActionWithCallback<number>>) {
try {
const formData: PeopleForUpdateModel = yield select((state: AppState) => get(state.forms, action.dataPath));
yield call(ajax.put, `/people/${action.payload.data}`, formData);
}
catch (error) {
if (action.payload.callback) {
const errors = handleFormErrors(error.response);
yield call(action.payload.callback, errors);
}
}
finally {
yield put(actions.changeValue("edit.isLoading", false));
}
}
HandleFormErrors 函数将我的服务器响应解析为接受的最终表单错误格式SubmissionErrors