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

@crux/redux-router

v0.0.45-alpha

Published

`@crux/redux-router` is a lightweight router that hooks into Redux, saving routing changes to state and enabling route navigation by dispatching Redux actions.

Downloads

90

Readme

@crux/redux-router

@crux/redux-router is a lightweight router that hooks into Redux, saving routing changes to state and enabling route navigation by dispatching Redux actions.

Installation

npm install --save @crux/redux-router

Usage

Creating the router

// ./router.ts
import { createReduxRouter } from '@crux/redux-router';

export const { actions, Link, middleware, reducer } = createReduxRouter({
  users: '/users',
  user: '/users/:id'
});

The route patterns are really powerful, see the tests for more examples. Suffice it to say that you can match pretty much any route you can dream up.

Connecting to the Redux store

Next you need to hook the reducer and middleware into your Redux store. The reducer manages the state, and the middleware connects Redux to the router itself.

import { configureStore } from '@reduxjs/toolkit';
import { middleware, reducer } from './router';

const store = configureStore({
  reducer: {
    router: routerReducer,
  },
  middleware: [middleware]
});

Navigating to a route

To navigate to a route, just dispatch the navigate action:

import { actions } from './router';

dispatch(actions.navigate('user', { id: '1' }));

// Route changes to /users/1

Or build the action manually:

import { EventType } from 'redux-router';

dispatch({
  type: EventType.Navigate,
  payload: {
    name: 'user',
    params: { id: '1' }
  }
});

You can also use the provided Link component:

import { Link } from './router';

export function App() {
  const user2Route = { name: 'user', params: { id: '2' } };

  return (
    <div>
      ...
      <Link to={user2Route} text="Go to user 2" />
    </div>
  );
}

Responding to state changes

The redux-router reducer provides the following state (using the above example with the id in the route):

{
  route: {
    name: string;
    params: { id: string }
  }
}

There is no useParams or useLocation provided by redux-router because all this data is available in the state. Just use selectors as you would to retrieve any other slice of state.

For example:

// ./selectors.ts
import { createSelector } from '@reduxjs/toolkit';
import { State } from '@crux/redux-router';

// Assuming you've named the slice `router`
const selectRouter = (state: { router: State }) => state.router;

export const selectRoute = createSelector(
  selectRouter,
  (router) => router.route
);

Then useSelector in your component:

import { useSelector } from 'react-redux';
import { selectRoute } from './selectors';

export function MyComponent() {
  const route = useSelector(selectRoute);

  return (
    <div>
      <div>Current route: {JSON.stringify(route)}</div>
    </div>
  );
}