React Slex Router
$ npm install react-slex-router
react-slex-router
is a component driven router implementation for react
. It is connected to slex-store
via react-slex-store
and its state is kept in its own store similar to how redux-router
combined with react-router
.
It differs from that however because react-slex-router
breaks routing into 3 stages:
-
Url changes set store state to loading and puts route into pending state to indicate that a route is changing.
-
Route validation is preformed to determine if access is allowed for the user.
-
Route is taken out of pending status and route workflow is completed.
Route Action Sequence
ROUTE_LOADING
- Triggered when url changes. Puts said route in pending state alongside the current route and set the store status to loading. { status: 'LOADING', routeName, routeState, pendingRoute: { routeName, routeState } }
↓
PENDING_ROUTE_READY
- Triggered when validate
resolves with a truthy result. Sets the pending route to routeName
and routeState
upon successful validation and puts store in ready state. { status: 'READY', routeName, routeState }
PENDING_ROUTE_ACCESS_DENIED
- Triggered when validate
resolves with a falsy result. Sets store in ready state upon unsuccessful validation whilst keeping the pending route. { status: 'READY', routeName, routeState,pendingRoute: { routeName, routeState } }
↓
PENDING_ROUTE_ERROR
- Triggered when validate
resolves with an error or rejected promise. Sets store in error state whilst keeping the pending route. { status: 'ERROR', routeName, routeState, pendingRoute: { routeName, routeState } }
Example Usage
const store = slexStore store { ReactDOM} { const userIsAllowerToViewRoute = true return userIsAllowerToViewRoute }
Route Validation
You can validate access to routes by providing a validate function to Route
. It can be be sync or async and resolve truthy for valid routes ({ routeName, routeState }) => Promise<boolean> || boolean
<Route validate={validate} path={path} name={name} />
Useful Middleware
Loading data on route change
When routing to a page you often need to load data in stores that are used to display data on the page, this is often done on componentDidMount
. You can also do it via middleware to decouple this logic from the UI.
{ const type: actionType = action const route: pendingRoute: routeName = {} } } = if actionType === routeActionTypesPENDING_ROUTE_READY && routeName === 'YOUR_ROUTE' }
Redirecting on route access denied
When access to a route is denied it is good practice to redirect a user either to a route where the denial of access can be addressed - usually a login page or an error page. You can achieve this with a middleware.
const redirectOnAccessDenied = { const type: actionType = action const route: routeState: path: currentPath = {} } = prevState if actionType === routeActionTypesPENDING_ROUTE_ACCESS_DENIED const isLoggedIn = true if isLoggedIn else }