npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

damidev-mobx-router

v1.4.6

Published

MobX powered router for React apps

Downloads

24

Readme

MobX Router

MobX-powered router for React apps.

Getting Started

Install via npm

npm install damidev-mobx-router

Then create all routes you need, like this:

import { Route } from 'damidev-mobx-router';

const views = {
    default: new Route({
        path: '/',
        component: (<Homepage />)
    }),
}

You can also use nested routes for layouting your contents.

import { Route } from 'damidev-mobx-router';

const views = {
    home: new Route({
        path: '/',
        component: (<Layout />),

        subroutes: {
            page: new Route({
                path: '/',
                component: (<Homepage />)
            }),

            next: new Route({
                path: '/about',
                component: (<About />)
            })
        }
    })
}

When you need to work params or stores, just define component as a function.

import { Route } from 'damidev-mobx-router';

const views = {
    profile: new Route({
        path: '/profile/:id',
        // first argument is route props, it's mix of route params and queryString params
        // second argument is rootStore reference, see startRouter bellow
        component: ({ id }, { userStore }) => {
            return (<Profile user={userStore.getProfile(id)} />);
        }
    })
}

You can control routes with beforeEnter events. property beforeEnter accepts function or array of functions, each function gets same argunent as prop component. Event beforeEnter can return Promise, ObservableFromPromise (@see mobx-utils fromPromise) or nothing.

When beforeEnter callback returns Promise or ObservableFromPromise object, route waits until Promise is finished, then steps to next beforeEnter or continues to component. You can deny route access when returned Promise is rejected.

import { Route } from 'damidev-mobx-router';

const views = {
    default: new Route({
        path: '/',
        component: (params, { dataStore }) => (<Homepage data={dataStore.getData()} />),
        beforeEnter: [
            // wait until data are fetched from server
            (params, { dataStore }) => {
                return fetch('/api/data.json')
                    .then((response) => dataStore.setData(response.data));
            },

            // or use mobx-utils fromPromise function
            (params) => {
                const promise = fetch('/api/data.json');
                return fromPromise(promise);
            },

            // allow only logged user to acces
            (params, { userStore }) => {
                return userStore.isLogged || Promise.reject();
            }
        ]
    }),
}

When you finish with routes configuration you need to start mobx router.

import { startRouter } from 'damidev-mobx-router';

const rootStore = {
    // define other stores here
};

startRouter(views, rootStore, options);

Now app is listening to url changes. Next you need to provide RouterStore instance to your app and define default slot for rendering your routes.

import { MobxRouter } from 'damidev-mobx-router';

class App extends React.Component {
    render() {
        return (
            <Provider routeStore={rootStore.routeStore}>
                <MobxRouter slot="default" />
            </Provider>
        );
    }
}

Use multiple slots

When you want to use multiple slots, just configure router like this:

const options = {
    currentView: {
        default: null,
        content: null,
    }
};

const views = {
    home: new Route({
        path: '/',
        component: (<Layout><MobxRouter slot="content" /></Layout>),

        subroutes: {
            page: new Route({
                slot: 'content',
                path: '/',
                component: (<Homepage />)
            }),

            next: new Route({
                slot: 'content',
                path: '/about',
                component: (<About />)
            })
        }
    })
};

startRouter(views, rootStore, options);

class App extends React.Component {
    render() {
        return (
            <Provider routeStore={rootStore.routeStore}>
                <MobxRouter slot="default" />
            </Provider>
        );
    }
}

How to use links?

Here are all custom props for Link component. Prop to needs to mach route name as defined in views. Nested route names are merged with dot.

import { Link } from 'damidev-mobx-router';

<Link to="home.page" params={{foo: 'bar'}} queryParams={{page: 1}} activeClassName="active">Link to Homepage component</Link>