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

@blendsdk/mobx-router

v0.0.11

Published

React Mobx router for @blendsdk

Downloads

4

Readme

Mobx React Router

This package provides a URL Router component based on Mobx and the History API.

The utilities and the component is this package are written in TypeScript and they are meant to be used in a React TypeScript project.

Installation

npm install @blendsdk/mobx-router --save
yarn add @blendsdk/mobx-router

Usage

import {
    createBrowserHistory,
    initRouter,
    IRoute,
    Link,
    Redirect,
    ROUTE_404,
    ROUTE_CATCH_ALL,
    Router,
    useRouter
} from "@blendsdk/mobx-router";

// Step 1: First we initialize the History provider
// For more information the the history package on npmjs
initRouter(createBrowserHistory());

// Step 2: Create some routes
//     Every rout needs to have:
//          - A name; that is used to identify the Route
//          - A path; that is used as a template to generate an URL
//          - A component; that is rendered in the incoming URL is matched to the path
//          - Optionally; a key/value pare providing default values for the path parameters
const routes: IRoute[] = [
    {
        name: "dashboard",
        path: "/",
        component: Dashboard
    },
    {
        name: "greet",
        path: "/greet/:name/name",
        component: Greet,
        // The default value of the :name parameter
        defaults: {
            name: "Johny Bravo"
        }
    },
    {
        // If no route is matched then this one is chosen as fallback
        name: ROUTE_CATCH_ALL,
        path: "",
        component: Dashboard
    }
];

// Step 3: Render the router with the routes parameter
const Main = () => {
    return <Router routes={routes}></Router>;
};

ReactDOM.render(<Main />, document.getElementById("root"));

404 Route

If you do not wish to configure a catch all route then you have the option to configure a 404 route to catch the unmatched location changes. The Router component provides a default 404 route if the incoming URL is not matched and there is also no catch all route.

To customize the built-in 404 route, just configure a route similar to:

....
    {
        name: ROUTE_404,
        path:"/not-found", // or something meaningful path name
        component: My404PageComponent // your custom 404 page
    }
....

The Link component

This component provides an easy way to render a link that is handled by the Router.

Properties:

  • to is the name of the route
  • params is a key/value pare to set the route parameters
  • reload an optional boolean that if set to true will reload the page when the link is clicked
<Link to='name-of-the-route' params={{ name: "sally" }} reload={true}>
    Text goes here...
</Link>
<Link to='order-items' params={{ order: someValue }}>
    Text goes here
</Link>

The Redirect component

This component is similar to the Link component except that is does not generate a user interface. When this component is rendered it will immediately redirect the to provided route.

Properties:

  • to is the name of the route
  • params is a key/value pare to set the route parameters
  • reload an optional boolean that if set to true will redirect by reloading the page.
const SomeComponent = () => {
    return authenticated ? <Dashboard /> : <Redirect to='login' params={{ param1: true }} reload={true} />;
};

Hooks

In order to get a reference to the router you can make use of the useRouter hook

const Component = () => {
    const router = useRouter();
    ...
};

API

The router component provides the following API

The go method

    const router = useRouter();

    // Navigate to a route
    router.go('name-of-the-route', { param1,'value1' });

    // Navigate to a route by reloading the page
    router.go('name-of-the-route', { param1,'value1' }, true);

The generateUrl

This method generates a URL based on a path name or a parameters.

const router = useRouter();
const url = router.generateUrl("order-item", { orderNumber: 1000 });

License

MIT