All Projects → miles-till → mobx-state-tree-router

miles-till / mobx-state-tree-router

Licence: MIT license
State-based router for React and MobX State Tree

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mobx-state-tree-router

react-mobx-router
Create React App with React Router 4 and MobX + Internationalization
Stars: ✭ 90 (+400%)
Mutual labels:  mobx, mobx-state-tree
mst-persist
Persist and hydrate MobX-state-tree stores (in < 100 LoC)
Stars: ✭ 75 (+316.67%)
Mutual labels:  mobx, mobx-state-tree
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (+5.56%)
Mutual labels:  mobx, mobx-state-tree
umi-plugin-mobx
😍 use mobx-state-tree gracefully in umijs.
Stars: ✭ 33 (+83.33%)
Mutual labels:  mobx, mobx-state-tree
Mobx State Tree
Full-featured reactive state management without the boilerplate
Stars: ✭ 6,317 (+34994.44%)
Mutual labels:  mobx, mobx-state-tree
Mobx Logger
Log Mobx Actions, Reactions, Transactions and Computations
Stars: ✭ 210 (+1066.67%)
Mutual labels:  mobx
React Book
《React进阶之路》示例代码
Stars: ✭ 249 (+1283.33%)
Mutual labels:  mobx
Saas
Build your own SaaS business with SaaS boilerplate. Productive stack: React, Material-UI, Next, MobX, WebSockets, Express, Node, Mongoose, MongoDB. Written with TypeScript.
Stars: ✭ 2,720 (+15011.11%)
Mutual labels:  mobx
Mobx Share
🔑一个分享mobx的在线演示ppt
Stars: ✭ 196 (+988.89%)
Mutual labels:  mobx
mobx-react-form-devtools
DevTools for MobX React Form
Stars: ✭ 30 (+66.67%)
Mutual labels:  mobx
mobx-crud-example
A crud mobx project using react, featherjs and mongodb
Stars: ✭ 22 (+22.22%)
Mutual labels:  mobx
React Cnodejs.org
Material UI version of cnodejs.org, the biggest Node.js Chinese community.
Stars: ✭ 242 (+1244.44%)
Mutual labels:  mobx
Mobx Task
Makes async function state management in MobX fun.
Stars: ✭ 218 (+1111.11%)
Mutual labels:  mobx
Wechat Weapp Mobx
微信小程序(wechat weapp) mobx 绑定, 跨页面通信的利器, 现已发布npm包
Stars: ✭ 208 (+1055.56%)
Mutual labels:  mobx
taro-ts-mobx-boilerplate
Taro 脚手架 Typescript/ Mobx / icon font / Jest
Stars: ✭ 12 (-33.33%)
Mutual labels:  mobx
Wiretap
🔍 A desktop app for inspecting mobx and mobx state tree apps
Stars: ✭ 198 (+1000%)
Mutual labels:  mobx
Delorean
A MobX-React Time Travel Debugger
Stars: ✭ 238 (+1222.22%)
Mutual labels:  mobx
pub-rules
Rules - Powerful and feature-rich validation library for both Dart and Flutter.
Stars: ✭ 24 (+33.33%)
Mutual labels:  mobx
Heard
React Native Enterprise Social Messaging App
Stars: ✭ 234 (+1200%)
Mutual labels:  mobx
Embed
Pixel-perfect Discord chat widgets for your website 💬
Stars: ✭ 229 (+1172.22%)
Mutual labels:  mobx

logo

MobX State Tree Router

State-based router for React and MobX State Tree

npm

Inspiration

Installation

Peer dependencies: react react-dom mobx mobx-react mobx-state-tree

NPM: npm install --save mobx-state-tree-router

Yarn: yarn add mobx-state-tree-router

Quick Start

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { RouterStore, View, startRouter, StateRouter, Link } from 'mobx-state-tree-router';

const views = {
  home: View.create({
    name: 'home',
    path: '/',
    component: <div>Home page</div>
  }),
  about: View.create({
    name: 'about',
    path: '/about',
    component: <div>About page</div>
  })
};

const router = RouterStore.create({
  views: views
});

startRouter(router);

ReactDOM.render((
  <div>
    <Link router={router} view={views.home}>Home</Link>
    <Link router={router} view={views.about}>About</Link>
    <StateRouter router={router} />
  </div>
), document.getElementById('root'));

Features

React components

  • <StateRouter router loading />
    • renders the appropriate component for the router's currentView
    • currentView can be set by calling router.setView(view, params) directly, or using a Link component
    • props can be set for the rendered component by calling router.setProps(props)
    • a component can be passed into the loading prop to display a loading indicator (e.g. ajax spinner)
  • <Link router view></Link>
    • renders an <a href></a> element with href set to the result of view.formatUrl(params)

MobX State Tree models

  • RouterStore
    • exposes the available routes based on the views
    • manages the currentUrl based on the currentView and params
    • setView(view, params) can be called to change the route
    • setProps(props) can be called to pass props to the currentView's rendered component
  • View
    • defines a name, route, component to render
    • defines optional change route hooks:
      • beforeExit(self, params)
      • beforeEnter(self, params)
      • onExit(self, params)
      • onEnter(self, params)
    • formatUrl(params) can be called to get the url for this view given the params passed

Browser url matching and history binding

startRouter(router) binds the router to the browser's history object to update the url or parse a url change and set the appropriate view.

Centralized view definitions

Views and their route cycle hooks (data fetching and business logic) can be defined in one centralized location:

views.js

import { View } from 'mobx-state-tree-router';
import Home from 'components/Home';
import ItemList from 'components/ItemList';
import Item from 'components/Item';
export const views = {
  home: View.create({
    name: 'home',
    path: '/',
    component: <Home />
  }),
  items: View.create({
    name: 'items',
    path: '/items',
    component: <ItemList />
    hooks: {
      async beforeEnter(self) {
        await self.root.itemStore.loadItems();
      }
    }
  }),
  item: View.create({
    name: 'item',
    path: '/item/:itemId',
    component: <Item />
    hooks: {
      async beforeEnter(self, params) {
        self.router.setProps({ itemId: params.itemId });
        await self.root.itemStore.loadItem(params.itemId);
      }
    }
  })
};

Route cycle hooks

These hooks provide a way to run some code during a route change. They can be synchronous or asynchronous. The route change process is as follows:

  • router.setView called
    • router.isLoading set to true
  • old view beforeExit(self, params)
    • returning false will cancel the route change
  • new view beforeEnter(self, params)
    • returning false will cancel the route change
  • route is updated and rendered
    • router.isLoading set to false
  • old view onExit(self, params)
  • new view onEnter(self, params)

Known Issues

  • formatUrl doesn't properly handle paths with catch-all *
    • current workaround is to use setView in the beforeEnter hook of the catch-all to redirect to another view
  • query parameters are not handled
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].