0

in my pages/_app.js i have this:

  if (process.browser) {
    myidb.init().then((result) => {
      console.log('setup complete! Got:', result)

      return (
        <AppLayout>
          <ItemProvider idbstore={result}>
            <Component { ...pageProps} />
          </ItemProvider>
        </AppLayout>
      )
    })
    .catch((error) => {
      console.error('could not setup browser db!', error)
      return(
        <h1>Error - we never get here tho, cos setup up always succeeds, but TOO LATE to be useful.</h1>
      )
    })
  }

The ItemProvider is just a functional HOC, essentially:

export const ItemProvider = ({children, idbstore}) => {
  // ... setup stuff
  return (<ItemContext.Provider value={...}>{children}</ItemContext.Provider>)
}

From logging, I see:

  • the react context provider is called immediately, before myidb.init() has a chance to finish opening the indexeddb connection, so the context has incomplete setup, and subsequent indexeddb calls fail once the app is running.
  • the <ItemProvider> is never called again by nextjs, so has no chance to recover. Only the targeted page component rebuilds.

What am I doing wrong? Why does the promise.then() not work as expected in _app.js and how can i wait for an indexed db to be setup before _app.js renders my page components?

yen
  • 1,769
  • 2
  • 15
  • 43
  • 1
    Is it not possible to `async await` this instead? – NicholasG04 Jul 02 '20 at 14:01
  • @NicholasG04 yeah i probably should. Will do that next and see if it uncovers anything. – yen Jul 02 '20 at 14:04
  • @NicholasG04 actually that helped fix my problem. Your async/await prompt caused me to look up [this SO answer](https://stackoverflow.com/questions/57847626/using-async-await-inside-a-react-functional-component) and I ended up cleaning out _app.js completely and letting manage indexeddb setup via `useEffect` hooks. THANKS!!! :D – yen Jul 02 '20 at 15:16
  • That is for sure a better solution, glad I could help in some way :) – NicholasG04 Jul 02 '20 at 15:30

0 Answers0