Xstate: Using actors & useMachine with StrictMode

Created on 21 Jun 2019  路  2Comments  路  Source: davidkpiano/xstate

Bug or feature request?

Bug

Description:

When using actors & useMachine with StrictMode React runs twice which might be causing to have 2 instances of the machine. I think it's causing the actor to lose the right reference to the parent machine.

(Bug) Expected result:

Should work fine with StrictMode.

(Bug) Actual result:

Warning: Event "fetch_SUCCESS" was sent to uninitialized service "fetchMachine" and is deferred. Make sure .start() is called for this service.

(Bug) Potential fix:

Don't know what the potential fix is since I'm not certain what is causing the reference to be lost.

I also strongly encourage you to create a pull request if you are confident in the potential fix.

Link to reproduction or proof-of-concept:

https://codesandbox.io/s/great-haibt-ezdz5

If you remove StrictMode it works fine.

help wanted 鈿涳笌 @xstatreact

Most helpful comment

Ok, so we deal with 2 different reports here.

@Jpadilla1 this is a combination of:

  • the fact that actors in the initial state are currently spawned at instantiation time rather than when .start() gets called. This is going to change in the future - https://github.com/davidkpiano/xstate/issues/534#issuecomment-510888732
  • initialState gets calculation gets cached for a machine and this unfortunately puts a stale parent reference in the spawned actor (in the initial state), because it's the one from first invocation rather than the current one and because of the StrictMode the interpreter gets called twice with the same machine

@ReasonableDeveloper this is actually expected - when a machine reaches its final state it's considered to be "done" as per - https://www.w3.org/TR/scxml/#final :

When the state machine reaches the child of an element, it must terminate.

All 2 comments

I don't think this has to do with StrictMode. I'm getting the same warning without StrictMode in my code. Fork of your sandbox: https://codesandbox.io/s/hopeful-night-1c7so

Ok, so we deal with 2 different reports here.

@Jpadilla1 this is a combination of:

  • the fact that actors in the initial state are currently spawned at instantiation time rather than when .start() gets called. This is going to change in the future - https://github.com/davidkpiano/xstate/issues/534#issuecomment-510888732
  • initialState gets calculation gets cached for a machine and this unfortunately puts a stale parent reference in the spawned actor (in the initial state), because it's the one from first invocation rather than the current one and because of the StrictMode the interpreter gets called twice with the same machine

@ReasonableDeveloper this is actually expected - when a machine reaches its final state it's considered to be "done" as per - https://www.w3.org/TR/scxml/#final :

When the state machine reaches the child of an element, it must terminate.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hnordt picture hnordt  路  3Comments

suku-h picture suku-h  路  3Comments

carloslfu picture carloslfu  路  3Comments

drmikecrowe picture drmikecrowe  路  3Comments

hnordt picture hnordt  路  3Comments