【发布时间】:2018-04-24 19:00:45
【问题描述】:
我正在使用 typescript-fsa 和 typescript-fsa-reducers 包在 TypeScript React 应用程序中简单地创建动作和减速器。
const actionCreator = actionCreatorFactory();
export function signInHandler(state: UserState, action: Action): UserState {
// ????
return { ...state };
}
export const signIn = actionCreator.async<SignInRequest, RequestResponse<SignInResponse>>("USER_SIGNIN");
export const UserReducer = reducerWithInitialState({ signedIn: false } as UserState)
.casesWithAction([signIn.started, signIn.done], signInHandler)
.build();
在组件中的使用:
export default connect<StateProps, DispatchProps>(
(state: RootState) => ({} as StateProps),
(dispatch: Dispatch<RootState>) => {
return {
signIn: (userName: string, password: string) => dispatch(signIn.started(new SignInRequest(userName, password)))
};
}
)(SignIn);
现在我被困住了。我不知道如何对我的 API 进行 HTTP 调用,因此当来自 API 的响应到达时,我可以在组件调度下一个动作时发送请求。我想使用承诺。 如何解决?
【问题讨论】:
-
你看过
typescript-fsa中关于异步操作的文档吗? github.com/aikoven/typescript-fsa#async-action-creators -
是的,但我不知道应该在哪里进行 API 调用。动作,动作创建者还是减速者?我想看一个例子
标签: reactjs typescript promise redux