1

I am a beginner in react with typescript, facing issue in following code

Here is the code of dashboard component

export default function Dashboard() {
return(
  <p>Dashboard page</p>
 )
 }

Here is code of route

const AppRouter = () => {
  return (
    <Router history={history}>
      <div>
        <Switch>
          <Route path={"/dashboard" } component={Dashboard} />

          <Route path="/" component={Login} />
        </Switch>
      </div>
    </Router>
  );
};

export default AppRouter;

In route code I am facing type issue i.e.

TypeScript error in E:/Web/src/router/route.tsx(18,51):

No overload matches this call.

Overload 1 of 2, '(props: Readonly): Route', gave the following error. Type '{ Dashboard: string; }' is not assignable to type 'ComponentClass | FunctionComponent | ComponentClass, any> | FunctionComponent<...> | undefined'.

Type '{ Dashboard: string; }' is not assignable to type 'FunctionComponent>'. Type '{ Dashboard: string; }' provides no match for the signature '(props: PropsWithChildren>, context?: any): ReactElement<...> | null'.

Overload 2 of 2, '(props: RouteProps, context?: any): Route', gave the following error. Type '{ Dashboard: string; }' is not assignable to type 'ComponentClass |FunctionComponent | ComponentClass, any> | FunctionComponent<...> | undefined'.

Type '{ Dashboard: string; }' is not assignable to type 'FunctionComponent>'. TS2769

16 |       <div>
17 |         <Switch>

18 | | ^ "

2
  • Did you import React from ‘react’ in both files? Commented Apr 11, 2020 at 10:26
  • Yes I imported react in both files Commented Apr 11, 2020 at 10:46

1 Answer 1

1

It seems that you haven't given your components a type (dashboard and login).

You need to do something similar to the below component declaration image, where by you set the type of your component as React.FC after importing the React library. (The FC standing for Functional Component, since we are not using a class-based component model)

Component Image

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.