react-redux-guard

0.1.2 • Public • Published

react-redux-guard

protect those containers from breaking when state needs data

Getting Started

basic usage with protect and required params:

  • guard : [Function] - handler that detects and resolves an invalid state
  • mstp - mapStateToProps for connect
  • mdtp - mapDispatchToProps for connect
import { protect, inSuspense } from "react-redux-guard";

const guard = props => {
  if (!props.hasProfile) {
    inSuspense(props.fetchProfile);
  }
};

const mstp = state => ({
  hasProfile: selectors.hasProfile(state)
});
const mdtp = dispatch => ({
  fetchProfile: done => dispatch(actions.fetchProfile(done))
});

ProfileViewer.Protected = protect(guard, mstp, mdtp)(ProfileViewer.Connected);

define more than one guard config to protectAll

import { protectAll, inSuspense } from "react-redux-guard";

export const profile = {
  mstp: state => ({
    hasProfile: selectors.hasProfile(state)
  }),
  mdtp: dispatch => ({
    fetchProfile: done => dispatch(actions.fetchProfile(done))
  }),
  guard: props => {
    if (!props.hasProfile) {
      inSuspense(props.fetchProfile);
    }
  }
};

ProfileViewer.Protected = protectAll([guards.profile]);

Doing stuff in Suspense

TODO: describe inSuspense

Readme

Keywords

none

Package Sidebar

Install

npm i react-redux-guard

Weekly Downloads

3

Version

0.1.2

License

ISC

Unpacked Size

4.92 kB

Total Files

5

Last publish

Collaborators

  • json2d