unihooks
unihooks copied to clipboard
Upgrade useStore to useReducer-like API?
Too often it is more useful to have in-place reducers/modifiers, rather than abstract actions. Something like
let [store, { set, load, ... }] = useStore(initialState, actions)
But then actions may need to be rather reducers-like.
const devices = createStore('devices', {
items: [],
id: {},
loading: false
}, {
load: function * () {
yield ({id: {}, loading: true, items: []})
let result = await api.get(`/devices`)
yield { items: result.payload, loading: false }
},
current: null
})
That creates following pattern, considering flowponents example as well. Reactive generators can be applied to any objects as reducers - to DOM-objects as morphdom reducers, to store as store reducers, etc.
let devices = {}
createReducer(devices, devices => {
yield {...devices, a,b,c}
await ...load()
yield {...devices, d,e,f}
})
That's not far from just action though.