You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A library to manage application state, heavily inspired by mobx-state-tree
Basic example
importmodelfrom'parket';// model returns a "constructor" functionconstPerson=model('Person',{// name is used internally for serializationinitial: ()=>({firstname: null,lastname: null,nested: null,}),actions: state=>({setFirstName(first){state.firstname=first;// no set state, no returns to merge, it's reactive™},setLastName(last){state.lastname=last;},setNested(nested){state.nested=nested;},}),views: state=>({fullname: ()=>`${state.firstname}${state.lastname}`,// views are computed properties}),});// merge an object with the initial stateconstinstance=Person({firstname: 'Tom'});// you can subscribe to actions, patches (state updates) and snapshots (full state after actions)constunsubscribe=instance.onSnapshot(console.log);// you can unsubscribe by calling the function returned by the listener// unsubscribe();instance.setLastName('Clancy');// views turn into cached gettersconsole.log(instance.fullname);// 'Tom Clancy'// nested models also bubble up events to the parentinstance.setNested(Person());instance.nested.setFirstName('wow');// you can get a snapshot of the state at any time// { firstname: 'Tom', lastname: 'Clancy', nested: { firstname: 'wow', lastname: null, nested: null } }console.log(instance.getSnapshot());
The text was updated successfully, but these errors were encountered:
https://github.com/ForsakenHarmony/parket
Basic example
The text was updated successfully, but these errors were encountered: