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