Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Basic Example of Providing actor with Input & Snapshot to react component tree#5389

Unanswered
zacharyhansen asked this question inQ&A
Discussion options

Hi all! I think I must be missing something obvious but I cannot figure out how to properly provide an actor I create in a react application to my component tree when it depends on input / persistent snapshots.

The examples here go into detail around how to provide a machine that doesnt depend on dynamic datahttps://stately.ai/docs/xstate-react#createactorcontextlogic but that is not useful if we are trying to start the actor with stored snapshots or input that is dynamic to the component (i.e. a specific route or other API driven information).

Examples like the following always reference the actor directly in the same component which works but is cumbersome in complex component trees.
https://codesandbox.io/p/sandbox/github/statelyai/xstate/tree/main/examples/tiles
https://codesandbox.io/p/sandbox/github/statelyai/xstate/tree/main/examples/todomvc-react

What I think would be ideal / really powerful is an example of how to provide my actor to my component tree when it depends on the component driven snapshot / input. Sort of like so

constExisitingConnectFlow=({portfolioConnectItem,}:{portfolioConnectItem:PortfolioConnectItemFragment;})=>{const[saveSnapshot]=useUpdatePortfolioConnectSnapshotMutation();constactor=createActor(machine,{input:{portfolioConnect:portfolioConnectItem},snapshot:portfolioConnectItem.persistedSnapshot,});return(<div><ConnectMachineContext.Providerlogic={}// <--- only takes a machine logic? cant provide actual actor>// ...component tree that will pull from context to reference the actor</ConnectMachineContext.Provider></div>);};

Would the recommendation be to have my own provider to hold the actor using types viahttps://stately.ai/docs/typescript#actorreffromt?

You must be logged in to vote

Replies: 1 comment

Comment options

The provider supports anoptions prop for that:

<ConnectMachineContext.Provideroptions={{input:{portfolioConnect:portfolioConnectItem},snapshot:portfolioConnectItem.persistedSnapshot}}>  ...</ConnectMachineContext.Provider>
You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@zacharyhansen@rothsandro

[8]ページ先頭

©2009-2025 Movatter.jp