Giter Site home page Giter Site logo

riot-state's Introduction

riot-state

Simplest centralized state management for Riot.JS

โš™ CodeSandbox Example

Usage

A riot-state store consists of following properties: name, state, actions. And provides following methods: dispatch, install

Store name

Property name is required if initial state would load from a global object. By default riot-state loads initial data from document.__GLOBAL_SHARED_STATE [name]

If there are more than one store it is possible to access them in an action context by name.

const actions = {
  ping(){
    this.Stores.storeName.dispatch('pong')
  }
}
State

A flat javascript object.

Actions

A javascript object containing functions. An action cannot be an arrow function

Component context

A component has to provide a list of shared variables that will be injected in component scope.

export default {
  shared: ['foo', 'bar']
}

Example:

//store.js
import { createStore } from "./state";

const name = "example";

const state = {
  number: 0
};

const actions = {
  increment(value = 1) {
    this.number += value;
  },

  decrement(value = 1) {
    this.number -= value;
  }
};

export default createStore({
  name,
  state,
  actions
});
<number>
  <div class="number">
    {number}
  </div>
  <script>
    import store from './store'
    export default ()=> ({
      shared: [
        'number'
      ],
      onMounted(){
        store.install(this)
        this.update()
      },
      onUpdated(){
        console.log(this.number)
      }
    })
  </script>
</number>
<controls>
  <button onclick={plus}>+</button>
  <button onclick={minus}>-</button>
  <script>
    import store from './store'
    export default () => ({
      onMounted(){
        store.install(this)
      },
      plus(){
        store.dispatch('increment')
      },
      minus(){
        store.dispatch('decrement')
      },
    })
  </script>
</controls>

License

MIT

riot-state's People

Contributors

nesterow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

riot-state's Issues

Can't manage to call an action from another action

Playing in the sandbox but can't call one action from another action

import { createStore } from "riot-state";

const name = "example";

const state = {
  number: 0
};

const actions = {
  increment(value = 1) {
    this.Stores.example.dispatch("realincr", 5);
  },

  realincr(value) {
    this.number += value;
  },

  decrement(value = 1) {
    this.number -= value;
  }
};

export default createStore({
  name,
  state,
  actions
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.