nareshbhatia / mobx-state-router

MobX-powered router for React apps
https://nareshbhatia.github.io/mobx-state-router/
MIT License
227 stars 30 forks source link

BREAKING CHANGE: React-router like route configuration #54

Closed chaaya closed 6 years ago

chaaya commented 6 years ago

Instead of passing a viewMap, this change makes it more like react-router where you pass the component to render as property in the route configuration.

import HomeComponent from './src/features/Home';
import DepartmentComponent from './src/features/Department';

export const routes = [
    {
        name: 'home',
        pattern: '/',
        component: HomeComponent
    },
    {
        name: 'department',
        pattern: '/departments/:id',
        component: DepartmentComponent 
    },
    {
        name: 'notFound',
        pattern: '/not-found',
        component: <h1>404 Page not found</h1>
    }
];

The change includes a new computed property in the routerStore, activeView, which relies on the internal viewMap thats being created in the constructor that maps the component with the route name.

coveralls commented 6 years ago

Coverage Status

Coverage decreased (-0.3%) to 98.48% when pulling 9c33f33050c55b6d2be00cc1a2ff472665ce3426 on chaaya:feature/remove-need-of-passing-viewmap into 86baa945c889de24fc5fffe8749282db938ce032 on nareshbhatia:master.

nareshbhatia commented 6 years ago

@chaaya, as far as I can tell, this change would make the the router dependent on React. The basic design principle we have been following is to completely decouple the router from the presentation layer. It is a simple state-machine that should be able to run without a front-end. Please let me know your thoughts. If you agree, please close this PR.

chaaya commented 6 years ago

@nareshbhatia I kind of agree with you, yes you could still use the core without react and write your own view layer that observers state changes and renders the correct view. The reason I kind of agree with you is that the Github info says:

image

My guess is that 98% people using this is using it in conjunction with React.

One idea is to make two repos, mobx-state-router-core and mobx-state-router-react in that case so that people can choose if they just want the core and write their own render layer or use the out-of-the-box solution with React. What do you think of that?

nareshbhatia commented 6 years ago

@chaaya, while I agree that mobx-state-router is mostly used with React, I still think we should not mix presentation and state in the router itself. You might want to read Michel's article How to decouple state and UI to reinforce this idea. If you still like to continue with your approach, please feel free to maintain a fork.

I am going to close this PR, but be assured that I am always open to new ideas, so please keep them coming.