-
Context I have to make multiple machines to pass Question Then, how can I make multiple machines in parent component by iteration. Pseudo Code const ParentComponent = () => {
const [,send, service] = useMachine(someMachine); // This machine should be made as multiple instances
// For example
// const machines = Array(10).fill(0).map(_ => useMachine(someMachine))
service.start();
const children = [];
return (
<div>
{children.map((_, idx) => {
return <ChildComponent service={service} key={idx} />
}}
</div>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This can't be done easily due to the rules of hooks. It's advised to move a hook into your 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>
);
} |
Beta Was this translation helpful? Give feedback.
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: