Skip to content

SSR support like Apollo Client's getDataFromTree? #594

Answered by sergiodxa
niamleeson asked this question in General
Discussion options

You must be logged in to vote

What I usually do is to keep SWR only for client-only data, if I need something with SSR and SWR I create a useMyData Hook calling SWR and I export it together with the fetcher.

export function getTodos() {
  // fetch todos and return them
}

export function useTodos(options) {
  return useSWR("todos", getTodos, options)
}

Then you can use useTodos inside your components and call getTodos inside getStaticProps or getServerSideProps, and pass them to useTodos as initial data.

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@niamleeson
Comment options

Answer selected by niamleeson
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
2 participants