Coder Social home page Coder Social logo

redux-modules's Introduction

redux-modules npm version Circle CI

redux-modules is a refinement on the Redux module concept with developer experience in mind. It provides:

  • An intuitive way define actions and state transformations
  • The ability to add action creator middleware
  • propType style typechecking for action payloads Example
  • A decorator for passing bound module actions to React views

Getting Started

Install

npm install redux-modules --save

Usage Example

Here's an example of a simple todo app. First create a module that allows todos to be created and destroyed.

src/modules/todos.js

import { createModule } from 'redux-modules';

export default createModule({
  name: 'todos',
  initialState: List(),
  transformations: [
    {
      action: 'CREATE',
      payloadTypes: {
        todo: shape({
          description: string.isRequired,
        }),
      },
      reducer: (state, {payload: { todo }}) =>
        state.update('collection', todos => todos.push(fromJS(todo))),
    },
    {
      action: 'DESTROY',
      payloadTypes: {
        index: number.isRequired,
      },
      reducer: (state, {payload: { index }}) => {
        state.update('collection', todos => todos.delete(index)),
    },
  ],
});

Once the module is complete, the reducer has to be added to the store.

src/App.jsx

const store = createStore(todoModule.reducer, {});

export default const App = props => (
  <Provider store={store}>
    <Todos {...props}/>
  </Provider>
)

Alternatively, use ModuleProvider to allow reducers to be automatically added to the store at runtime.

import { ModuleProvider } from 'redux-modules';
const store = createStore(state => state, {});

export default const App = props => (
  <ModuleProvider store={store}>
    <Todos {...props}/>
  </ModuleProvider>
)

The last step is to connect the module to the view. This works like a normal Redux connect with the added bonus of auto dispatching and namespacing actions.

src/views/Todos.jsx

import { connectModule } from 'redux-modules';
const selector = state => {
  return {
    todos: state.get('todos').toJS(),
  };
};

@connectModule(selector, todoModule)
export default class Todos extends Component {
  static propTypes = {
    todos: array,
    actions: shape({
      todos: shape({
        create: func,
        destroy: func,
      }),
    }),
  };

That's it! Check the documentation for comparisons with idiomatic Redux, in depth examples, and advanced usage.

Documentation

redux-modules's People

Contributors

farism avatar mboperator avatar wtgtybhertgeghgtwtg avatar

Watchers

 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.