import { TwoAndEight, createReactStore } from '2n8' class Store extends TwoAndEight { count = 0 addClicked() { this.count++ } resetClicked() { this.$reset('count') }} const useStore = createReactStore(new Store()) const Counter = () => { const count = useStore((s) => s.count) const addClicked = useStore((s) => s.addClicked) const resetClicked = useStore((s) => s.resetClicked) return ( <div> <span>{count}</span> <button onClick={addClicked}>One up</button> <button onClick={resetClicked}>Reset</button> </div> )}
import { TwoAndEight, createReactStore } from '2n8' class Store extends TwoAndEight { status: 'idle' | 'pending' | 'success' = 'idle' async buttonClicked() { this.status = 'pending' this.$commit() await fetchData() this.status = 'success' } resetClicked() { this.$reset('status') }} const useStore = createReactStore(new Store()) const Async = () => { const status = useStore((s) => s.status) const buttonClicked = useStore((s) => s.buttonClicked) const resetClicked = useStore((s) => s.resetClicked) return ( <div> <button onClick={buttonClicked}> {status === 'idle' ? ( <DownArrowIcon /> ) : status === 'pending' ? ( <SpinnerIcon /> ) : ( <DoneIcon /> )} </button> <button onClick={resetClicked}>Reset</button> </div> )}