如何使用React Query的使用QueryQuery用onsubmit函数以Formik formik formik表单获取数据

发布于 2025-01-26 10:07:00 字数 1802 浏览 2 评论 0原文

我真的是反应问题的新手,所以我面对这个问题。这是场景。 我的组件中有一个formik表单。它的表具有带有数据列表的表格,该表可从初始获取请求获得,并以Formik表单包裹的搜索表格,该表格允许用户过滤数据。填写搜索表格后,用户必须单击过滤器,以执行另一个获取请求,然后替换表中的数据。

我尝试使用的一种方法是通过从formik表单中传递参数来使用Refetch函数,但它不起作用:

这是我的挂钩:

USETRANSACTIONS.TSX

import { useQuery, useQueryClient } from 'react-query';
import { API } from "api";


interface ITransactionsData { 
  mobile?: string,  
}

export const payload: ITransactionsData = {
  mobile: '',  
}

const getTransactions = (payload: ITransactionsData) => {
  return API.get()('transactions', payload);
}

export const useTransactionListData = (payload: ITransactionListData) => {
  return useQuery(
    'transactions', 
    () => getTransactions(payload),
    {      
      keepPreviousData: true,         
    }
  )
}

请注意,对于上面的挂钩文件,我在其中有有效负载。此有效负载将导出到表单组件中,我不希望将其绑定到usequery作为变量,因为我只希望在用户单击搜索按钮上时将使用Querquery拆除。另外,我不想在Usestate中保存此值,并将其用作USEQUERY中的变量,因为我已经将其保存在Formik的初始价值中。

这是我的组件:(有效负载是从钩子文件导出的)

const Form = () => {
    const { isLoading, isFetching, data: queryResult, isError, error, refetch} = useTransactionListData(payload);

    const handleSearch = (values: any, { setSubmitting }: any) => { 
    //refetch();
    refetch(values) <- I'm trying to pass in the values from formik to do a refetch with the new query params but it doesn't work...

  }

   return (
      <Formik
        initialValues={{        
          mobile: payload.mobile,              
        }}      
        onSubmit={handleSearch}
        ... (Formik Antd form here which holds a mobile number field). 
       setFieldValue will change the values.mobile
      </Formik>
    )
}

是否有解决方案,还是我应该考虑使用USEMUNT?不确定这是否是做事的正确方法,似乎我必须复制Usetransactionslistdata功能相似的版本,而是使用USEMONT。

I'm really new to React-Query so I an facing this issue. Here's the scenario.
I have a component with a Formik form in it. It has a table with the list of data which is obtained from an initial GET request and a search form wrapped in a formik form that allows user to filter data. The user must click on filter once the search form is filled up to do another GET request which will then replace the data in the table.

One way I tried was to use the refetch function by passing in the parameters from the formik form but it doesn't work:

Here's my hook:

useTransactions.tsx

import { useQuery, useQueryClient } from 'react-query';
import { API } from "api";


interface ITransactionsData { 
  mobile?: string,  
}

export const payload: ITransactionsData = {
  mobile: '',  
}

const getTransactions = (payload: ITransactionsData) => {
  return API.get()('transactions', payload);
}

export const useTransactionListData = (payload: ITransactionListData) => {
  return useQuery(
    'transactions', 
    () => getTransactions(payload),
    {      
      keepPreviousData: true,         
    }
  )
}

Note that for the hook file above, I have a payload with mobile in it.This payload gets exported to the Form component and I do not wish to bind this to the useQuery as a variable since I only want the useQuery to be refetched when the user clicks on the search button. Also, I don not want to save this value with useState and use that as a variable in the useQuery since I'm already saving it in Formik's initialValues.

And here's my component: (payload is exported from the hooks file)

const Form = () => {
    const { isLoading, isFetching, data: queryResult, isError, error, refetch} = useTransactionListData(payload);

    const handleSearch = (values: any, { setSubmitting }: any) => { 
    //refetch();
    refetch(values) <- I'm trying to pass in the values from formik to do a refetch with the new query params but it doesn't work...

  }

   return (
      <Formik
        initialValues={{        
          mobile: payload.mobile,              
        }}      
        onSubmit={handleSearch}
        ... (Formik Antd form here which holds a mobile number field). 
       setFieldValue will change the values.mobile
      </Formik>
    )
}

Is there a solution for this or should I consider using useMutation instead? Not sure if thats the proper way of doing things though and seems like I have to duplicate the useTransactionsListData function a similar version but using useMutation instead.

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

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

发布评论

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

评论(1

所有深爱都是秘密 2025-02-02 10:07:00

您必须制作有效负载查询密钥的一部分,因为有效载荷是您查询的“依赖关系”。当键更改时,React-Query将始终自动恢复:

export const useTransactionListData = (payload: ITransactionListData) => {
  return useQuery(
    ['transactions', payload], 
    () => getTransactions(payload),
    {      
      keepPreviousData: true,         
    }
  )
}

<

  • a href =“ https://reaect-query.tanstack.com/guides/query-keys-keys#if-your-query-query-query-function-deperts-on -a-variable-include-in-in-in-your-Query-key“ rel =”文档中的nofollow noreferrer“>
  • 在我的博客

中当键更改为新密钥时,来自上一个键的数据 - 它没有静态查询键,例如'Transactions'

有效载荷的好“存储”将是url。当用户单击按钮时,您可以写入URL,然后通过useparams订阅查询(如果使用React Router)。结果,您将免费获得可共享的URL。

You'd have to make payload part of the query key, as payload is a "dependency" for your queryFn. react-query will always refetch automatically when the key changes:

export const useTransactionListData = (payload: ITransactionListData) => {
  return useQuery(
    ['transactions', payload], 
    () => getTransactions(payload),
    {      
      keepPreviousData: true,         
    }
  )
}

This is documented:

keepPreviousData: true will make sure that you'll see data from the previous key when the key changes to a new key - it does nothing with a static query key like 'transactions'.

A good "storage" for the payload would be the url. You can write to the url when the user clicks the button, and then have your query subscribe via useParams (if you are using react router). As a result, you'll get sharable urls for free.

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