ncthbrt / react-use-elmish

A React hook for unified purely-functional state and effect management
MIT License
57 stars 4 forks source link

Parent-child composition #4

Closed iyegoroff closed 4 years ago

iyegoroff commented 4 years ago

How to implement communication between components like in Elmish? There is no analog to Cmd.map - should I use custom effect creator or there is another approach?

ncthbrt commented 4 years ago

It depends on whether you want to map your dispatch function or not.

If you declare a union: type Action = SubtypeA | SubtypeB in typescript, and your dispatcher is Dispatch<Action>, the dispatcher is also Dispatch<SubtypeA> due to the nature of the type system. So you could have a component declare that it only cares about a subset of actions and have it take in the raw dispatch without mapping.

Alternatively if you want to map your state or dispatch, you could use the useMemo and useCallback hooks to map state and dispatch respectively.

On Mon, 2 Dec 2019, 03:41 iyegoroff, notifications@github.com wrote:

How to implement communication between components like in Elmish? There is no analog to Cmd.map - should I use custom effect creator or there is another approach?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ncthbrt/react-use-elmish/issues/4?email_source=notifications&email_token=ACCJEWI4CS2X7J6NV45PG63QWRRWNA5CNFSM4JTO7AF2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4H5D7LKA, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACCJEWJP2YOV3RA6O5AY2F3QWRRWNANCNFSM4JTO7AFQ .

iyegoroff commented 4 years ago

Thanks for the explanation!