Giter VIP home page Giter VIP logo

redux-loader's Introduction

Redux Loader

Codeship Status for Versent/redux-loader

A high order component and request management utility for Redux.

  • Loads resources and passes them to the child components via props.
  • Keeps tracks of requests already done and avoids duplicate requests.

Usage

Your Redux application must include the Redux-Loader reducer. It needs to be under the requests key:

import reduxLoader from 'redux-loader'

const allReducers = combineReducers({
  requests:        reduxLoader.reducer,
  ...
})

const store = finalCreateStore(allReducers)

This reducer is used for keeping track of pending and completed requests.

Then create a high order component to load the data:

import reduxLoader    from 'redux-loader'
import { connect }    from 'react-redux'
import _              from 'lodash'
import Show           from './Show.jsx'
import Busy           from './Busy.jsx'

const Loader = reduxLoader.create({

  // React component to show while loading the resources
  busy:      Busy,

  // React component what will be rendered when resources are loaded
  component: Show,

  /*
  `resources` is a map with resources to load before rendering the component above
  this can be one or many.

  The component above will receive these resources as props e.g. post
  */
  resources: {

    /*
    These resources will be send to the child component via props.

    You must return a function for each resource you want to load.

    This function takes:

    - options.props
    - options.context
    - options.dispatch

    You need to pass the state you need to the Loader using connect.
    You function will be called again each time your component receives new props,
    meaning that you will always have fresh props.

    This function must return an object with keys {id, find, load}
    */
    user(options) {

      const userId = options.props.userId
      const id = `/users/${userId}`

      return {
        /*
        Loader must return an id for the current resource.
        This id will be used to keep track of request already done.
        */
        id,

        /*
        Ask to load the resource/s
        This is triggered when a request has not been done before.
        This is determined by `id` above.
        */
        load() {
          const action = actions.fetchOne(userId)
          return options.dispatch(action)
        },

        /*
        Find the resource/s in your state.

        This is called when a request has been done successfully.
        Loader uses the given `id` above to determine this.
        */
        find() {
          return _.find(options.props.users, {id: userId})
        },

      },

      /*
      You may also load several resources at once
      */
      posts(options) {
        ...
      }

    }
  }
});

// You need to pipe Loader through connect
export default connect(state => state)(Loader)

Example app here

How does it work

Heavily inspired by Marty

redux-loader's People

Stargazers

Kevin J. Hanna avatar Simon Elliott avatar Nicolas Quiceno B avatar Sérgio A. Kopplin avatar Anthony Mittaz avatar Alexey Yurchenko avatar Sam Gluck avatar Siddhartha Basu avatar  avatar Taichi Yamakawa avatar Chris Lusted avatar oreshinya avatar Ilya Ayzenshtok avatar  avatar Miki Oracle avatar Victor Bjelkholm avatar Arjun Balaji avatar Andrei Antropov avatar Gustavo Ajzenman avatar Yifan Wang avatar Stefano Verna avatar João Cunha avatar Tim Dussinger avatar Andrey Subbotin avatar Roberto Ortis avatar Sudhakar avatar Jan Vlnas avatar Forrest Galloway avatar Winston Fassett avatar Petr Halas avatar Felix Stürmer avatar Korachal Phadvibulya avatar Dave Slutzkin avatar Lennard van Gunst avatar Sebastian Porto avatar Jeroen Coumans avatar Brad Pillow avatar RnbWd avatar

Watchers

Darcy Laycock avatar Samuel Richardson avatar Dave Slutzkin avatar James Cloos avatar Felix Stürmer avatar Sebastian Porto avatar Brad Denver avatar Nath Kewley avatar Alastair Firth avatar Francesco Orsenigo avatar (◕ᴥ◕) avatar Ben Flanagan avatar  avatar

redux-loader's Issues

When resource find returns null, infinite busy loop occurs.

  return {
    id,
    load() {
      const action = actions.fetchOne(ePId);
      return options.dispatch(action);
    },
    find() {
      // if I mess up the prop name (e.g. engagement_packages) the loader component will get stuck in an infinite loop instead of rendering the component.
      return _.find(options.props.engagementPackages, {id: ePId});
    },
  };

Doesn't work with React 0.14

All I did was copy and past your example into my project and I get this error:

selection_397

My best guess is that because you're specifying "react": "^0.13.3", in your dependencies, redux-loader is bringing its own instance of React, which is building incompatible components.

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.