Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useService logs warning about uninitialized service when service is passed to child component #1132

Closed
kevinold opened this issue Apr 16, 2020 · 13 comments

Comments

@kevinold
Copy link

kevinold commented Apr 16, 2020

Description
In v4.7.0-4.9.0 a warning is logged to the console when calling useService on a service prop.

Expected Result
No warning.

Actual Result
Warning: Attempted to read initial state from uninitialized service 'toggle'. Make sure the service is started first.

Reproduction
https://codesandbox.io/s/xstate-react-template-uninitialized-service-warning-cz351

Additional context
Note that the machine is created at the top of the application (App) and passed to a child (Child).

@kevinold kevinold added the bug label Apr 16, 2020
@davidkpiano
Copy link
Member

This behavior likely occurred in 4.8.x as well, can you check there?

@kevinold
Copy link
Author

@davidkpiano Yes, checked all the way back to 4.7.0 where this issue begins. It was not present 4.6.7 and 4.7.0 RC's throw errors.

@seloner
Copy link

seloner commented May 19, 2020

@davidkpiano Is any work around for that ? I am trying to put the service in React Context and consume it after but I am getting the same error .

@davidkpiano
Copy link
Member

Try updating to the latest xstate and @xstate/react@next; the warning is no longer there.

@seloner
Copy link

seloner commented May 19, 2020

@davidkpiano I am on "@xstate/react": "^0.8.1", and "xstate": "^4.9.1" and I am still getting the warning . I am getting the Interpreter  from context but the useService is returning undefined so I can not use the machine .

@davidkpiano
Copy link
Member

Does the app otherwise work fine? If so, you can ignore the warning for now.

@seloner
Copy link

seloner commented May 20, 2020

@davidkpiano
It is not wokring. I am getting undefined in the useService Hook and not only on mount.It does not change.
image
image

My workaround is to pass into React context, [state,send] from UseMachine instead of the service . I can provide a code sandbox if you want.

@seloner
Copy link

seloner commented May 21, 2020

@davidkpiano in @xstate/react/v/1.0.0-rc.4 I have no problem . Is it safe to use the next version for production ?

@seloner
Copy link

seloner commented May 25, 2020

@davidkpiano
In versions after 1.0.0-rc.1 when you try to put initial context in the Machine like the example in docs
https://xstate.js.org/docs/guides/context.html#initial-context I am getting this warning

Machine given to useMachine has changed between renders. This is not supported and might lead to unexpected results.
Please make sure that you pass the same Machine as argument each time.

In versions before 1.0.0-rc.2 I am getting the service warning .

Sandbox : https://codesandbox.io/s/great-herschel-9opwh?file=/src/App.js

I guess its just a warning a not a bug ?

@davidkpiano
Copy link
Member

@seloner Prefer this:

  const [state, send] = useMachine(counterMachine, {
    context: someContext // initial context
  });

@Andarist
Copy link
Member

@seloner the problem is that useMachine can only ever work with the same instance of a machine. Machines need to have static config - and it's not possible to change this for a particular useMachine call. The warning exists to avoid confusion that you could actually provide a different argument there - and as you are calling withContext each render it creates a new Machine each render and we can't recognize them being the same between renders so it's up to you to memoize it correctly or use @davidkpiano's suggestion

@seloner
Copy link

seloner commented May 27, 2020

@Andarist I want to persist state also. I am getting no warning ,I guess its ok to do it like that ?
image

@davidkpiano
Copy link
Member

This is now fixed in the latest @xstate/react RC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants