statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.21k stars 1.26k forks source link

Bug: @xstate/react system.get("actorSystemID") returns undefined on <StrictMode> #5074

Open bsunderhus opened 2 months ago

bsunderhus commented 2 months ago

XState version

XState version 5

Description

Here's a repro: https://stackblitz.com/edit/github-4nrvzx?file=src%2FfeedbackMachine.ts

On <StrictMode> the re-render is causing the reference returned by system.get to be undefined on the body of a fromCallback logic function:

image

image

Expected result

On <StrictMode> , system.get('feedback') should consistently return a reference to the feedbackActor instead of undefined

Actual result

If the machine spawning the reference is not the machine being accessed by system.get.

In this scenario I got 3 actors:

  1. root (machine)
  2. alert (fromCallback)
  3. feedback (machine)

If root spawns both actors then whenever alert function callback is called the second time (due to <StrictMode> re-rendering) then system.get('feedback') will return undefined ❌, while system.get('root') returns the actor reference ✅.

If root spawns feedback and feedback spawns alert then the problem is solved. ✅

Reproduction

https://stackblitz.com/edit/github-4nrvzx?file=src%2FfeedbackMachine.ts

Additional context

No response