Git Product home page Git Product logo

dhistory's Introduction

dhistory

Dispatches actions on history state changes (ie. push state or navigate).

Getting started

Install dhistory using npm:

npm install -s dhistory

Or via yarn:

yarn add dhistory

Define routes and connect to your store:

import { createStore, applyMiddleware } from 'redux';
import { createBrowserHistory } from 'dhistory';

const routes = {
  '/posts/:id': 'VIEW_POST',
  '/posts': 'VIEW_POSTS',
  '/': 'VIEW_HOME',
};

const reducer = (state, action) => {
  switch (action.type) {
    case 'VIEW_POST':
      return { ...state, post_id: action.id, view: 'POST' };
    case 'VIEW_POSTS':
      return { ...state, view: 'POSTS' };
    case 'VIEW_HOME':
      return { ...state, view: 'HOME' };
    ...
  }
};

const store = createStore(reducers, applyMiddleware(createBrowserHistory(routes)));

Your store will now receive actions defined in routes when the browser's history changes. For example, when the URL changes to /posts/123 the action VIEW_POST will be dispatched with the param id: 123.

To navigate simply dispatch the action, along with any parameters, as you normally would in your Redux app:

dispatch({ type: 'VIEW_POST', id: 1 });

Which will update the location to /posts/1 and dispatch the action.

Motivation

There are many routers and history management libraries but none of them seem to truly fitted with Redux. Navigations don't match with actions or follow the same flow and are treated separate from everything else.

Example:

import { Router, Route } from 'react-router';

<Router>
  <Route to="/post/new" component={NewPostView}/>
  <Route to="/post/:id" component={PostView}/>
</Router>

function createPost(data) {
  // dhistory will update the history for us, otherwise we'd have to manually update it:
  // ie. history.push(`/posts/${post.id}`);
  return { type: 'POST_CREATED', post };
}

function NewPostView({ store }) {
  return (
    <PostForm onSubmit={data => store.dispatch(createPost(data))}>
      ...
    </PostForm>
  );
}

API

createBrowserHistory(routes, options) and createMemoryHistory(routes, options) and createHashHistory(routes, options)

Returns a router object.

Parameters

  • routes - A routes object. Keys are patterns and values are either the action name or an action object.
  • options - Options for browser history. Read ReactTraining's history package for more info.

Returns a function to be passed to applyMiddleware.

Attribution

Much of this work wouldn't be made possible without the efforts put to react-router.

dhistory's People

Contributors

evilmarty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.