redux-rest-easy
  • ⛳@brigad/redux-rest-easy
  • docs
    • api
      • reducer
      • createResource
        • actionsConfig
        • actions
        • selectors
      • connect
      • reducer
        • reducers
      • createResource
      • getPersistableState
      • initializeNetworkHelpers
      • reset
    • principles
      • persistence
      • actions
      • selectors
      • preflight
      • reducers
  • LICENSE
  • CHANGELOG
  • CODE_OF_CONDUCT
  • CONTRIBUTING
  • TODO
Powered by GitBook
On this page
  • Release article
  • Installation
  • Problem
  • Solution
  • API
  • Internals
  • Core principles
  • Minimal Example
  • Peer dependencies
  • Examples
  • Simple Example
  • Pagination (TODO, coming soon)
  • Data invalidation (TODO, coming soon)
  • Multiple requests (e.g. S3 signed upload) (TODO, coming soon)
  • Cache hints (TODO, coming soon)
  • Store persistence (TODO, coming soon)
  • Contributors

@brigad/redux-rest-easy

Nextdocs

Last updated 6 years ago

Redux/React/React Native framework handling network requests, state management, selectors, caching and much more

Installation

yarn add @brigad/redux-rest-easy

Or, if you are using npm:

npm install --save @brigad/redux-rest-easy

Problem

At Brigad, we have been extensively using redux and redux-thunk to perform network requests, and store/access the resulting data, and we always felt some pain points, or at least like there were things we could do better:

  • We were often copying/pasting a lot of code (along with some logic regarding caching, hooks, etc) from one file to another each time we would create a new resource or action

  • Our state was not organized at all, and accessing it was messy and error-prone

  • We had a huge caching problem: sometimes performing unnecessary requests, sometimes not performing requests which should have been

  • We had no way to know if a given component was performing an action, we only knew if an action was being performed on a given resource

Solution

To solve the problems listed above, redux-rest-easy generates actions, reducers, and selectors, and also manages the state's data and metadata for your network requests. It is easy to use, and to observe via the Redux Devtools.

It also provides sensible defaults, allowing you to use it with almost no configuration, but also to customize anything you would like.

API

import {
  createResource,
  reducer,
  connect,
  reset,
  initializeNetworkHelpers,
  getPersistableState,
} from '@brigad/redux-rest-easy';

Internals

Core principles

Minimal Example

// users.js

import { createResource } from '@brigad/redux-rest-easy';

const users = createResource('users')({
  retrieve: {
    method: 'GET',
    url: 'https://my-api.com/users',
    afterHook: () => console.log('Users retrieved successfully'),
  },
});

const {
  actions: { retrieve: retrieveUsers },
  selectors: {
    resource: { getResource: getUsers },
    retrieve: {
      request: { isPerforming: isRetrievingUsers },
    },
  },
} = users;

export { retrieveUsers, getUsers, isRetrievingUsers };
// reducers.js

import { reducer } from '@brigad/redux-rest-easy';

const reducers = combineReducers({
  restEasy: reducer,
});
// UsersList.js

import React, { Component } from 'react';
import { connect } from '@brigad/redux-rest-easy';
import {
  retrieveUsers,
  getUsers,
  isRetrievingUsers,
} from './redux-rest-easy/users';

class UsersList extends Component {
  state = {
    error: false,
  };

  componentDidMount() {
    this.props.retrieveUsers(this.onSuccess, this.onError);
  }

  onSuccess = () => {
    this.setState({ error: false });
  };

  onError = () => {
    this.setState({ error: true });
  };

  render() {
    if (this.props.isRetrievingUsers) {
      return <div>{'Loading...'}</div>;
    }

    if (this.state.error) {
      return (
        <div>{'There seems to be a problem... A network error occured.'}</div>
      );
    }

    return <Users items={this.props.users} />;
  }
}

const mapStateToProps = state => ({
  users: getUsers(state),
  isRetrievingUsers: isRetrievingUsers(state),
});

const mapDispatchToProps = dispatch => ({
  retrieveUsers: (onSuccess, onError) =>
    dispatch(retrieveUsers({ onSuccess, onError })),
});

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(ConnectedComponent);

Peer dependencies

Examples

Displays a list of users and allows to create new ones.

Pagination (TODO, coming soon)

Displays a paginated list, with seamless query-based selectors and cache.

Data invalidation (TODO, coming soon)

Invalidates store data after a successful POST request

Multiple requests (e.g. S3 signed upload) (TODO, coming soon)

Performs multiple requests in beforeHook before the final one, to upload a signed file to S3.

Cache hints (TODO, coming soon)

Makes use of cache hints to customize the built-in cache.

Store persistence (TODO, coming soon)

Introduces store persistence in the "Pagination" example, so that data persists after the page is refreshed.

Contributors

And the cherry on the top: it works seamlessly with and !

for a small example, or to get started! To learn more about the problem and solution, you can also read the .

- easily generate then export your actions and selectors from one file

- plug a single reducer to your state, we handle the rest

- connect your components to the state so the magic can happen

- reset redux-rest-easy's whole state (you can reset parts of the state with actions generated by createResource)

- provide your own network handlers (optional, fallback to included defaults)

- transform the state before storing it, in order to later persist it (using , , or friends)

- defining your actions with createResource

- actions generated by createResource

- selectors generated by createResource

Redux-rest-easy assumes you are using (or ) and .

Redux-rest-easy also uses under the hood, to handle async actions, and therefore requires you to use redux-thunk's middleware in your store. If you are already using redux-thunk, then you have nothing more to do. Else, follow for a quick setup.

Thanks goes to these people ():

This project follows the specification. Contributions of any kind welcome!

⛳
redux-offline
redux-persist
createResource
reducer
connect
reset
initializeNetworkHelpers
getPersistableState
redux-offline
redux-persist
Actions configuration
Actions
Selectors
Preflight checks
Actions
Reducers
Selectors
react
react-native
react-redux
redux-thunk
redux-thunk's docs
Simple Example
emoji key
all-contributors
release article
Scroll down
browse the documentation
Release article
Adrien HARNAY
💻
📖
⚠️
📝
🤔
🚇
👀
Thibault Malbranche
🐛
💻
🤔
👀
Grisha Ghukasyan
🤔
Aymeric Beaumet
🤔
Jess
🐛
📖
Matt Labrum
💻
All Contributors