All Projects → kitze → Mobx Router

kitze / Mobx Router

A simple router for MobX + React apps

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Mobx Router

Gear
A lightweight, composable and high performance web service framework for Go.
Stars: ✭ 544 (+11.25%)
Mutual labels:  hooks, router
React Hooks Mobx State Tree
React Hooks + MobX State Tree + TypeScript = 💛
Stars: ✭ 169 (-65.44%)
Mutual labels:  hooks, mobx
Hookrouter
The flexible, and fast router for react that is entirely based on hooks
Stars: ✭ 1,200 (+145.4%)
Mutual labels:  hooks, router
Redux First History
🎉 Redux First History - Redux history binding support react-router - @reach/router - wouter
Stars: ✭ 163 (-66.67%)
Mutual labels:  router, history
ultra-router
Router for component-based web apps. Pair with React or <BYOF />.
Stars: ✭ 35 (-92.84%)
Mutual labels:  router, history
Navigo
A simple vanilla JavaScript router.
Stars: ✭ 2,435 (+397.96%)
Mutual labels:  router, history
React Atom
A simple way manage state in React, inspired by Clojure(Script) and reagent.cljs
Stars: ✭ 133 (-72.8%)
Mutual labels:  hooks, mobx
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (-78.12%)
Mutual labels:  hooks, mobx
boring-router
A type-safe MobX router with parallel routing support.
Stars: ✭ 74 (-84.87%)
Mutual labels:  router, mobx
hooksy
Simple app state management based on react hooks
Stars: ✭ 58 (-88.14%)
Mutual labels:  hooks, mobx
Redux Saga Router
A router for Redux Saga
Stars: ✭ 153 (-68.71%)
Mutual labels:  router, history
micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (-84.05%)
Mutual labels:  hooks, mobx
Route
原生 js 实现的轻量级路由,且页面跳转间有缓存功能。
Stars: ✭ 68 (-86.09%)
Mutual labels:  router, history
Sheet Router
fast, modular client-side router
Stars: ✭ 219 (-55.21%)
Mutual labels:  router, history
Navaid
A navigation aid (aka, router) for the browser in 850 bytes~!
Stars: ✭ 648 (+32.52%)
Mutual labels:  router, history
Alldemo
🍑 2020全栈学习Demo大合集 包含最新 hooks TS 等 还有umi+dva,数据可视化等实战项目 (持续更新中)
Stars: ✭ 189 (-61.35%)
Mutual labels:  hooks, mobx
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-88.14%)
Mutual labels:  router, mobx
Frontexpress
An Express.js-Style router for the front-end
Stars: ✭ 263 (-46.22%)
Mutual labels:  router, history
React Recipes
👩‍🍳 A React Hooks utility library containing popular customized hooks
Stars: ✭ 452 (-7.57%)
Mutual labels:  hooks
React Use Gesture
👇Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
Stars: ✭ 5,704 (+1066.46%)
Mutual labels:  hooks

🙋‍♂️ Made by @thekitze

Other projects:

  • 🏫 React Academy - Interactive React and GraphQL workshops
  • 💌 Twizzy - A standalone app for Twitter DM
  • 💻 Sizzy - A tool for testing responsive design on multiple devices at once
  • 🤖 JSUI - A powerful UI toolkit for managing JavaScript apps

〽️ MobX Router

Example usage

Inspiration

📖 How to decouple state and UI - a.k.a. you don’t need componentWillMount

Features

  • Decoupled state from UI
  • Central route configuration
  • URL changes are triggering changes directly in the store, and vice-versa
  • No need to use component lifecycle methods like componentWillMount to fetch data or trigger a side effect in the store
  • Supported callbacks for the routes are: beforeEnter, onEnter, beforeExit, onExit. All of the callbacks receive route, params, store, and queryParams as parameters. If the beforeExit or beforeEnter methods return false the navigation action will be prevented.
  • The current URL params and query params are accessible directly in the store store.router.params / store.router.queryParams so basically they're available everywhere without any additional wrapping or HOC.
  • Navigating to another route happens by calling the goTo method on the router store, and the changes in the url are reflected automatically. So for example you can call router.goTo(routes.book, {id:5, page:3}) and after the change is made in the store, the URL change will follow. You never directly manipulate the URL or the history object.
  • <Link> component which also populates the href attribute and works with middle click or cmd/ctrl + click
  • Typescript support (Converted to typescript by thdk)
  • Hash-based routing (using paths like /#/foo/bar) support

Implementation

import React, {createContext} from 'react';
import ReactDOM from 'react-dom';

import {MobxRouter, RouterStore, startRouter} from 'mobx-router';
import routes from 'config/routes';

//example mobx store
export class AppStore {
    title = 'MobX Router Example App',
    user = null
}

export class RootStore {
    public router: RouterStore<RootStore>;
    public app: AppStore;

    constructor() {
        this.router = new RouterStore<RootStore>(this);
        this.app = new AppStore();
    }
}

const store = new RootStore();

// Use React context to make your store available in your application
const StoreContext = createContext({});
const StoreProvider = StoreContext.Provider;

startRouter(routes, store);

ReactDOM.render(
  <StoreProvider value={store}>
  	<MobxRouter store={store}/>
  </StoreProvider>, document.getElementById('root')
)

Example config

/config/routes.js

import React from 'react';

//models
import {Route} from 'mobx-router';

//components
import Home from 'components/Home';
import Document from 'components/Document';
import Gallery from 'components/Gallery';
import Book from 'components/Book';
import UserProfile from 'components/UserProfile';

const routes = {
  home: new Route({
    path: '/',
    component: <Home/>
  }),
  userProfile: new Route({
    path: '/profile/:username/:tab',
    component: <UserProfile/>,
    onEnter: () => {
      console.log('entering user profile!');
    },
    beforeExit: () => {
      console.log('exiting user profile!');
    },
    onParamsChange: (route, params, store) => {
      console.log('params changed to', params);
    }
  }),
  gallery: new Route({
    path: '/gallery',
    component: <Gallery/>,
    onEnter: (route, params, store, queryParams) => {
    	store.gallery.fetchImages();
    	console.log('current query params are -> ', queryParams);
    },
    beforeExit: () => {
      const result = confirm('Are you sure you want to leave the gallery?');
      return result;
    }
  }),
  document: new Route({
    path: '/document/:id',
    component: <Document/>,
    beforeEnter: (route, params, store) => {
      const userIsLoggedIn = store.app.user;
      if (!userIsLoggedIn) {
        alert('Only logged in users can enter this route!');
        return false;
      }
    },
    onEnter: (route, params) => {
      console.log(`entering document with params`, params);
    }
  }),
  book: new Route({
    path: '/book/:id/page/:page',
    component: <Book/>,
    onEnter: (route, params, store) => {
      console.log(`entering book with params`, params);
      store.app.setTitle(route.title);
    }
  })
};
export default routes;

Custom director configuration

mobx-router uses director behind the scenes. mobx-router exposes the director config object for you to pass your own configuration to director.

To do this you must pass a DirectorConfig object as third argument of startRouter method.

Hash based Routing | html5history

If you disable html5history option, mobx will fallback to hash based routing.

startRouter(
  routes, 
  store, 
  {
    // https://github.com/flatiron/director#configuration
    html5history: false,
  }
);

Not found (404) route | notfound

You can pass a function to notfound which will be called when you don't have any matching route for the current path.

startRouter(
  routes, 
  store, 
  {
    // https://github.com/flatiron/director#configuration
    notfound: () => store.router.goTo(YOUR_NOT_FOUND_ROUTE),
  }
);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].