preact-router icon indicating copy to clipboard operation
preact-router copied to clipboard

'Router' cannot be used as a JSX component.

Open ljmerza opened this issue 2 years ago • 1 comments

when trying to build i get

src/app.tsx:29:10 - error TS2786: 'Router' cannot be used as a JSX component.
  Its instance type 'Router' is not a valid JSX element.
    Property 'refs' is missing in type 'Router' but required in type 'ElementClass'.

29         <Router>

ljmerza avatar Jul 08 '22 16:07 ljmerza

Same here, using [email protected] and [email protected]

It’s a problem with the typescript types, the functionality is fine.

codepunkt avatar Jul 15 '22 18:07 codepunkt

Just add this to your tsconfig.json

// TypeScript >= 4.1.1
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "preact",
    //...
  }
}

Described here: https://preactjs.com/guide/v10/typescript/#typescript-configuration

bezenson avatar Jan 20 '23 16:01 bezenson

Can't reproduce, might've been a TS issue?

Going to close out, feel free to reply with more info if it's still an issue.

rschristian avatar May 04 '23 18:05 rschristian

Have you solved this problem?

EquinoxFlowerJG avatar Oct 05 '23 06:10 EquinoxFlowerJG

Have you solved this problem?

My solution didn't help? https://github.com/preactjs/preact-router/issues/426#issuecomment-1398645009

bezenson avatar Oct 05 '23 07:10 bezenson

Have you solved this problem?

My solution didn't help? #426 (comment)

I have added that in my tsconfig.json before. But it still can not work. Then, I found that the default exported Router is indeed not a JSX component. I changed the import Router from 'preact-router'; to import { Router } from 'preact-router'; because the useage example in README.md do that sometimes. In fact, both of these ways are used in the README.md, which makes me very confused. There are still many areas in the README.md that confuse me, not just this one.

EquinoxFlowerJG avatar Oct 05 '23 11:10 EquinoxFlowerJG

{/* @ts-expect-error Server Component /} {/ @ts-expect-error */}

Weirdly enough, copying either of those in the line before the component that throws the error, seems to fix the Problem for now. Remove the {} if you are inside a return, so just /* @ts-expect-error */

Luk139 avatar Feb 21 '24 13:02 Luk139