GithubHelp home page GithubHelp logo

smaifullerton-wk / resourceful-redux Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jamesplease/redux-resource

0.0 1.0 0.0 1.46 MB

2kb resource management for Redux

Home Page: https://resourceful-redux.js.org

License: MIT License

JavaScript 99.25% HTML 0.75%

resourceful-redux's Introduction

Resourceful Redux

A tiny but powerful system for managing 'resources': data that is persisted to remote servers.

Gitter Travis build status npm version Test Coverage Code Climate GPA gzip size

Installation

To install the latest stable version:

npm install --save resourceful-redux

Size

Resourceful Redux is around 2kb gzipped, which is typically much smaller than the boilerplate code that it replaces.

Documentation

View the documentation at resourceful-redux.js.org โ‡—.

Quick Start

Follow this guide to get a taste of what it's like to work with Resourceful Redux.

First, we set up our store with a "resource reducer," which is a reducer that manages the state for one type of resource. In this guide, our reducer will handle the data for our "books" resource.

import { store, combineReducers } from 'redux';
import { resourceReducer } from 'resourceful-redux';

const reducer = combineReducers({
  books: resourceReducer('books')
});

const store = createStore(reducer);

Once we have a store, we can start dispatching actions to it. In this example, we initiate a request to read a book with an ID of 24, then follow it up with an action representing success. There are two actions, because requests usually occur over a network, and therefore take time to complete.

import { actionTypes } from 'resourceful-redux';
import store from './store';

// This action represents beginning the request to read a book with ID of 24.
store.dispatch({
  type: actionTypes.READ_RESOURCES_PENDING,
  resourceName: 'books',
  resources: [24]
});

// Later, when the request succeeds, we dispatch the success action.
store.dispatch({
  type: actionTypes.READ_RESOURCES_SUCCEEDED,
  resourceName: 'books',
  resources: [{
    id: 24,
    title: 'My Name is Red',
    releaseYear: 1998,
    author: 'Orhan Pamuk'
  }]
});

Later, in your view layer, you can access information about the status of this request. When it succeeds, accessing the returned book is straightforward.

import { getStatus } from 'resourceful-redux';
import store from './my-store';

const state = store.getState();
// The second argument to this method is a path into the state. This method
// protects you from needing to check for undefined values.
const readStatus = getStatus(store, 'books.meta.24.readStatus');

if (readStatus.pending) {
  console.log('The request is in flight.');
}

else if (readStatus.failed) {
  console.log('The request failed.');
}

else if (readStatus.succeeded) {
  const book = state.books.resources[24];

  console.log('The book was retrieved successfully, and here is the data:', book);
}

This is just a small sample of what it's like working with Resourceful Redux.

For a real-life webapp example that uses many more CRUD operations, check out the zero-boilerplate-redux webapp โ‡—.

Repository Structure

This repository is a Lerna project. That means it's a single repository that allows us to control the publishing of a number of packages:

Package Version Size Description
resourceful-redux npm version gzip size The main library
resourceful-action-creators npm version gzip size The Action Creators extension
resourceful-prop-types npm version gzip size The Prop Types extension

Contributing

Thanks for your interest in helping out! Check out the Contributing Guide, which covers everything you'll need to get up and running.

resourceful-redux's People

Contributors

jamesplease avatar jporry avatar whichsteveyp 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.