-
What it's the best way to refresh the entire list, if I need to trigger it from another component? For example: <AddComment />
// ^^^^^^^^^^ I need to refetch entire list after successfully adding <Comments />
// ^^^^^^^^ I have `mutate()` from *useSWRInfinite* hook @shuding , is there a workaround that I'm missing out? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
The best way is to create a customized hook: const useComments () {
const { data, mutate, size, setSize } = useSWRInfinite()
const comments = data ? [].concat(data) : []
return {
comments,
nextPage: () => setSize(size + 1),
refresh: mutate,
}
} And use it in the component that you need to refresh the list: function Foo () {
const { comments } = useComments()
// render comments
}
...
function Bar () {
const { refresh } = useComments()
return <button onClick={refresh}>refresh</button>
} |
Beta Was this translation helpful? Give feedback.
-
@shuding Is there a way to locally update useSWRInfinite data? Any clean way on making it (feels) instant? :/ Edit: I wonder if is |
Beta Was this translation helpful? Give feedback.
-
I feel like there should be an example for optimistic updates. |
Beta Was this translation helpful? Give feedback.
The best way is to create a customized hook:
And use it in the component that you need to refresh the list: