Skip to content

Simple browser router based on history package

Notifications You must be signed in to change notification settings

Azein/router420

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

router420

Basic browser router based on history package

Purpose of this yet another clientside SPA router

I built this with a goal of having very simple browser router for my React.js projects. The code is very down-to-earth, yet capable to do it's job - routing, redirecting, working with protected routes.

How to use

Install the package with npm:

    $ npm install --save router420

Then integrate it into your project with bundle like webpack, and start writing the code:

import { Router, Route, browserHistory} from 'router420';
//...your other imports

<Router styles={styles.container} history={browserHistory}>
  <Route exactly pattern='/' component={AuthFormContainer} />
  <Route exactly pattern ='/customer' component={CustomerRoute}  />
  <Route exactly pattern='/customer/orderCreation' component={OrderCreationRoute} />
  <Route exactly pattern='/customer/orderCreation/orderCreationForm' component={OrderCreationFormRoute} />
</Router>

You can easily write your own protected routes using Redirect. Just remember that protected route's component needs to access the credentials. In this example I pass them from Redux store, yet Redux isn't a requirement for this router.

import { Redirect } from 'router420';
// ...other imports

class CustomerRoute extends React.Component {

  checkCredentials = () => {
    if (this.props.isLogged && this.props.role === 'customer'){
      return(
       <CustomerMain />
      )
    } else {
      return <Redirect to={'/'} />
    }
  }

  render(){
    return(
      this.checkCredentials()
    )
  }
}

For programmatical navigation there some helper functions for history api:

import { locationPush, locationReplace } from 'router420';
//...
locationPush('/some/cool/route')

Important issue

Beware that for now React forbids to return array of elements without wrapping tags, so this router will produce one container div which you can style through passing prop "styles". This isn't cool and I will fix that issue.

About

Simple browser router based on history package

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published