可以非常更新状态第二吗?
我试图在购物车中显示物品的数量。我希望计数每秒更新,因此,如果用户从购物车中添加或删除,它将每秒发出该请求并刷新,更新状态,数字将会更改。我只是尝试了这种方法,它可以正常工作,但是我想知道是否可以这样做,或者是否有更好的方法。
const [update, setUpdate] = useState(0)
const [data, setData] = useState([])
let currentUser = 1
const getData = () => {
axios.get(`http://localhost:4000/api/userCart/${currentUser}`)
.then((res) => {
setData(res.data)
setUpdate(++update)
})
}
useEffect(() => {
getData()
}, [update])
useEffect(() => {
setInterval(() => {
getData()
}, 1000);
},[])
I am trying to display the count of items in a cart. I want the count to update every second so if a user adds or deletes from cart it will make that request every second and refresh, updating the state and the number will change. I just tried this method and it works fine, but I'd like to know if it's ok to do or if there is a better way of doing it.
const [update, setUpdate] = useState(0)
const [data, setData] = useState([])
let currentUser = 1
const getData = () => {
axios.get(`http://localhost:4000/api/userCart/${currentUser}`)
.then((res) => {
setData(res.data)
setUpdate(++update)
})
}
useEffect(() => {
getData()
}, [update])
useEffect(() => {
setInterval(() => {
getData()
}, 1000);
},[])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为没关系,您只需要一种清除此间隔的方法,当您销毁该组件时,
我认为这可能是一个问题,您会遇到一个间隔和效果。
I think thats ok, you need just a way to clear this interval when you destroy the component
your first useEffect I think can be a problem, you made a interval and a effect that runs every get
当您想提供像实时的经验时,这是好的。如果这将是生产的,则需要考虑将完成多少请求以及解决和获取数据所需的时间。
Vercel团队中有一个Pacakge SWR,您可以使用 https://swr.vercel.vercel.app/docs/docs/revalidationation ,它获取数据,验证其状态并在可用时提供缓存状态。 如果您想继续自己的实现,请尝试一下
,那么您需要考虑以下方面:
要在您共享的代码中考虑当前代码
,getData函数被调用两次,一个来自Interval,然后将requestin Data保留,然后在更新
update> UPDATE
prop时再次调用。重构想法可以是这样:
It's okay when you want to give a real-time like experience. If this will be on production you need to consider how many request will be done and the time it can take to resolve and get the data.
There's a pacakge SWR from Vercel team which you can use https://swr.vercel.app/docs/revalidation , it fetches data, validates it's state and serves a cached state when available. Give it a try
If you want to continue with your own implementation then you need to take into consideration this:
To consider in your current code
In the code you shared, the getData function is being invoked twice, one from interval which then keeps requestin data, and again when you update the
update
prop.A refactor idea can be this: