使用效率重新渲染来自supabase查询的相同结果
我正在使用用户的书签列表。因此,当我尝试在使用效果内进行查询时,它每次都会呈现结果。
const [posts, setPosts] = useState([])
useEffect(() => {
async function getBookmarks() {
await supabase
.from('bookmarks')
.select('post_id')
.match({ user_id: user.id })
.then(res => {
res.data.map(async (bk) => {
await supabase
.from('post')
.select('id,description,songlink,created_at,author,profiles:author(username)')
.match({ id: bk.post_id })
.single()
.then(res => setPosts(posts => [...posts, res.data]))
})
})
}
getBookmarks()
}, [])
我不想获得两次或更多的结果,但我不知道我在做什么错。
I'm working with a user's bookmarks list. So, when I tried to render it doing the queries inside useEffect, it renders the result each time.
const [posts, setPosts] = useState([])
useEffect(() => {
async function getBookmarks() {
await supabase
.from('bookmarks')
.select('post_id')
.match({ user_id: user.id })
.then(res => {
res.data.map(async (bk) => {
await supabase
.from('post')
.select('id,description,songlink,created_at,author,profiles:author(username)')
.match({ id: bk.post_id })
.single()
.then(res => setPosts(posts => [...posts, res.data]))
})
})
}
getBookmarks()
}, [])
I don't want to get the same result twice or more, but I don't know what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好地了解您想要做什么来做些什么可能会有所帮助,而不是为了帮助人们更好地回答这一点。我在代码中看到的是您要数据,然后再次询问。了解表的数据结构是否会有所帮助,我不确定您是否需要在此处进行多个接听电话。
https://supabase.com/docs/docs/reference/reference/reference/javascript/javascript/select/select/select 加入,您应该能够做类似的事情
it might be helpful to get a better understanding of what you're wanting it to do vs what it's actually doing for people to help answer this better. What I'm seeing here in the code is you asking for data and then asking again. Knowing the data structure of your tables could help, and I'm not sure that you need to be doing multiple fetch calls here.
https://supabase.com/docs/reference/javascript/select
if your tables are joined, you should be able to just do something like
您可能需要使用Usecallback挂钩。没有尝试代码,但是您可以得到这个想法:
You might want to use useCallback hook. Didn't try the code but you can get the idea: