#'Promise Element' is not a valid JSX element

2 messages · Page 1 of 1 (latest)

balmy lake
#

Hello.
I am trying to use data fetching in app folder. I get this error when trying to insert component into layout:

TS2786: 'Header' cannot be used as a JSX component.
   Its return type 'Promise<Element>' is not a valid JSX element.
     Type 'Promise<Element>' is missing the following properties from type 'ReactElement<any, any>': type, props, key

What am I doing wrong here?
My layout file:

export default function RootLayout ({ children }: {
  children: React.ReactNode
}) {
  return (
    <html>
    <body>
    <Header/>
    {children}
    </body>
    </html>
  )
}

And my header file which causes this error:

async function Header () {
  const siteVariablesRes = await fetch('http://localhost:4000/api/globals/siteVariables')
  const siteVariables: {
    siteName: string
    phone: string
  } = await siteVariablesRes.json()

  return (
    <header className={styles.header}>
      ...
    </header>
  )
}

If contents of header are relevant, I can post them too, but there is just plain markup with only accessing siteVariables.
Despite this error, my app seems to be working in dev mode just fine.

brazen needle
#

Your not doing anything wrong, the typescript definitions are currently wrong, they need to be updated by the typescript team to allow for this. In the mean time I have just been disabling type checking in next config. Check out https://youtu.be/h_9Vx6kio2s for a full explanation.

Next.js 13 just released!!! It's super cool. Sadly it has some dumb problems for us Typescript fans. Hopefully this helps

LINK TO MALTE'S GIST: https://gist.github.com/cramforce/b5e3f0b103f841d2e5e429b1d5ac4ded

ALL MY CONTENT IS FILMED LIVE ON TWITCH AT https://twitch.tv/theo
ALL MY BEST MEMES ARE ON TWITTER FIRST https://twitter.com/t3dotgg
A...

▶ Play video