1.0.5 • Published 5 years ago

@tarvit/micro_router v1.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

MicroRouter

Simple and minimalistic pure JS router for clientside

Usage example

import { MicroRouter } from "@tarvit/micro_router";

// define router
const MyRouter = new MicroRouter('state_view_router_state', MicroRouter.modes.windowHash);

// draw your routes
MyRouter.setDefaultRoute('notification/example');
MyRouter.addRoute('dashboard', StateDashboard); // StateDashboard is your ReactComponent.
MyRouter.addRoute('notification/:text', StateNotification); // StateNotification is another your ReactComponent.

// create nav helpers (optional)
MyRouter.nav = {
  toDashboard: () => {
    MyRouter.navigate({ view: StateDashboard});
  },
  toNotification: (message) => {
    MyRouter.navigate({ view: StateNotification, text: message})
  }
};


// create root component
import React, { Component } from 'react';

export default class AppRoot extends Component {
  constructor(props) {
    super(props);
    
    // enable automtical route resolving
    MyRouter.bindToWindowHash();

    // define default state
    this.state  = {
      viewState: MyRouter.currentState()
    };

    // subscribe to updates
    MyRouter.afterNavigate(()=>{
      this.setState({
        viewState: MyRouter.currentState()
      })
    });
  }
  
  // render corresponding component
  render() {
    const V = this.state.viewState.view;
 
    return (
      <div>
        { <V {...this.state.viewState} /> }
      </div>
    );
  }
}

To change router state use the folowing syntaxMyRouter.navigate({ view: StateNotification, text: message}) or MyRouter.navigatebyPath('notification/your-text-here')

Commands for development

install dependencies

$ yarn

test

$ yarn test

build

$ yarn build

release

$ npm publish

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago