Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typing React Router Router Component? Confused

I have not used React Router v4, so I could be making some novice errors. My issue is that Typescript compilation is breaking on an error that I've seen where I guess there is potentially a class that is being returned under the hood that I am not seeing?

Code ::

import * as React from 'react';
import { Router, RouteComponentProps } from 'react-router-dom';
import HomeRoute from './home';
import PhotosRoute from './photos';
import StoreRoute from './store';
import TourRoute from './tour';
import 'index.css';

interface RouterProps {
  component?: React.SFC<RouteComponentProps<any> | void> | React.ComponentClass<RouteComponentProps<any> | void>;
}

class Navigation extends React.Component<RouterProps, {}> {
  render(): JSX.Element {
    return (
       <Router>
        <div>
          <ul className="nav-styles">
           <li>
               <HomeRoute />
            </li>
            <li>
               <PhotosRoute />
            </li>
            <li>
               <StoreRoute />
            </li>
            <li>
                <TourRoute />
            </li>
          </ul>
        </div>
      </Router>
    );
  }
}

export default Navigation;

error ::

(16,8): error TS2322: Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Router> & Readonly<{ children?: ReactNode; }> & Re...'.
  Type '{ children: Element; }' is not assignable to type 'Readonly<RouterProps>'.
    Property 'history' is missing in type '{ children: Element; }'.

In case I am doing something wrong or someone wishes to see it, here is an example of HomeRoute. However I don't see this being the breaking issue at this time.

import * as React from 'react';
import { Route, Link } from 'react-router-dom';
import Home from '../Home/Home';

export default (): JSX.Element => (
  <div>
    <nav>
      <Link to="/">Home</Link>
    </nav>
    <div>
      <Route path="/" component={ Home } />
    </div>
  </div>
);

I know I need to type the class somehow, and I have been trying and can't figure out a good reference or how to type the Navigation class.

like image 432
cheussy Avatar asked Oct 29 '17 01:10

cheussy


People also ask

Why withRouter is not working?

The error "export 'withRouter' (imported as 'withRouter') was not found in 'react-router-dom'" occurs because the withRouter function has been removed in react router v6. To solve the error, install version 5.2. 0 of react router by running npm install [email protected] .

What are the router components of react router?

There are three primary categories of components in React Router: routers, like <BrowserRouter> and <HashRouter> route matchers, like <Route> and <Switch> and navigation, like <Link> , <NavLink> , and <Redirect>

Why use react-router-DOM instead of react router?

If you are working on a web application, the better option is to use react-router-dom, because it contains all the necessary common components and features essential for routing in a web application.


1 Answers

I made the same 'novice' error when following along the react-router tutorial:

import { Router } from 'react-router-dom';

...should instead be:

import { BrowserRouter as Router } from 'react-router-dom';
like image 90
Daniel Loiterton Avatar answered Sep 18 '22 23:09

Daniel Loiterton