Skip to content
Discussion options

You must be logged in to vote

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>
}

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@vixeven
Comment options

@wildfrontend
Comment options

@aryaniyaps
Comment options

Answer selected by vixeven
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants