Another action based state management tool
npm i ractorRactor builds upon three main ideas.
System is an event system. Normally, You need to create one for your logic app.
import{System}from"ractor";exportconstsystem=newSystem("counter");The abstraction of system behavior. You can create it by class.
exportclassIncrement{}Conceptually, your can think of Store like a event handler or redux’s reducer.
Store is an abstract class, abstract method createReceive needs to return an Receive object. There is a convenient helper method receiveBuilder to help you to create Receive object
import{Increment}from"./Increment";import{Decrement}from"./Decrement";import{Store}from"ractor";exportclassCounterStoreextendsStore<{value: number}>{publicstate={value: 1};publiccreateReceive(){returnthis.receiveBuilder().match(Increment,async()=>this.setState({value: this.state.value+1})).match(Decrement,async()=>this.setState({value: this.state.value+1})).build();}}There is a convenient function createStore to createStore quickly.
import{ReceiveBuilder}from"ractor"constCounterStore=createStore((getState,setState)=>{returnReceiveBuilder.create().match(Increment,()=>setState(getState()+1)).match(Decrement,()=>setState(getState()-1)).build()},0)There is the quick glance of ractor-hooks
Create an event system for your App.
import{Provider}from"ractor-hooks"import{System}from"ractor"functionApp(){return<Providersystem={newSystem("app")}><Counter/></Provider>}Inject Store to your component, return the state of the store which had injected.
functionCounter(){constcounter=useStore(CounterStore)returnjsx}Take the system out of current context.
functionCounter(){constsystem=useSystem(CounterStore)return<buttononClick={()=>system.dispatch(newIncrement)}>+</button>}