Skip to content

Commit 369fe84

Browse files
committed
fix(builders): viewToMachine not accepting Root components
This used to work but the change that introduced allowing passing children to Root components broke it Relaxed the typings of viewToMachine to allow any component with any props
1 parent 10183d9 commit 369fe84

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/builders.spec.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ describe("xstate-tree builders", () => {
1717

1818
await waitFor(() => getByText("hello world"));
1919
});
20+
21+
it("works for Root components", async () => {
22+
const ViewMachine = viewToMachine(() => <div>hello world</div>);
23+
const Root = buildRootComponent({ machine: ViewMachine });
24+
const RootMachine = viewToMachine(Root);
25+
const RootView = buildRootComponent({ machine: RootMachine });
26+
27+
const { getByText } = render(<RootView />);
28+
29+
await waitFor(() => getByText("hello world"));
30+
});
2031
});
2132

2233
describe("buildRoutingMachine", () => {

src/builders.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ export function createXStateTreeMachine<
274274
* @param view - the React view you want to invoke in an xstate machine
275275
* @returns The view wrapped into an xstate-tree machine, ready to be invoked by other xstate machines or used with `buildRootComponent`
276276
*/
277-
export function viewToMachine(view: () => JSX.Element): AnyXstateTreeMachine {
277+
export function viewToMachine(
278+
view: (args?: any) => JSX.Element
279+
): AnyXstateTreeMachine {
278280
return createXStateTreeMachine(
279281
createMachine({
280282
initial: "idle",

xstate-tree.api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export type ViewProps<TSelectors, TActions, TSlots extends readonly Slot[], TMat
446446
};
447447

448448
// @public
449-
export function viewToMachine(view: () => JSX.Element): AnyXstateTreeMachine;
449+
export function viewToMachine(view: (args?: any) => JSX.Element): AnyXstateTreeMachine;
450450

451451
// @public (undocumented)
452452
export type XstateTreeHistory<T = unknown> = History_2<{

0 commit comments

Comments
 (0)