rct-router
v4.2.1
Published
a router for react
Downloads
31
Readme
rct-router
Routing in React with the HTML5 History API
NOTE: This won't work with browsers that don't support the HTML5 History API
An example
/** routes.js */
import { Collection, createGo, RootCollection, RctRoute } from 'rct-router'
const router = new RootCollection({
error: Views.Error,
notFound: Views.NotFound,
path: '/',
template: Templates.Root,
}).addRoute(new RctRoute({
name: 'home',
path: '/',
component: Views.Login,
})).addRoute(new RctRoute({
name: 'login',
path: '/login',
component: Views.Login,
})).addCollection(
new Collection({
name: 'dashboard',
path: '/dashboard',
template: Templates.Dashboard,
}).addRoute(new RctRoute({
name: 'home',
path: '/',
component: Views.Dashboard.Home,
beforeRender: authenticateAnd(['admin']),
inject: {
...injectBreadCrumbs([
{ name: 'home', urn: route.dashboard },
])
}
})).addCollection(
new Collection({
name: 'profile',
path: '/profile',
}).addRoute(new RctRoute({
name: 'home',
path: '/',
component: Views.Dashboard.Profile.Home,
beforeRender: authenticateAnd(['admin']),
inject: {
...injectBreadCrumbs([
{ name: 'home', urn: route.dashboard },
{ name: 'profile', urn: route.profile },
])
}
})))).build()
export const routes = router
export const go = createGo<Route>(router)
/** app.js */
import { Router } from 'rct-router'
import { routes } from './routes'
ReactDOM.render(
<Router routes={routes} />,
document.getElementById('root')
)
How to use
Start with RootCollection, which takes the parameters "template", "notFound", and "path"... notFound is the optional component to render when a route isn't found error is an optional component which should follow the react 16 error handler pattern. there is a default ugly one provided in this package.
new RootCollection({
path?: '/' or whatever you want the root to be,
template?: Component with props.children,
notFound?: Component,
error?: Component
})
use addCollection
to add a collection of routes with the params. this method also exists on collections
new Collection({
name: string,
path: string,
template?: Component with props.children,
})
the addRoute
method on both RootCollection and Collection takes the parameters
new RctRoute({
name: string
path: string
component: React.ComponentType<any>
template?: React.ComponentType<any>
beforeRender?: Promise or function (it uses async/await)
inject?: any object
})
- Routes and templates inherit their parent templates
beforeRender
is for middleware, authentication and things of the sort can be done there. The view won't render until the function completesinject
lets you inject props into the view
Call the method build
on the RootCollection
when you're done adding routes to create the end routes class.
use the helper createGo
to create a function (I call it 'go') for routing. The arguments for 'go' are:
go(
pointer, // names of parent collections and name of the route joined with periods,
params, // an object of params needed for the route.
event?, // optionally pass in event from click events or whatever, and it will call preventDefault for you
)
// How I use it
/** routes.ts */
export enum Route {
Home = 'home',
Login = 'login',
Dashboard = 'dashboard.home',
Profile = 'dashboard.profile.home',
}
export const go = createGo(router)
/** dashboard/home.tsx */
import { Route, go } from '../../routes'
class Dashboard extends Component<Props, State> {
/** ... */
onClick = (e) => {
go(Route.Profile, { personId: this.props.person.id }, e)
}
/** ... */
}
If you have any questions, feel free to reach out to me <3