import{RiotMeiosis}from'@riot-tools/meiosis';// can be an objectconststate={initial: true,isNew: true,mutable: false,nested: {hasPasta: true}};// can be a mapconststate=newMap([['init',true],['mutable',true],['nested',{hasPasta: true}]])// merge the states when objectconstreducer=(newState: object,oldState: object)=>({
...oldState,
...newState});// merge the states when mapconstreducer=(newState: object,oldState: Map)=>{for(const[key,value]ofObject.entries(newState)){oldState.set(key,value);}returnoldState;}conststateManager=newRiotMeiosis(state);// Extract the state streamconst{ stream }=stateManager;// Add your state reducerstream.addReducer(reducer);// send updatedstream.dispatch({initial: false,isNew: false});// Gets an immutable clone of the current stateconsole.log(stream.state());// > {// initial: false,// isNew: false,// mutable: false,// nested: {// hasPasta: true// }// }exportdefaultstateManager;
mycomponent.riot
<myComponent><pif={hasPasta}>I have pasta!</p><script>import{connect}from'./appState';importmyActionsfrom'./actions';constmapToState=(appState,ownState,ownProps)=>({
...ownState,
...appState.nested});// Optional mapping of functions or objects to componentconstmapToComponent=myActions;// ORconstmapToComponent=(ownProps,ownState)=>myActions;constcomponent={onBeforeMount(){// connect will respect original onBeforeMountthis.state={lala: true}},onMounted(){// Component will have access to dispatch from lexical thisthis.dispatch({myComponentMounted: true});}}exportdefaultconnect(mapToState)(component);// ORexportdefaultconnect(mapToState,mapToComponent)(component);</script></myComponent>