#v5 useSelector with matches. Crash problem in code base.

1 messages · Page 1 of 1 (latest)

civic dagger
#

I can't upgrade above beta-41 in our react-native code base (which uses actor model with spawn). I have tried digging into the problem, but I keep getting the error

TypeError: Cannot read property 'value' of undefined

somewhere inside the matches function in XState, that I use via:

export const MainActor = createActorContext(mainMachine);

// ...
  const inSomeState = MainActor.useSelector(({ matches }) => matches('SomeState'));

If I upgrade to beta-42 or above our whole code base will crash with this error in runtime the first time any matches function is used like above.

What I find is that in XState function machineSnapshotMatches, this is undefined and therefore this.value crashes. It is hard to see in the distributed XState code (raise-746abff7.cjs.js) how this can ever be defined there.
Anyhow, that function uses matchesState, which is exported from XState. So my workaround is to replace all uses of matches with machesState.

I understand that it's hard to say much with the above info, it doesn't seem anyone else has run into this problem. But any ideas at all?

nova frigate
#

cc. @half marsh

I think it's not currently possible to detach matches like that. Nor should you IMO unless you have a good reason

half marsh
#

yes, correct this is required there right now (so dont detach this method from the snapshot), it's already useful for our internal purposes and it might become even more useful in the future when we manage to get some kind of typestates in

civic dagger
#

Oh... ok

#

I'm too used to functional style so I didn't even reflect on the difference with dot-chaining vs destructuring.

civic dagger
#

It's a bit sad I think, because I like to make abstractions like:

  const selected = useInChildActor('addressForm', 'context.selected');

and everything else is possible to access and use via that custom hook, except:

  const matches = useInChildActor('addressForm', 'matches');
  const isOk = matches('FormOk');

@half marsh

half marsh