RECT-在使用效果上和单击元素上运行功能?
我在使用效果挂钩内部有一个功能,该挂钩向服务器请求某些数据:
useEffect(() => {
const reqFn = async () => {
// req code...
setState(req result)
}
return reqfn()
},[setState])
另外,当用户单击按钮以请求其他数据时,我需要运行此功能(REQFN使用USESTATE值作为服务器请求的Prop) 。
我已经尝试在使用效果之外使用reqfn(),但这使得在循环中运行REQFN。 我该如何实现?
I have a funtion inside a useEffect hook that requests some data to my server:
useEffect(() => {
const reqFn = async () => {
// req code...
setState(req result)
}
return reqfn()
},[setState])
Also, I need to run this function when a user clicks on a button to request a different data (reqFn uses a useState value as a prop for the server request).
I've tried taking the reqFn() outside the useEffect, but this makes running reqFn in a loop.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的问题中可以看到,您需要第一个渲染中的API数据,然后您只需单击即可处理更新。因此,您可以使用
使用
而无需状态回调,并使用onclick
处理其余As I see from your question you need api data in the first render and then you are handling updates with just click. So you can use
useEffect
without state callback and handle the rest withonClick