Skip to content

Riotjs tiny State management lib - Riot 4 -Riot.js

License

Notifications You must be signed in to change notification settings

klicat/riot-stx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

riot-stx : Riotjs tiny state management lib

Demo 1 Demo 2

Update and propagate state in your app

  • Easy to use : NO subscribing complex functions or stores or action... to define !
  • Auto-update components without to call this.update()
  • update are propagated in two ways binding : main Js (state object) <--> components (stx object)
  • Inside components, use object 'stx'. Outside of components use object 'state'
  • Inside riot component you must subscribe to interested state keys in the definition of component. Just use stx:{key1:'defaultValue',key2...} It's all.
  • In riot cycle event : just use this.stx.key1="new val" to update each component that have subscribed to this key (no need to call this.update() )
  • Outside of components, to update key of global state, use state.mykey='something' or use riotStx.setState(objectStateToDeepMerge) Or riotStx.setOneState(key, value)
  • Use riotStx.subscribeState('myKey',callback) to subscribe to an update on 'myKey' and call your own callback({key:'myKey',value:'myValue'}
  • Call riotStx.optionsState(true) if you prefer that the first meet of an stx.key in component declaration auto-set the global state with this default value
  • 1.47KB

Usage

npm i riot-stx@https://github.com/klicat/riot-stx.git

CDN

https://cdn.jsdelivr.net/gh/klicat/riot-stx/riot-stx.min.js

Import

  • include riot-stx.js as javascript
  • or use :
import riot-stx
window.riotStx=riotStx

Example

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/klicat/riot-stx/riot-stx.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/riot@4/riot+compiler.min.js"></script>
    <script type="riot" src="./my-root-tag.html"></script>
    <script type="riot" src="./tag1.html"></script>
    <script type="riot" src="./tag2.html"></script>

    <my-root-tag></my-root-tag>

    <script type="text/javascript">
     //Initalize some global state properties if you want (not mandatory)
     let initStateObj = { message: 'Hello world'}
     riot.compile().then(() => {
     
        //The magic is here, it setups all listener and create the global 'state' object (named 'stx' in component)
        riotStx.initState(initStateObj)
        // end of magic (-;
        
        riot.mount('my-root-tag')

      // You can update message var from your main code.
      // update will be send to all components that have subscribed to 'message'
      setTimeout(()=>{
            state.message='Another message after 30 seconds from main js'
           },30000)
     }
    </script>
  </body>
</html>
<my-root-tag>
 <h1>{stx.message}</h1>
 <tag1></tag1>
 <tag2></tag2>
 <script>
  export default {
    stx: {
      // by declaring stx.message, this component auto-subscribes to any change on it
      message:null
    }
  }
 </script>
</my-root-tag>
<tag1>
  <h1>{stx.message}</h1>
  <script>
    export default {
      stx: {
        // by declaring stx.message, this component auto-subscribes to any change on it
        message:'default on mount message'
      },
      onMounted(){
       this.stx.message='new message from tag #1'
      },
      onUpdated(){
       //you can even change the var in onUpdated handler !
       setTimeout(()=>{
        this.stx.message='Another message after 20s'
       },20000)
      }
    }
  </script>
</tag1>
<tag2>
  <h1>{stx.message}</h1>
  <p onclick={changMsg}>Click here to change message</p>
  <script>
    export default {
      stx: {
        // by declaring stx.message, this component auto-subscribes to any change on it
        message:'another default on mount message, could be null'
      },
      changMsg(){
        this.stx.message='new message from tag #2'
      }
    }
  </script>
</tag2>