koordinates / xstate-tree

Build UIs with Actors using xstate and React
MIT License
85 stars 6 forks source link

Improve action handling #2

Closed UberMouse closed 1 year ago

UberMouse commented 2 years ago

Currently the result of the buildActions builder is cached until one of the following happens

  1. The reference to the send function changes, I don't think this ever happens in practice
  2. The selectors result changes, since the action functions need the most up to date selectors output

And then the selectors can change for the following reasons

  1. Context ref changes
  2. Current state changes

Which means that the actions will change reference whenever the machines context is updated or the state is changed. This is not great for re-renders and makes the DX of using an action inside of a useEffect pretty awful. Can't refer to the action directly and need to use a ref that gets synchronized with the actions current reference.

It seems like a solution to this would be to instead return a stable Proxy reference for actions and have that proxy forward the call to the current actions reference.

UberMouse commented 2 years ago

Though maybe it would be best to instead always return a static actions object and instead use a proxy for the selectors, and pass that to actions so it's always reading the latest selector data via the proxy.

UberMouse commented 1 year ago

Closed by #25