Skip to content

Bug: Snapshot type for useMachine second argument is wrong #5480

@mkarajohn

Description

@mkarajohn

XState version

XState version 5

Description

The docs say that you can reuse a previous snapshot.toJSON(), to rehydrate a newly created machine: https://stately.ai/docs/xstate-react#persisted-and-rehydrated-state

However, when doing that, we get typescript errors regarding the type of the snapshot:

Image

Is there something that can be done about this in userland?

IMO, there should be an easier way to simply resume a machine from a specific state, by simply passing the state key as a string or something. Having to pass a whole rehydrated snapshot should be for special cases.

The docs could also be clearer on this. For example, what is persistedState? Show the data structure. The docs say that the 2nd argument of useMachine is "actor options" but there is no link to the actor options. The docs are chaotic/overwhelming already, don't have the user search for what is an "actor option" config.

Expected result

To not get type errors

Actual result

Type errors when reusing a rehydrated previous snapshot

Reproduction

https://codesandbox.io/p/devbox/priceless-sid-69dmhf?file=%2Fsrc%2FApp.tsx%3A15%2C22

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions