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

@ethossoftworks/redux-router

v1.0.0

Published

Redux middleware for handling routing

Downloads

2

Readme

Redux Router

Redux Router is a simple page router built for handling page navigation via Redux.

Documentation

Features

  • Typed routes
  • Page title support
  • Animation support

Motivation

There are several popular routing libraries built for React and Redux that are great. However, two things commonly found in other routing libraries frustrated me:

  1. String path matching for route rendering as opposed to strictly typed Route objects.
  2. Externally stored location state
  3. Lack of typed parameters for routes

Redux Router is my attempt to solve those problems and make page routing in an SPA as developer-friendly as possible.

Installation

redux-router can be installed using NPM or Yarn. The scripts provided by the NPM package are UMD scripts and will also work as script tags.

With Package Manager

yarn add @ethossoftworks/redux-router

With Script Tags

You can find the scripts in either the NPM package or from releases. Only the core script is necessary. The components script adds components and hooks for usage with React.

Note: If loading both scripts, the core script must be loaded before the components script. The components script depends on the core script.

<script src="redux-router.core.js"></script>
<script src="redux-router.components.js"></script>
<script>
    // ReduxRouter.core
    // ReduxRouter.components
</script>

Usage

1. Create Some Routes

Routes are created with the route() function. The route() function specifies the path match (this is the only place you will have to use path matches), the data creator, and an optional title creator. Don't worry about specifying the type on your Routes object, TypeScript type inference works really well here.

// Routes.ts
import { route } from "@ethossoftworks/redux-router"

export const Routes = {
    Home: route({
        path: "/",
    }),
    Articles: route({
        path: "/articles",
        title: () => `Articles`,
    }),
    Article: route({
        path: "/articles/:articleId",
        data: (articleId: number) => ({ params: { articleId: articleId.toString() } }),
        title: (data) => `Article ${data.params.articleId}`,
    }),
}

2. Initialize the Router Middleware and Add the Router Reducer

To use Redux Router you need to create the middleware using createRouterMiddleware() and run the init() method after createStore() has been called. The init() function initializes the router state with the current route.

You will also need to add the Router Reducer to your reducer. You may use any name for the router reducer in your application state as long as the proper key is passed into createRouterMiddleware().

// Store.ts
import { RouterState, createRouterMiddleware } from "@ethossoftworks/redux-router"
import { createStore, combineReducers, applyMiddleware } from "redux"
import { Routes } from "./Routes"

export type AppState = {
    router: RouterState
}

function configureStore() {
    const router = createRouterMiddleware<AppState>(Routes, "router")
    const store = createStore(
        combineReducers({ router: router.reducer }),
        applyMiddleware(router.middleware)
    )
    router.init()
    return store
}

export const store = configureStore()

3. Render a Page

All that's left is to render a page based on the current route! This can be done with a React components or more standard JS control structures.

React Components

// App.ts
import React from "react"
import { Routes } from "./Routes"
import { PageNotFound } from "@ethossoftworks/redux-router"
import { Route } from "@ethossoftworks/redux-router/components"

export function App() {
    return (
        <RouteSwitch>
            <Route matches={Routes.Home}>
                Home
            </Route>
            <Route matches={Routes.Articles}>
                Articles
            </Route>
            <Route matches={Routes.Article}>
                Article
            </Route>
            <Route matches={PageNotFound}>
                Page Not Found
            </Route>
        </RouteSwitch>
    )
}

Vanilla JS

// App.ts
import React from "react"
import { Routes } from "./Routes"
import { isRouteMatch, PageNotFound } from "@ethossoftworks/redux-router"

export function App() {
    const route = useRoute()

    if (isRouteMatch(route.item, Routes.Home)) {
        return "Home"
    } else if (isRouteMatch(route.item, Routes.Articles)) {
        return "Articles"
    } else if (isRouteMatch(route.item, Routes.Article)) {
        return "Article"
    } else if (isRouteMatch(route.item, PageNotFound)) {
        return "Page Not Found"
    }
}

4. Navigate To Another Page

Navigating to another page can be done by dispatching actions directly or by using the Link component

import React from "react"
import { Routes } from "./Routes"
import { RouterActions } from "@ethossoftworks/redux-router"
import { Link } from "@ethossoftworks/redux-router/components"

function Home() {
    const dispatch = useDispatch()

    return (
        <>
            <Link to={Routes.Article(1)}></Link>
            <Button onClick={() => dispatch(RouterActions.navigate(Routes.Article(1)))}>
        </>
    )
}

Additional Documentation

For addition documentation, please read the guides and api documentation