All Projects β†’ cjdev β†’ routedux

cjdev / routedux

Licence: MIT License
A router that maps urls to redux actions and vice-versa

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to routedux

react-native-boilerplate
Ready-made structure of your next React Native application within a few minutes.
Stars: ✭ 36 (+89.47%)
Mutual labels:  router
monoreact
πŸ“¦ React workspaces implementation
Stars: ✭ 13 (-31.58%)
Mutual labels:  react-applications
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-26.32%)
Mutual labels:  router
highway
Highway - A Modern Javascript Transitions Manager
Stars: ✭ 1,349 (+7000%)
Mutual labels:  router
whip
Simple fast http server for nim based on httpbeast and nest for high performance routing
Stars: ✭ 55 (+189.47%)
Mutual labels:  router
froute
Type safe and flexible router for React
Stars: ✭ 31 (+63.16%)
Mutual labels:  router
yarr
A React router library enabling the render-as-you-fetch concurrent UI pattern.
Stars: ✭ 97 (+410.53%)
Mutual labels:  router
backbone.base-router
A better starting point for building a new Backbone Router.
Stars: ✭ 49 (+157.89%)
Mutual labels:  router
nuxt-interpolation
Nuxt.js module as directive for binding every link to catch the click event, and if it's a relative link router will push.
Stars: ✭ 38 (+100%)
Mutual labels:  router
trail
Routing library for the Scala platform
Stars: ✭ 76 (+300%)
Mutual labels:  router
chomex
Chrome Extension Messaging Routing Kit / Promisify Chrome Messaging / LocalStorage Object Mapper
Stars: ✭ 41 (+115.79%)
Mutual labels:  router
react-social-network
react social network
Stars: ✭ 13 (-31.58%)
Mutual labels:  react-applications
dodgr
Distances on Directed Graphs in R
Stars: ✭ 106 (+457.89%)
Mutual labels:  router
wolfpacs
WolfPACS is an DICOM load balancer written in Erlang.
Stars: ✭ 1 (-94.74%)
Mutual labels:  router
ROS Scripts
Scripts for RouterOS (MikroTik devices)
Stars: ✭ 81 (+326.32%)
Mutual labels:  router
radvd
radvd | Official repository: https://github.com/radvd-project/radvd
Stars: ✭ 138 (+626.32%)
Mutual labels:  router
TutoAsus
Tutorial on how to setup a nginx reverse proxy on Asus router with Merlin firmware, and get Let's Encrypt certificate with acme.sh.
Stars: ✭ 35 (+84.21%)
Mutual labels:  router
oas2
OpenAPI 2.0 (aka Swagger) utils for Golang.
Stars: ✭ 19 (+0%)
Mutual labels:  router
TG799VAC-XTREME-17.2-MINT
My personal unique wiki for hacking the router firmware used by (Telia)TG799vac Xtream v17.2-MINT delivered from Technicolor
Stars: ✭ 71 (+273.68%)
Mutual labels:  router
VueStudy
Vue.jsε­¦δΉ η³»εˆ—η€ΊδΎ‹δ»£η εŠζ•™η¨‹
Stars: ✭ 80 (+321.05%)
Mutual labels:  router

Routedux β€” Routes the Redux Way npm version Node.js CI

Route Dux

Routedux (πŸ¦† πŸ¦† πŸ¦†) routes URLs to Redux actions and vice versa.

(v1.x only works with React >=16)

Your application doesn't need to know it lives in a browser, but your users want pretty urls and deep links.

Wait, my application doesn't need to know it lives in a browser?

URLs are great for finding things on the internet. But a single page application is not the same as a collection of resources that lives on a remote server.

A single page application is a web application only in the sense that it lives on the web. URLs are are not essential to it working well.

URLs give users accessing your application in a browser the ability to bookmark a particular view in your application so that their expectation of browser-based applications will continue to work.

We think that's a good thing, but we also don't think the idea of url paths should be littered through your application.

When you are developing a redux application, you want your UI to be a pure function of the current state tree.

