npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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 completes
  • inject 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