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

mobx-react-router-view

v1.1.4

Published

React routing like Vue router-view sync with your MobX state

Downloads

17

Readme

mobx-react-router-view

React routing like Vue router-view sync with your MobX state

Installation

npm install --save mobx-react-router-view

Typescript

If you are using typescript - the built in typings for this project depend on @types/history, so make sure you have them installed too.

const store = new RouterStore();

Usage

routes.js

export const beforeEach = async (...[from, to, store]) => {
    return await new Promise((res) => {res()});
}

export const notFoundRoute = {
    name: '404',
    path: '/404',
    component: NoMatchComponent
}

export const initialRoute = {
    name: 'initial',
    path: '/',
    component: LoadingComponent,
    beforeExit: () => {return new Promise((res) => {setTimeout(res, 1000)})}
}

export const routes = [
    {
        name: 'home',
        path: '/',
        component: MainComponent,
        children: [
            {
                name: 'test',
                path: '/test',
                component: TestComponent
            },
        ]
    },
    {
        name: 'info',
        path: '/info',
        component: InfoComponent,
        children: [
            {
                name: 'infoItem',
                path: '/:id',
                component: InfoIdComponent,
                children: [
                    {
                        name: "infoDetails",
                        path: '/details',
                        component: InfoDetailsComponent
                    }
                ],
            },
            {
                name: 'infoList',
                path: '/list',
                component: InfoListComponent,
            },
        ]
    }
]

index.js


import RouterStore, {syncHistoryWithStore} from "mobx-react-router-view";
import {routes, notFoundRoute, initialRoute, beforeEach} from "./routes"

const routingStore = new RouterStore(routes, {
     notFoundRoute: notFoundRoute,
     initialRoute: initialRoute,
     beforeEach: beforeEach
 });
const stores = {
  // Key can be whatever you want
  routing: routingStore,
  // ...other stores
};

const browserHistory = createBrowserHistory();
const history = syncHistoryWithStore(browserHistory, store);

ReactDOM.render(
  <Provider {...stores}>
    <Router history={history}>
      <App />
    </Router>
  </Provider>,
  document.getElementById('root')
);

App.js

import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import { RouterView } from 'mobx-react-router-view';

@inject('routing')
@observer
export default class App extends Component {
  render() {
    const { actualRoute, push, goBack, goTo } = this.props.routing;

    return (
      <div>
        <span>Current pathname: {actualRoute.location.pathname}</span>
        <button onClick={() => goTo('home')}>Main</button>
        <button onClick={() => push('/test')}>Change url</button>
        <button onClick={() => goTo('test', {test: 1})}>With state</button>
        <button onClick={() => goBack()}>Go Back</button>
        <RouterView store={this.props.routing}/>
      </div>
    );
  }
}

mainComponent.js

import React from 'react';
import { RouterView, WithRoute } from '../route';

const MainComponent = (props) => { //props: WithRoute<any>
    return (
        <div>
            is main component
            <RouterView route={props.route} store={props.store}/>
        </div>
    )
}

export default MainComponent

Features

  • add props matching for the goTo function