Closed pelotom closed 5 years ago
I went ahead and made a separate library for this: https://github.com/pelotom/use-state-methods
Cool! If you ping me in a tweet I'll RT
On Tue, Mar 26, 2019 at 6:22 AM Tom Crockett notifications@github.com wrote:
I went ahead and made a separate library for this: https://github.com/pelotom/use-state-methods
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mweststrate/use-immer/issues/10#issuecomment-476479764, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvGhKw_Iwb6ArPa3HWMp9grP8wn2if6ks5vaa6KgaJpZM4cCoo2 .
@pelotom nice libray, would it be integrated into use-immer?
Hi, I wanted to share a simple hook I've built on top of
use-immer
that I've found very useful, and ask if there's any interest in including it in the library. I call ituseStateMachine
, and (adapting the example from your README), one uses it like this:The API bears an obvious resemblance to that of
useImmerReducer
/useReducer
, but with some important differences:dispatch
function, one gets back an object of callbacks corresponding to the methodsI find this pattern much nicer to use than standard reducers for a few reasons:
break
/return
from each casearg => dispatch({ type: 'foo', arg })
-- blech!), and they are properly memoized!Action
union type and keep it up to date; everything is inferred from the method typesThe hook is very simple; here's the code for it:
If you think this would make a good addition to the library I'd be happy to open a PR.