最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chain querymutation calls with RTK Query using React Hooks - Stack Overflow

programmeradmin3浏览0评论

In the example bellow I'm trying to create a simple login/authentication ponent with React and Redux Toolkit.

const Login = () => {
    const dispatch = useDispatch();
    const [login, { isLoading }] = useLoginMutation();
    const [loginData, setLoginData] = useState({
        email: '',
        password: '',
    });

    const loginHandler = async () => {
        try {
            const result = await login({
                email: loginData.email,
                password: loginData.password,
            }).unwrap();

            const { token, userId } = result;

            const { data } = await dispatch(
                backendApi.endpoints.getUser.initiate({ token, userId })
            );

            dispatch(
                setCredentials({
                    user: {
                        email: data.user.email,
                        id: data.user.id,
                        name: data.user.name,
                        type: data.user.type,
                    },
                    token,
                })
            );
        } catch (err) {
            console.log(err);
        }
    };

    return (
        <Space>
            {isLoading ? (
                <SpinnerIcon />
            ) : (
                // setLoginData and loginHandler are set and used here
                // Standard React code: Input onChange -> setLoginData / Submit button onClick -> loginHandler
                // (nothing special, code shortened for the sake of readability)
                <FormWithEmailPasswordInputsAndButton />
            )}
        </Space>
    );
};

This works fine, the problem is that:

I want to make use of the isLoading property on the getUser query result object so that the <SpinnerIcon /> ponent will be visible until both requests are done.

As it is, the spinnerIcon ponent is only shown during the login API call because I'm not using React Hooks to call getUser query therefore I don't have the isLoading property from getUser available to the rest of the ponent.

But if I want to use React Hooks to make the call I'll need the result from the login mutation beforehand.

const [getUser, { isLoading: isUserLoading }] = useGetUserQuery(resultFromLoginMutation);

And I don't know how to do that. One after the other, so that I can make use of both isLoading properties.

In the example bellow I'm trying to create a simple login/authentication ponent with React and Redux Toolkit.

const Login = () => {
    const dispatch = useDispatch();
    const [login, { isLoading }] = useLoginMutation();
    const [loginData, setLoginData] = useState({
        email: '',
        password: '',
    });

    const loginHandler = async () => {
        try {
            const result = await login({
                email: loginData.email,
                password: loginData.password,
            }).unwrap();

            const { token, userId } = result;

            const { data } = await dispatch(
                backendApi.endpoints.getUser.initiate({ token, userId })
            );

            dispatch(
                setCredentials({
                    user: {
                        email: data.user.email,
                        id: data.user.id,
                        name: data.user.name,
                        type: data.user.type,
                    },
                    token,
                })
            );
        } catch (err) {
            console.log(err);
        }
    };

    return (
        <Space>
            {isLoading ? (
                <SpinnerIcon />
            ) : (
                // setLoginData and loginHandler are set and used here
                // Standard React code: Input onChange -> setLoginData / Submit button onClick -> loginHandler
                // (nothing special, code shortened for the sake of readability)
                <FormWithEmailPasswordInputsAndButton />
            )}
        </Space>
    );
};

This works fine, the problem is that:

I want to make use of the isLoading property on the getUser query result object so that the <SpinnerIcon /> ponent will be visible until both requests are done.

As it is, the spinnerIcon ponent is only shown during the login API call because I'm not using React Hooks to call getUser query therefore I don't have the isLoading property from getUser available to the rest of the ponent.

But if I want to use React Hooks to make the call I'll need the result from the login mutation beforehand.

const [getUser, { isLoading: isUserLoading }] = useGetUserQuery(resultFromLoginMutation);

And I don't know how to do that. One after the other, so that I can make use of both isLoading properties.

Share Improve this question asked Jun 24, 2021 at 19:22 Gustavo MáximoGustavo Máximo 6,3003 gold badges20 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can use skipToken or the skip option:


const [login, { isLoading, data: resultFromLoginMutation, isSuccess }] = useLoginMutation();
const [getUser, { isLoading: isUserLoading }] = useGetUserQuery(resultFromLoginMutation, { skip: !isSuccess });

or

const [login, { isLoading, data: resultFromLoginMutation }] = useLoginMutation();
const [getUser, { isLoading: isUserLoading }] = useGetUserQuery(resultFromLoginMutation ?? skipToken);
发布评论

评论列表(0)

  1. 暂无评论