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

react-slex-router

v1.20.4

Published

[![CircleCI](https://circleci.com/gh/alexstroukov/slex-libs.svg?style=svg)](https://circleci.com/gh/alexstroukov/slex-libs)

Downloads

27

Readme

CircleCI

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:

  1. Url changes set store state to loading and puts route into pending state to indicate that a route is changing.

  2. Route validation is preformed to determine if access is allowed for the user.

  3. 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

import React from 'react'
import ReactDOM from 'react-dom'
import slexStore from 'slex-store'
import { Provider } from 'react-slex-store'
import route, { Router, Route, createRouteMiddleware } from 'react-slex-router'

const store = slexStore.createStore({
  reducer: slexStore.createReducer({
    route
  }),
  middleware: [
    createRouteMiddleware({ validators: new Validators() })
  ]
})

store.subscribe(renderApp)

function renderApp (state) {
  ReactDOM.render((
    <Provider store={store}>
      <Router>
        <Route path={'/'} name={'HOME'}>
          <HomePage />
        </Route>
        <Route path={'/login'} name={'LOGIN'}>
          <LoginPage />
        </Route>
        <Route path={'/items'} name={'ITEMS'} validate={'validateItems'}>
          <Items />
        </Route>
        <Route path={'/items/:id'} name={'ITEM_DETAILS'} validate={'validateItemDetails'}>
          <ItemDetails />
        </Route>
      </Router>
    </Provider>
  ), document.getElementById('app'))
}

class Validators {
  validateItems = ({ routeName, routeState }) => {
    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.

import { actionTypes as routeActionTypes } from 'react-slex-router'

function loadDataOnRouteChangeMiddleware (dispatch, getState, action) {
  const { type: actionType } = action
  const { route: { pendingRoute: { routeName } = {} } } = getState()
  if (actionType === routeActionTypes.PENDING_ROUTE_READY && routeName === 'YOUR_ROUTE') {
    dispatch(loadDataAction)
  }
}

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.

import { replace, actionTypes as routeActionTypes } from 'react-slex-router'

const redirectOnAccessDenied = ({ dispatch, prevState, nextState, action }) => {
  const { type: actionType } = action
  const { route: { routeState: { path: currentPath } } = {} } = prevState
  if (actionType === routeActionTypes.PENDING_ROUTE_ACCESS_DENIED) {
    const isLoggedIn = true
    if (isLoggedIn) {
      replace({ path: currentPath || '/' })
    } else {
      replace({ path: '/login' })
    }
  }
}