By adding routes to that, it makes it harder to test. And this difficulty can be compounded by other decisions about how to add routes to your application.

An alternative approach

React Router is the currently-accepted way to do URL routing in React applications. For a standard React application without Redux, this solution isn't too bad. But once you add Redux, things get difficult.

We basically discovered the same lessons as Formidable Labs: React Router is the wrong way to route in Redux apps.

However, we don't think their solution (redux-little-router) goes far enough, as it still embeds the idea of routes throughout your user interface.

Once you separate URLs from your application state, you can easily port it to other environments that don't know what URLs are, and by simply removing the routing declaration, things will work as before.

As an added (and we think absolutely essential) benefit, your entire application becomes easier to test, as rendering is a pure function of Redux state, and model logic and route actions are entirely encapsulated in Redux outside of the app.

Demo Site

See a simple demo documentation site.

Simple Routing in 25 lines

import installBrowserRouter from 'routedux';
import {createStore, compose} from 'redux';

const LOAD_USER = 'LOAD_USER';

function currentUserId() {
  return 42;
};

function reduce(state = initialState(), action) {
  ...
}

const routesConfig = [
  ['/user/:id', LOAD_USER, {}],
  ['/user/me', LOAD_USER, {id: currentUserId()}],
  ['/article/:slug', 'LOAD_ARTICLE', {}],
  ['/', 'LOAD_ARTICLE', {slug: "home-content"}]
];

const {enhancer, init} = installBrowserRouter(routesConfig);

const store = createStore(reduce, compose(
  enhancer
));

//when you are ready to handle the initial page load (redux-saga and similar libraries necessitate this being separte)
init();

Any time a handled action fires the url in the address bar will change, and if the url in the address bar changes the corresponding action will fire (unless the action was initiated by a url change).

Route matching precedence - which route matches best?

Route precedence is a function of the type of matching done in each segment and the order in which the wildcard segments match. Exact matches are always preferred to wildcards moving from left to right.

const routesInOrderOfPrecedence = [
  ["/user/me/update", "/user/me"], // both perfectly specific - will match above any wildcard route
  "/user/me/:view",
  "/user/:id/update", // less specific because 'me' is exact match, while :id is a wildcard
  "/user/:id/:view",
];

Fragment component

Given that every UI state will be in your state tree as a function of your reducer logic, you can express any restriction on which parts of the UI display, even those that have nothing to do with the specific transformations caused by your URL actions.

const state = {
  menu: ...
}

const view = (
  <PageFrame>
      <Fragment state={state} filterOn="menu">
        <Menu />
      </Fragment>
  </PageFrame>
)

// If menu is truthy, this renders as:

(
  <PageFrame>
    <Menu />
  </PageFrame>
)

// If menu is falsy, this renders as:
(
  <PageFrame>
  </PageFrame>
)

// If property is missing in path, it's falsy.

const view = (
  <PageFrame>
      <Fragment state={state} filterOn="menu.missingProp.something">
        <Menu />
      </Fragment>
  </PageFrame>
)

// Renders as:
(
  <PageFrame>
  </PageFrame>
)

ActionLink and pathForAction(action)

Occasionally it is nice to render URLs inside of your application.

As a convenience, we have attached pathForAction to the store object, which uses the same matcher that the action matcher uses. This allows you to create links in your application by using the actions.

const routesConfig = [
  ['/user/:id', LOAD_USER, {}],
  ['/user/me', LOAD_USER, {id: currentUserId()}]
];
/* ... do store initialization */

store.pathForAction({type:LOAD_USER, id: currentUserId()});
/* returns /user/me */

/* ActionLink */

import { ActionLink as _ActionLink } from "routedux";

const store = createStore(...);

class ActionLink extends _ActionLink {
    constructor(props) {
        super({ ...props });
        this.store = store;
    }
}

const action = {
  type: LOAD_USER,
  id: 123
};

return (
  <ActionLink action={action}>Link Text</ActionLink>
);

/* renders as a link to <a href="https://github.com/usr/123">Link Text</a> with the text */

Now you have links, but your links always stay up to date with your routing configuration.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].