react-router-object
Declarative object based routing for React Router
Install
# yarn $ yarn add react-router-object# npm $ npm install react-router-object
Usage
This is a basic routes configuration
import React from 'react'import BrowserRouter as Router Route Switch from 'react-router-dom'import applyPathToRoutesObj mapRoutesObjToArray from 'react-router-object'import Page from './pages/Page' const routesConfig = // index is a reserved key for root paths index: <Page ="Index" /> key: 'index' exact: true blog: index: <Page ="Blog" /> key: 'blog' exact: true author: <Page ="Blog > Author" /> key: 'blog-author' // add the path based on the position within the objects keys treeconst routesObj = // convert the routes object into an array and that's it!const routesArr = ... const Routes = <Router> <Switch> routesArr </Switch> </Router>
Provider
Using RoutesObjProvider
you can easily access all, and current routes object.
routes
prop containing:
Passes the Property | Type | Description |
---|---|---|
all |
object |
All routes |
current |
object |
The current route |
In your routes setup:
Important - RoutesObjProvider
uses withRouter()
, so make sure you place the provider inside the <Router>
import RoutesObjProvider from 'react-router-object' const routesObj = <Router> <RoutesObjProvider => <Switch> routesArr </Switch> </RoutesObjProvider></Router>
Wrap the consuming component with withRoutesObj
and that's all!
import withRoutesObj from 'react-router-object' const Page = { console return <h1>propstitle</h1>} Page
License
MIT © antoniojps