0.0.45-alpha • Published 2 years ago

@crux/redux-router v0.0.45-alpha

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

@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>
  );
}
0.0.45-alpha

2 years ago

0.0.40-alpha

2 years ago

0.0.43-alpha

2 years ago

0.0.41-alpha

2 years ago

0.0.42-alpha

2 years ago

0.0.44-alpha

2 years ago

0.0.39-alpha

2 years ago

0.0.38-alpha

2 years ago

0.0.37-alpha

2 years ago

0.0.36-alpha

2 years ago

0.0.29-alpha

2 years ago

0.0.35-alpha

2 years ago

0.0.23

2 years ago

0.0.30-alpha

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.32-alpha

2 years ago

0.0.31-alpha

2 years ago

0.0.28-alpha

2 years ago

0.0.27-alpha

2 years ago

0.0.33-alpha

2 years ago

0.0.26

2 years ago

0.0.26-alpha

2 years ago

0.0.34-alpha

2 years ago

0.2.0-alpha

2 years ago

0.1.2-alpha

2 years ago

0.1.1-alpha

2 years ago

0.1.0-alpha

2 years ago