dt1000
dt1000

Reputation: 3732

React show routes if set up using react-router

Is there a command on the console I can execute at run-time that will tell me all the routes? I have used react-router, but the routes don't all work. In rails you can get a list at runtime.

Upvotes: 4

Views: 422

Answers (1)

Md.Estiak Ahmmed
Md.Estiak Ahmmed

Reputation: 1593

you can get the routes in an array by using below library

https://github.com/alansouzati/react-router-to-array

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import reactRouterToArray from 'react-router-to-array';
// or var reactRouterToArray = require('react-router-to-array');

console.log(reactRouterToArray(
<Route path="/" component={FakeComponent}>
{/* just to test comments */}
<IndexRoute component={FakeComponent} />
<Route path="about" component={FakeComponent}>
  <Route path="home" component={FakeComponent} />
  <Route path="/home/:userId" component={FakeComponent} />
</Route>
<Route path="users" component={FakeComponent} />
<Route path="*" component={FakeComponent} />
</Route>)
); //outputs: ['/', '/about', '/about/home', '/users']

Upvotes: 1

Related Questions