Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How to pass Components into a react-router Route using Typescript?

When I pass in some Components to a Route Component I get a TS compiler error:

Type '{ main: typeof MainComponent; sidebar: typeof SidebarComponent; }' is not assignable to type 'RouteComponents'. Property 'sidebar' is incompatible with index signature. Type 'typeof SidebarComponent' is not assignable to type 'ReactType'. Type 'typeof SidebarComponent' is not assignable to type 'StatelessComponent'. Type 'typeof SidebarComponent' provides no match for the signature '(props: any, context?: any): ReactElement'

I've tried to correctly construct a RouteComponents object, but I had to modify the react-router's index.d.ts to export the RouteComponents definition as only RouteComponent is exported, and it still didn't like the expression. Where can I find out what a RouteComponents object supposed to look like?

Here is the simple file routes.tsx:

import * as React from 'react'
import { IndexRoute, Route } from 'react-router'

import App from './components/App'
import MainComponent from './components/MainComponent'
import OtherMainComponent from './components/OtherMainComponent'
import SidebarComponent from './components/SidebarComponent'

const routes = () => {
  return (
    <Route path="/" component={App}>
      <IndexRoute components={{ main: MainComponent, sidebar: SidebarComponent }} />
      <Route path="/add" components={{ main: OtherMainComponent, sidebar: SidebarComponent }} />
    </Route>
  )
}

export default routes

As I mentioned, I tried it the following way too:

const indexRouteComponents: RouteComponents = {
  main: MainComponent,
  sidebar: SidebarComponent 
}

const routes = () => {
  return (
    <Route path="/" component={App}>
      <IndexRoute components={indexRouteComponents} />
      <Route path="/add" components={{ main: OtherMainComponent, sidebar: SidebarComponent }} />
    </Route>
  )
}

The components={indexRouteComponents} part does not have an error, but creating the indexRouteComponents object is an error (The same error as above, because it's not a correct RouteComponents object)

What is the correct way to set up the react-router Components using Typescript?

Edit: here are Github issues related to this: https://github.com/ReactTraining/react-router/issues/4317 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/13689

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

const AppRouter = [
{
    main: MainComponent,
    exact: true,
    path: "/",
    sidebar: SidebarComponent,
},
{
    main: OtherMainComponent,
    exact: true,
    path: "/add",
    sidebar: SidebarComponent,
}

];

And in your main component:

public render() {
    return (
        <Router>
           <div className={"sidebar"}> 
               {AppRouter.map((route, index) => (
                <Route
                    key={index}
                    path={route.path}
                    exact={route.exact}
                    component={route.sidebar}
                />
            ))}
            </div>
            <div className={"content"}>
                {AppRouter.map((route, index) => (
                <Route
                    key={index}
                    path={route.path}
                    exact={route.exact}
                    component={route.main}
                />
            ))}
            </div>
        </Router>
    )
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!