@the-/store

15.5.0 • Public • Published

@the-/store

npm Version

Redux wrapper for the-framework

Installation

$ npm install @the-/store --save

Usage

'use strict'

const theStore = require('@the-/store')

const { Scope } = theStore

// Scoped state class
class CounterScope extends Scope {
  // Define initial state
  static get initialState() {
    return 0
  }

  // Define reducer factory
  static get reducerFactories() {
    return {
      increment(amount) {
        return (state) => state + amount
      },
    }
  }
}

async function tryExample() {
  const store = theStore()

  // Create state instance and attach to the store
  store.load(CounterScope, 'counterA')
  store.load(CounterScope, 'counterB')

  {
    // Access to loaded store
    const { counterA } = store

    // Each instance has dispatcher methods which share signatures with reducerFactories
    // Invoking this method dispatches an action and reduce it into state
    // The action looks like { type: "counterA/increment", payload: [1] }
    counterA.increment(1)

    // Access to the scoped state
    console.log(counterA.state) // -> 1
  }

  // Store it self has all state
  console.log(store.state) // -> { counterA: 1 }
}

tryExample().catch((err) => console.error(err))

API Guide

See API Guide for more detail

License

This software is released under the MIT License.

Links

Readme

Keywords

Package Sidebar

Install

npm i @the-/store

Weekly Downloads

1

Version

15.5.0

License

MIT

Unpacked Size

135 kB

Total Files

22

Last publish

Collaborators

  • okunishinishi