Skip to content

How can I make multiple machines by hook? #1578

Answered by Andarist
baeharam asked this question in Q&A
Discussion options

You must be logged in to vote

This can't be done easily due to the rules of hooks. It's advised to move a hook into your ChildComponent in a case like this (like you have mentioned).

You could also create a single machine in your parent component and spawn multiple children - like:

const parentMachine = {
  entry: assign(() => ({
    children: Array(10).fill(0).map(_ => spawn(someMachine))
  }))
}

const ParentComponent = () => {
  const [state, send, service] = useMachine(parentMachine); 

  return (
    <div>
    {state.context.children.map((child, idx) => {
      return <ChildComponent service={child} key={idx} />
     }}
    </div>
  );
}

Replies: 1 comment 1 reply

Comment options

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

Answer selected by baeharam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants