简体   繁体   English

了解React-Redux中的useDispatch和dispatch

[英]Understanding useDispatch and dispatch in React-Redux

I am trying to understand the code that my past colleague has written.我试图理解我过去的同事编写的代码。

From what I understand, useDispatch takes in an object that contains the action type and payload , which will be compared to all the reducers and the state will be changed accordingly .据我了解, useDispatch 接受一个包含 action type 和 payload 的 object ,与所有减速器进行比较, state 将相应更改 However, in the below code, he passes an entire function instead of an object .但是,在下面的代码中,他传递了整个 function 而不是 object My question is why can useDispatch still function without passing in an object , as the function does not return anything.我的问题是为什么useDispatch 仍然 function 没有传入 object ,因为 function 不返回任何东西。

Below is the useDispatch function and the action function.下面是 useDispatch function 和 action function。

    const dispatch = useDispatch();

    const handleSubmit = (e: any): void => {
        dispatch(login("email", "password"));
    };
export const login =
    (email: string, password: string) =>
    async (dispatch: Dispatch<LoginActions>) => {
        try {
            // Update the store
            dispatch({
                type: USER_ACTIONS.USER_LOGIN_REQUEST,
            });


            const data = await loginRequest({ email, password });

            dispatch({
                type: USER_ACTIONS.USER_LOGIN_SUCCESS,
                payload: data,
            });
        } catch (error: any) {
            dispatch({
                type: USER_ACTIONS.USER_LOGIN_FAIL,
                payload:
                    error.response && error.response.data.message
                        ? error.response.data.message
                        : error.message,
            });
        }
    };

For redux itself, you can only dispatch action which is a plain JS object(or an action creator returns an action, this action is a plain JS object must have a type field.).对于 redux 本身,您只能调度作为普通 JS 对象的动作(或者动作创建者返回一个动作,此动作是普通 JS object 必须具有type字段。)。 But with various middlewares, you can dispatch various things such as a thunk, plain JS object action, etc. The middleware will transform the action to plain JS object action in the end.但是使用各种中间件,您可以调度各种东西,例如 thunk、普通 JS object 动作等。中间件最终会将动作转换为普通 JS object 动作。

When using the redux-thunk middleware, you can dispatch a thunk like this:当使用redux-thunk中间件时,你可以像这样发送一个 thunk:

const thunkFunction = (dispatch, getState) => {
  // logic here that can dispatch actions or read state
}

store.dispatch(thunkFunction)

The login thunk action creator does return a thunk. login thunk 操作创建者确实返回了一个 thunk。

A thunk action creator is a function that may have some arguments, and returns a new thunk function. thunk 动作创建者是一个 function,它可能有一些 arguments,并返回一个新的 thunk function。 See Writing Thunks请参阅 编写 Thunks

Another example, redux-promise-middleware ,另一个例子, redux-promise-middleware

Given a single action with an async payload, the middleware transforms the action to separate pending action and a separate fulfilled/rejected action, representing the states of the async action.给定具有异步负载的单个操作,中间件将操作转换为单独的待处理操作和单独的已完成/已拒绝操作,表示异步操作的状态。

You can dispatch an action with async payload:您可以使用异步有效负载调度操作:

const response = dispatch({
    type: 'FIRST',
    payload: new Promise(...),
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM