ReactJS调用函数一次,如果设置了查询字符串

发布于 2025-02-13 05:00:46 字数 1231 浏览 0 评论 0原文

解释:

中使用我通过getProducts()函数基于给定的data,数据包含搜索过滤器和将通过用户更新,因此需要实时观看,例如,数据包含一个{brand:'x'}的对象

const [data, setData] = useState({});

useEffect(() => {
  getProducts(data) // get products via api based on data, if data is clear, it will return all products
    .then(data => {
      //
    });
}, [data]) // watch data in realtime and send it to getProducts()

useEffect(() => {
  getQuery(); // check if there are search querys
}, [])

。 ,它将检查如果在重新加载页面时搜索参数中有任何查询字符串,并且会获取查询字符串并将其设置为data,并且在上面的代码中,如果data get feet Update get Update update update update update update update update update update undive it it it it in the cope> data getProducts(),实际上它更新了产品。

const getQuery = () => {
  let obj_url = new URL(window.location.href);
  let params = obj_url.searchParams;
  let searchParams = new URLSearchParams(params);

  // some other code 
  setData(obj); // get query and set it to data
}

问题:

我注意到URL中有查询字符串时,它将调用getProducts()两次!好吧,这个代码肯定是这样做的,但是我不想要它,我该如何防止打电话两次?我只想如果没有一次查询字符串一次,如果没有一次。

Explain:

In useEffect I get products via getProducts() function based on given data and data contains search filters and will be update by user so need to watch it in realtime, for example data contains an object like this {brand: 'x'}

const [data, setData] = useState({});

useEffect(() => {
  getProducts(data) // get products via api based on data, if data is clear, it will return all products
    .then(data => {
      //
    });
}, [data]) // watch data in realtime and send it to getProducts()

useEffect(() => {
  getQuery(); // check if there are search querys
}, [])

Also there is a getQuery() function, it will check if there are any query strings in search params when page is reload, and will get query strings and set it to data, and by above code if data get update it will call getProducts() again, actually it update products.

const getQuery = () => {
  let obj_url = new URL(window.location.href);
  let params = obj_url.searchParams;
  let searchParams = new URLSearchParams(params);

  // some other code 
  setData(obj); // get query and set it to data
}

Problem:

I noticed while there are query strings in url it will call getProducts() twice! well this code definitely does that, but I don't want it, how can I prevent to call this twice? I just want if there are query strings call once, if not call once.

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

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

发布评论

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

评论(2

罪歌 2025-02-20 05:00:46

是的,它将以usestate的初始化为调用,第二次从getQuery上使用useffect上的更新数据。您实际上不需要使用 getQuery函数。您可以直接将来自查询的数据直接设置(初始状态只能在初始渲染上设置,而不是在后续渲染上设置)。

const [data, setData] = useState(getQuery());

useEffect(() => {
  getProducts(data) // get products via api based on data, if data is clear, it will return all products
    .then(data => {
      //
    });
}, [data]) // watch data in realtime and send it to getProducts()

const getQuery = () => {
  let obj_url = new URL(window.location.href);
  let params = obj_url.searchParams;
  let searchParams = new URLSearchParams(params);

  // some other code
  return obj ?? {}
}

Yes, it will call it once for the initialization of useState, and second time for the updated data from getQuery on the useEffect. You don't really need a useEffect for the getQuery function. You can just set the data from query on the initial state directly (the initial state will only be set on the initial render anyway, not on subsequent ones).

const [data, setData] = useState(getQuery());

useEffect(() => {
  getProducts(data) // get products via api based on data, if data is clear, it will return all products
    .then(data => {
      //
    });
}, [data]) // watch data in realtime and send it to getProducts()

const getQuery = () => {
  let obj_url = new URL(window.location.href);
  let params = obj_url.searchParams;
  let searchParams = new URLSearchParams(params);

  // some other code
  return obj ?? {}
}
未央 2025-02-20 05:00:46

在状态初始化中使用该函数的坏主意,您可以以本机方式进行此操作,react> react-router-dom。基于@cristian-florin calina答案,我提供了新答案:

import { useRouter } from 'next/router';

const Router = useRouter();
const [data, setData] = useState(Router.query);

useEffect(() => {
  getProducts(data)
    .then(data => {
      //
    });
}, [data]);

router.query返回所有搜索参数,因此无需调用getQuery 功能。 简单!

Bad idea to use that function in state initialization, you can do this with native way, react-router-dom. Based on @Cristian-Florin Calina answer, I provide new answer:

import { useRouter } from 'next/router';

const Router = useRouter();
const [data, setData] = useState(Router.query);

useEffect(() => {
  getProducts(data)
    .then(data => {
      //
    });
}, [data]);

Router.query return all search params and with this there is no need to call getQuery function. easy!

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