rematch-state-plugin

use `state` & `setState` in rematch effects

Usage no npm install needed!

<script type="module">
  import rematchStatePlugin from 'https://cdn.skypack.dev/rematch-state-plugin';
</script>

README

rematch-state-plugin

use state & setState in rematch effects

Build Status Coverage Status npm version npm downloads npm license

Install

$ npm i -S rematch-state-plugin

API

const rematchStatePlugin = require('rematch-state-plugin')

add plugin entry

import {init} from '@rematch/core'

const store = init({
  models: {foo, bar},
  plugins: [rematchStatePlugin()],
})

plugin create options

const {state, setState, setStateAction} = {
  state: 'state',
  setState: 'setState',
  setStateAction: '__rematch_state_plugin_@@_set_state__',
}

use this.state & this.setState in effects

// models/count.js with this plugin enabled
export default {
  state: {
    val: 1,
  },

  effects: {
    add() {
      this.setState({val: this.state.val + 1})
    },
  },
}
// models/count.js without this plugin enabled
export default {
  name: 'count',

  state: {
    val: 1,
  },

  setState(payload) {
    return {...state, ...payload}
  },

  effects: {
    add(payload, rootState) {
      const nsp = 'count'
      const {val} = rootState[nsp]
      this.setState({val: val + 1})
    },
  },
}

Changelog

CHANGELOG.md

License

the MIT License http://magicdawn.mit-license.org