
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
rct-router
Advanced tools
NOTE: This won't work with browsers that don't support the HTML5 History API
/** 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')
)
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
})
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 viewCall 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
FAQs
a router for react
The npm package rct-router receives a total of 10 weekly downloads. As such, rct-router popularity was classified as not popular.
We found that rct-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.