useMutation 状态始终为“空闲”;和“正在加载”总是“假” - 反应查询

发布于 2025-01-11 01:07:48 字数 1848 浏览 0 评论 0原文

所以我使用react-query来处理API请求。当前的问题是,当我尝试提交表单数据、发布请求时,突变状态始终处于空闲状态,并且加载始终为 false。我还使用 zustand 进行状态管理。

这是 useSubmitFormData 挂钩。 Post 请求按预期执行,只是突变状态和 isLoading 没有改变。

export const useSubmitFormData = () => {
  const { postDataPlaceholder } = usePlaceholderApi();
  // data which is submiting is getting from the store - reducer
  const { data, filesToUpload } = useFormDataStore();

  const { mutate, status, isLoading } = useMutation(() => postDataPlaceholder({ data }), {
    onMutate: () => console.log('onMutate', status),
    onSuccess: (res) => console.log(res),
    onError: (err) => console.log('err', err),
  });

  return {
    submitForm: mutate,
    isLoading,
  };
};

现在在 FormPage.jsx 上它是这样触发的:

const { submitForm, isLoading } = useSubmitFormData();

const onSubmit = () => submitForm();

这就是 usePlaceholderApi 的样子。它是一种自定义钩子,旨在结合使用 axios 和拦截器来处理授权令牌。

const usePlaceholderApi = () => {
  const { post } = usePlaceholderAxios();

  return {
    postDataPlaceholder: async (data) => post('/posts', { data }),
  };
};

export default usePlaceholderApi;

这就是usePlaceholderAxios。

import axios from 'axios';

const usePlaceholderAxios = () => {
  axios.interceptors.request.use(async (config) => {
    const api = 'https://jsonplaceholder.typicode.com';

    if (config.url.indexOf('http') === -1) {
      // eslint-disable-next-line no-param-reassign
      config.url = `${api}${config.url}`;
    }

    return config;
  });

  return {
    get: (url, config) => axios.get(url, config),
    post: (url, data, config) => axios.post(url, data, config),
  };
};

export default usePlaceholderAxios;

有什么想法这里可能会出问题吗?我错过了什么吗?还尝试直接在mutation中调用axios,中间不使用usePlaceholderApi钩子,但结果相同。

So I use react-query to handle API requests. Current problem is that when i try to submit form data, post request, mutation state is always idle, and loading is always false. I also use zustand for state management.

This is useSubmitFormData hook. Post request executes as expected, just mutation status and isLoading does not get changed.

export const useSubmitFormData = () => {
  const { postDataPlaceholder } = usePlaceholderApi();
  // data which is submiting is getting from the store - reducer
  const { data, filesToUpload } = useFormDataStore();

  const { mutate, status, isLoading } = useMutation(() => postDataPlaceholder({ data }), {
    onMutate: () => console.log('onMutate', status),
    onSuccess: (res) => console.log(res),
    onError: (err) => console.log('err', err),
  });

  return {
    submitForm: mutate,
    isLoading,
  };
};

Now on FormPage.jsx it is triggered like this:

const { submitForm, isLoading } = useSubmitFormData();

const onSubmit = () => submitForm();

And this is how usePlaceholderApi looks like. It is kind of custom hook in purpose to use axios in combination with interceptors to handle authorization token.

const usePlaceholderApi = () => {
  const { post } = usePlaceholderAxios();

  return {
    postDataPlaceholder: async (data) => post('/posts', { data }),
  };
};

export default usePlaceholderApi;

And this is usePlaceholderAxios.

import axios from 'axios';

const usePlaceholderAxios = () => {
  axios.interceptors.request.use(async (config) => {
    const api = 'https://jsonplaceholder.typicode.com';

    if (config.url.indexOf('http') === -1) {
      // eslint-disable-next-line no-param-reassign
      config.url = `${api}${config.url}`;
    }

    return config;
  });

  return {
    get: (url, config) => axios.get(url, config),
    post: (url, data, config) => axios.post(url, data, config),
  };
};

export default usePlaceholderAxios;

Any ideas what could go wrong here ? Am I missing something ? Also tried to call axios directly in mutation, without usePlaceholderApi hook in between, but same outcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2025-01-18 01:07:48

我设法通过创建钩子流来解决完全相同的问题。
就我而言,我更改了将两个独立的钩子导入到一个钩子中。

从这里

const { tasks, createTask, deleteTask } =
    useMutateTask()

  const { handleSubmit, onSubmit, register } =
    useTaskForm()

到这里

  const {
    handleSubmit,
    onSubmit,
    register,
    tasks,
    createTask,
    deleteTask,
  } = useTaskForm()

I managed to solve exactly the same problem by creating a flow of hooks.
In my case, I changed importing two separated hooks into one hook.

From this

const { tasks, createTask, deleteTask } =
    useMutateTask()

  const { handleSubmit, onSubmit, register } =
    useTaskForm()

To this

  const {
    handleSubmit,
    onSubmit,
    register,
    tasks,
    createTask,
    deleteTask,
  } = useTaskForm()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文