dy / unihooks

Universal unreacted hooks
MIT License
21 stars 1 forks source link

Upgrade useStore to useReducer-like API? #17

Open dy opened 4 years ago

dy commented 4 years ago

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.