如何仅在首次单击反应时获取数据

发布于 2025-01-26 17:27:38 字数 1203 浏览 2 评论 0原文

我在实现 data 中使用的表格时遇到了问题,通过React-Query从数据库中检索,才能在单击输入时一次检索一次,随后的单击不应引起。冰淇淋

如何以干净的方式进行操作?

钩子:

export const useUnitOfMeasureSelectData = ({
  queryPageIndex,
  queryPageSize,
  queryFilters,
  enabled,
  refetchOnWindowFocus,
}) => {
  return useQuery(
    ["unitofmeasure-list", queryPageIndex, queryPageSize, queryFilters],
    () => fetchItemData(queryPageIndex, queryPageSize, queryFilters),
    {
      keepPreviousData: true,
      staleTime: 60000,
      enabled,
      refetchOnWindowFocus,
      select: (data) => {
        const categories = data.data.results.map((obj) => ({
          value: obj.id,
          label: obj.name,
        }));
        return categories;
      },
    }
  );
};

在组件中调用onclick = {()=> refetch()}} recect点击。

  const {
    data: unitOfMeasureSelectData = [],
    refetch: refetchUnitOfMeasureSelectData,
  } = useUnitOfMeasureSelectData({
    queryPageIndex: queryPageIndex,
    queryPageSize: queryPageSize,
    queryFilters: filterString,
    enabled: false,
    refetchOnWindowFocus: false,
  });

I encountered a problem while implementing the form where the data used in select, retrieved from the database via react-query, should only be retrieved once when clicking on input, and subsequent clicks should not cause a refetch.

How to do it in clean way?

Hook:

export const useUnitOfMeasureSelectData = ({
  queryPageIndex,
  queryPageSize,
  queryFilters,
  enabled,
  refetchOnWindowFocus,
}) => {
  return useQuery(
    ["unitofmeasure-list", queryPageIndex, queryPageSize, queryFilters],
    () => fetchItemData(queryPageIndex, queryPageSize, queryFilters),
    {
      keepPreviousData: true,
      staleTime: 60000,
      enabled,
      refetchOnWindowFocus,
      select: (data) => {
        const categories = data.data.results.map((obj) => ({
          value: obj.id,
          label: obj.name,
        }));
        return categories;
      },
    }
  );
};

In component calling onClick={()=>refetch()} refetch data on every click.

  const {
    data: unitOfMeasureSelectData = [],
    refetch: refetchUnitOfMeasureSelectData,
  } = useUnitOfMeasureSelectData({
    queryPageIndex: queryPageIndex,
    queryPageSize: queryPageSize,
    queryFilters: filterString,
    enabled: false,
    refetchOnWindowFocus: false,
  });

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

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

发布评论

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

评论(2

晨敛清荷 2025-02-02 17:27:38

看起来您想要一个无限陈旧时间的懒惰查询:

const [enabled, setEnabled] = useState(false)

useQuery(key, fn, { enabled, staleTime: Infinity })

然后,当您单击输入时,您可以调用setEnabled:true。无限的陈旧时间确保查询始终被认为是新鲜的,因此不再发生背景。

looks like you want a lazy query with infinite stale time:

const [enabled, setEnabled] = useState(false)

useQuery(key, fn, { enabled, staleTime: Infinity })

then, when you click on the input, you call setEnabled: true. The infinite stale time makes sure that the query is always considered fresh, so no more background refetch is happening.

带上头具痛哭 2025-02-02 17:27:38

React/本机中的按钮具有属性禁用,完成后可以将其设置为False。可能的方法可能是状态

<button disabled={this.state.btnDisable}/>

然后您可以在第一次单击按钮时更新状态

//class component
this.setState({btnDisable: false})

//function component
setbtnDisable(false)

The button in react/native has a property disabled that you can set to false when you are done. Probably the way to do this is with state

<button disabled={this.state.btnDisable}/>

Then you can update the state when you first click the button

//class component
this.setState({btnDisable: false})

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