Skip to content

Typescript, Actor and useActor problem #1534

Answered by davidkpiano
RafalFilipek asked this question in Q&A
Discussion options

You must be logged in to vote

This PR would address/fix this issue: #1622

Notably, it:

  • loosens the ActorRef<TEvent, TEmitted> type to be compatible with the v4 Actor<...> types that spawn(...) returns
  • introduces ActorRefFrom<SomeMachine> to make it easier to type spawned machines:
const childMachine = createMachine(...);

interface SomeContext {
  machineRef?: ActorRefFrom<typeof childMachine>
}

const parentMachine = createMachine<SomeContext>({
  // ...
  entry: assign({
    machineRef: (ctx, e) => spawn(childMachine)
  })
});

// ... in a component

const ChildComponent: React.FC<{ child: ActorRefFrom<typeof childMachine> }> = ({ child }) => {
  const [state, send] = useActor(child);

  // ...
}

Replies: 5 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@davidkpiano
Comment options

@GeospatialMax
Comment options

Comment options

You must be logged in to vote
1 reply
@Andarist
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by RafalFilipek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants