Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error next 13, typescript with fetching data inside async component

HERE ERROR IMAGE

export const Filter = async (): Promise<JSX.Element> => {
  const { data: categories } = await api.get('/categories/')

  return (
    <div className='w-full h-full relative'>
      <Container className='sticky top-[6rem] max-lg:col-span-2'>
        <div className='grid grid-cols-1 gap-6 max-h-[40rem] overflow-y-scroll overflow-hidden'>

           
         There is the problem i think
          <Container className='flex items-center flex-col'>
            <SmallHeading>Kategoria</SmallHeading>
            <ul className='grid grid-cols-1 gap-4 max-h-[25rem] overflow-y-scroll overflow-hidden w-full'>
              {categories.map(c => {
                return <CategoryItem key={c.id} href={c.name} name={c.name} image={c.img} isSmall={true} />
              })}
            </ul>
          </Container>
           


        </div>
      </Container>
    </div>
  )
}

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

Help i’ll try everythink

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried what is in the code I gave above, I don’t understand why such an error appear

>Solution :

I think you can use async components only in app directory. From the next13 docs

This new data fetching model is currently being developed by the React
team. We recommend reading the support for promises React RFC which
introduces async/await in Server Components and a new use() hook for
client components. While you can try it out, it is not yet stable.
We’ll keep these docs updated to reflect the latest developments.

Warning: You can use async/await in layouts and pages, which are
Server Components. Using async/await inside other components, with
TypeScript, can cause errors from the response type from JSX. We are
working with the TypeScript team to resolve this upstream. As a
temporary workaround, you can use {/* @ts-expect-error Server
Component */} to disable type checking for the component.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading