Skip to content

Observables support #437

Locked Answered by sergiodxa
mateja176 asked this question in General
Jun 9, 2020 · 5 comments · 3 replies
Discussion options

You must be logged in to vote

What do you think of an API like this one:

const { data } = useSWR("/api/messages", fetcher, {
  subscribe(key, mutate) {
    const unsubscribe = ws.subscribe(key, (newMessage) =>
      mutate((currentMessages) => currentMessage.concat(newMessage))
    );
    return unsubscribe;
  },
});

The new option subscribe is a function receiving the key and the bound mutate, here it starts the subscription and update the cache when there is a new value, and it returns a function to unsubscribe when the component unmount.

Replies: 5 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

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

@pke
Comment options

@sergiodxa
Comment options

Answer selected by mateja176
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
Converted from issue

This discussion was converted from issue #437 on June 10, 2020 09:23.