Skip to content

Commit

Permalink
fix(react): react component can be now passed as an action
Browse files Browse the repository at this point in the history
Now passing a React component instead of a simple function as an action won't cause an error.

fix #5
  • Loading branch information
uhyo committed Feb 11, 2021
1 parent e125a6f commit 68ff9fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/react/hooks/useRoutes.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from "react";
import React, { useMemo, useState } from "react";
import {
isLocationNotFoundError,
LocationNotFoundError,
Expand Down Expand Up @@ -203,4 +203,25 @@ describe("useRoutes", () => {
expect(screen.queryByText("fugafuga")).toBeInTheDocument();
});
});
describe("React Component as action", () => {
it("1", () => {
const FooComponent = () => {
const loc = useState("foo");
return <p>I am {loc}</p>;
};
const location = {
pathname: "/foo",
state: null,
};
const routes = Path()
.route("foo", (foo) => foo.action(FooComponent))
.route("bar", (bar) => bar.action(() => <div>I AM BAR</div>));
const Component: React.FC = () => {
return useRoutes(routes);
};

renderInLocation(location, <Component />);
expect(screen.queryByText("I am foo")).toBeInTheDocument();
});
});
});
6 changes: 5 additions & 1 deletion src/react/hooks/useRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export const useRoutes = (builder: ReactRouteBuilder): ReactElement | null => {
},
});

const result = resolved.route.action(resolved.match as never);
// const result = resolved.route.action(resolved.match as never);
const result = React.createElement(
resolved.route.action,
resolved.match as never
);
const routeContextValue = {
route: resolved.route,
ancestorRoutes,
Expand Down

0 comments on commit 68ff9fc

Please sign in to comment.