4.2.1 • Published 1 month ago

rct-router v4.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
1 month ago

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

4.2.1

1 month ago

4.2.0

1 month ago

4.1.1

3 months ago

4.1.0

3 months ago

4.0.1

1 year ago

4.0.0

2 years ago

3.0.0

2 years ago

2.0.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.8.9

4 years ago

0.8.8

4 years ago

0.8.7

4 years ago

0.8.6

4 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago