Skip to content

Commit

Permalink
Add simple Redirect component.
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Apr 26, 2019
1 parent 00fd81b commit 8a4f4fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,11 @@ export const Switch = ({ children, location }) => {
return element ? cloneElement(element, { match: true }) : null;
};

export const Redirect = props => {
const router = useRouter();
useEffect(() => router.history.push(prop.href || props.to));

return null;
};

export default useRoute;
9 changes: 9 additions & 0 deletions test/redirect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";
import TestRenderer from "react-test-renderer";

import { Redirect } from "../index.js";

it("renders nothing", () => {
const rendered = TestRenderer.create(<Redirect to="/users" />).root;
expect(rendered.children.length).toBe(0);
});

0 comments on commit 8a4f4fc

Please sign in to comment.