1.0.10 ā€¢ Published 5 years ago

with-rematch v1.0.10

Weekly downloads
12
License
MIT
Repository
github
Last release
5 years ago

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:

  1. What is my initial state? state
  2. How do I change the state? reducers
  3. 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);
1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago