• Stars
    star
    115
  • Rank 296,413 (Top 6 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React router for your web, electron or nw.js app.

React Universal Router

Finally a simple and universal router for React

Motivation

At Avocode we needed a simple universal router that can be used on the web as well as in an Electron app. We tried React Router since it's de-facto a standard, but sadly it didn't fit our needs. We didn't like the robustness, frequently changing API and the fact that some simple tasks were difficult for unknown reasons (e.g. getting a route name). Nevertheless, there were some nice things like the History that perfectly manages browser history and can be used on a web, Electron or React Native thanks to MemoryHistory. So eventually we've built React Universal Router, an abstraction above the History library with some added sugar. Keep in mind the API interface is not finalized yet and will probably change until it reaches the 1.0 version.

Quick Tour

1. Definition of routes

Key/Value definition of routes.

module.exports = {
  '/': 'homepage',
  '/designs': 'designs',
  '/projects': 'projects',
  '/project/:projectId': 'project-detail',
  '404': 'notFound',
}

2. Definition of targets

Targets are the "glue" between the routes and your components.

module.exports = {
  component: MyAppComponent,
  states: {
    'homepage': {
      myCustomProp: 'Homepage section'
    },
    'designs': {
      myCustomProp: 'Designs section'
    },
    'projects': {
      myCustomProp: 'Projects section'
    },
    'project-detail': {
      myCustomProp: 'Project Detail section'
    },
  }
}

States are matching the names (values) defined in route definition(s). You can optionally add props that are going to be injected to defined component. In this case MyAppComponent will receive myCustomProp prop.

3. Actual usage

You need to create a new instance of Router, add route definition(s) and target definition(s).

const options = {}
const router = new Router(options)
router.addRoutes(routes)
router.addTarget(routeTarget)

Constructor optional options with default values:

  • avoidTransitionSameRoute
    • true (default)
    • false
  • slash:
    • 'enforce' - it always adds / at the end of the url
    • 'omit' - it always remove / at the end of the url
    • null (default)
  • history:
    • 'memory' - is used as a reference implementation and may also be used i non-DOM environments, like React Native or Electron
    • 'hash' - is for use in legacy web browsers
    • 'push' - is for use in modern web browsers that support the HTML5 history API
    • object - a custom history object instance can be passed in. The instance must implement the API specified by the history package. A default route must not be specified (see below).
  • defaultRoute:
    • null - the initial history state is untouched
    • '/my-route' - will be default

To start listening on route changes you need to add listen listener. You can use it in your root component like this:

const App = React.createClass({
  getInitialState() {
    return { activeComponent: router.getCurrentComponent() };
  },

  componentDidMount() {
    router.listen(() => {
      this.setState({
        activeComponent: router.getCurrentComponent()
      });
    });
  },
  render() {
    return div(null, this.state.activeComponent);
  }
});

Or in a similar fashion as the React router:

router.listen(component => React.render(component, mountElement));

Changing the route

You can change the route by calling the transitionTo method and a name of the route defined in route definition(s).

// Simple transition to a different page
const { transitionTo } = router.getRouterProps();
transitionTo('designs');

// Transition to a different page with params
transitionTo('project-detail', {
  params: {
    projectId: 42
  }
});

TransitionTo optional options:

  • methodType:
  • 'pushState' (default)
  • 'replaceState'
  • params, state, query - Read more about it in the History documentation

More Repositories

1

combokeys

Web browser keyboard shortcuts. CommonJS, NPM.
JavaScript
673
star
2

react-shortcuts

Manage keyboard shortcuts from one place
JavaScript
326
star
3

avocode

Get CSS, SVG, image assets, fonts, colors. All without Photoshop or Sketch.
182
star
4

react-droparea

Drag and Drop library for React
CoffeeScript
93
star
5

react-tabguard

Limit tabbing within specified area with an ease
CoffeeScript
63
star
6

electron-windows-autoupdate

Get Electron AutoUpdater working on Windows
HTML
56
star
7

react-resizer

A React component for resizing HTML elements.
CoffeeScript
50
star
8

psd-to-sketch-converter

Convert PSD to Sketch for free
44
star
9

json-immutable

Immutable.JS structure-aware JSON serializer/deserializer
JavaScript
23
star
10

react-listview

Create List components with keyboard support
CoffeeScript
13
star
11

avocode-atom-integration

Integrate Avocode web app with Atom to inspect Photoshop & Sketch designs inside your text editor and to get code suggestions from layer styles.
JavaScript
9
star
12

pretty-selectors

Pretty selectors take your ugly text and transform it to nice CSS selectors
CoffeeScript
7
star
13

console-plus

New levels + colors to Console. Works in both the browser and node.js.
CoffeeScript
6
star
14

dragster

Better HTML5 drag events
CoffeeScript
5
star
15

react-focusguard

JavaScript
4
star
16

e2e-stories

E2E testing with Jest and Puppeteer flavored with simplified Yaml syntax.
TypeScript
4
star
17

avocode-email-tagsinput

React input component for handling email input as a tag.
JavaScript
3
star
18

react-listener-component

Safe listener registration for React context-provided services.
CoffeeScript
3
star
19

reactmemory

ReactMemory.render(<App />, ReactMemory.createRoot())
JavaScript
2
star
20

codeinthedark

Code in the Dark
HTML
1
star