Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade react-query to v3 #518

Closed
flybayer opened this issue Feb 26, 2021 · 3 comments · Fixed by blitz-js/blitz#2101
Closed

Upgrade react-query to v3 #518

flybayer opened this issue Feb 26, 2021 · 3 comments · Fixed by blitz-js/blitz#2101

Comments

@flybayer
Copy link
Member

What do you want and why?

We need to upgrade to react-query v3. This is a fairly sizable task and will required some typescript skills.

Possible implementation(s)

Read the v2 to v3 migration guide and makes all changes required.

  • Upgrade react-query v3
  • Add new queryClient providers
  • Update types for our useQuery hooks (there's some types renamed, etc in v3)
  • Add new useQueries hook
  • Change template to use useQueryErrorResetBoundary
  • ... any other changes needed
  • Update our docs for all changes

To Figure Out

@philipj93
Copy link
Contributor

philipj93 commented Feb 27, 2021

How about something like this for allowing the user to specify both the query client and opt into the hydration by passing the client and dehydrated state as props to a custom QueryProvider component:

import type { FC } from 'react'

import { QueryClient, QueryClientProvider } from 'react-query'
import { Hydrate } from 'react-query/hydration'

interface Props {
  client: QueryClient
  state?: unknown
}

const QueryProvider: FC<Props> = ({ client, state, children }) => (
  <QueryClientProvider client={client}>
    <Hydrate state={state}>
      {children}
    </Hydrate>
  </QueryClientProvider>
)

export default QueryProvider

and then in _app.tsx we can just let the user define the queryClient or provide one like this:

const queryClient = new QueryClient()

function MyApp({ Component, pageProps, router }: CustomAppProps) {
  const { reset } = useQueryErrorResetBoundary()
  const getLayout = Component.getLayout || ((page) => page)

  return (
    <ErrorBoundary FallbackComponent={RootErrorFallback} resetKeys={[router.asPath]} onReset={reset}>
      <QueryProvider client={queryClient} state={pageProps.dehydratedState}>
        {getLayout(<Component {...pageProps} />)}
      </QueryProvider>
    </ErrorBoundary>
  )
}

export default MyApp

I use this approach now in my Next.js apps and like it 🥳

@flybayer
Copy link
Member Author

flybayer commented Feb 27, 2021

@tundera yes that is close to what I was picturing except that we need to own the queryCache internally so it can be used outside the react tree.

But now that I'm thinking more about it, I think we can do this with just a config object. And then in a babel plugin we'll wrap the default export like withBlitz(MyApp, config).

import {AppConfig} from 'blitz'

export const config: AppConfig = {
  queryClient: {
    //...all query client options except cache
  }
}

function MyApp({ Component, router }: CustomAppProps) {
  const { reset } = useQueryErrorResetBoundary()
  const getLayout = Component.getLayout || ((page) => page)

  return (
    <ErrorBoundary FallbackComponent={RootErrorFallback} resetKeys={[router.asPath]} onReset={reset}>
      {getLayout(<Component {...pageProps} />)}
    </ErrorBoundary>
  )
}

export default MyApp

But then actually why not just put that config inside blitz.config.js along with everything else?

@philipj93
Copy link
Contributor

@flybayer That makes sense that the cache would need to be internal. I like your idea of wrapping the default export in a withBlitzApp function. A nice side-effect of that approach is that it would simplify the process of moving existing Next apps to Blitz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants