ReactJS调用函数一次,如果设置了查询字符串
解释:
在中使用
我通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它将以
usestate
的初始化为调用,第二次从getQuery
上使用useffect
上的更新数据。您实际上不需要使用
getQuery
函数。您可以直接将来自查询的数据直接设置(初始状态只能在初始渲染上设置,而不是在后续渲染上设置)。Yes, it will call it once for the initialization of
useState
, and second time for the updated data fromgetQuery
on theuseEffect
. You don't really need auseEffect
for thegetQuery
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).在状态
初始化
中使用该函数的坏主意,您可以以本机方式进行此操作,react> react-router-dom
。基于@cristian-florin calina
答案,我提供了新答案: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:Router.query
return all search params and with this there is no need to callgetQuery
function. easy!