Socket
Book a DemoInstallSign in
Socket

with-rematch

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

with-rematch

A HoC that implement reducer a la Rematch

latest
Source
npmnpm
Version
1.0.10
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

with-rematch

🕶 A HoC that implement reducer a la Rematch

Inpired By Rematch.js ♡

Instalation

npm install -g with-rematch
or
yarn add with-rematch

Getting Started

Step 1: Model

const model = {
  state: 0,
  reducers: {
    increment(state, payload, props) {
      console.log(state); // the state
      console.log(payload); // the payload passed via action
      console.log(props); // component props
      return state + (payload || 1);
    },
    decrement(state, payload, props) {
      return state - (payload || 1);
    }
  },
  effects: actions => ({
    async asyncIncrement(payload, state, props) {
      await new Promise(resolve => setTimeout(resolve, 1000));
      actions.increment();
    }
  }),
  lifecycle: {
    componentDidMount() {
      console.log('componentDidMount', this);
    }
  }
};

Understanding models is as simple as answering a few questions:

  • What is my initial state? state
  • How do I change the state? reducers
  • How do I handle async actions? effects with async/await

Step 2: HoC Props (state, actions)

Actions is how we trigger reducers & effects in your models. actions props standardizes your actions without the need for writing action types or action creators.

// reducers
actions.increment();
actions.increment(5);
actions.decrement(2);

// effects
actions.asyncIncrement(10);
const Counter = ({state, actions}) => (
  <div>
    Value: {state}
    <button onClick={actions.increment}>INCREMENT</Button>
    <button onClick={actions.decrement}>DENCREMENT</button>

    <button onClick={actions.asyncIncrement}>ASYNC INCREMENT</button>
    <button onClick={() => actions.increment(5)}>INCREMENT 5</Button>
  </View>
);

export default withRematch(model)(Counter);

Step 3: Lifecycle

TODO

Complete Example

import withRematch from 'with-rematch';

const model = {
  state: 0,
  reducers: {
    increment(state, payload, props) {
      console.log(state);   // the state
      console.log(payload); // the payload passed via action
      console.log(props);   // component props
      return state + (payload || 1);
    },
    decrement(state, payload, props) {
      return state - (payload || 1);
    }
  },
  effects: actions => ({
    async asyncIncrement(payload, state, props) {
      await new Promise(resolve => setTimeout(resolve, 1000));
      actions.increment();
    }
  }),
  lifecycle: {
    componentDidMount() {
      console.log('componentDidMount', this);
    }
  }
};

const Counter = ({state, actions}) => (
  <div>
    Value: {state}
    <button onClick={actions.increment}>INCREMENT</Button>
    <button onClick={actions.decrement}>DENCREMENT</button>

    <button onClick={actions.asyncIncrement}>ASYNC INCREMENT</button>
    <button onClick={() => actions.increment(5)}>INCREMENT 5</Button>
  </View>
);

export default withRematch(model)(Counter);

Keywords

redux

FAQs

Package last updated on 31 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts