Skip to content

Commit

Permalink
Make sure Switch always controls its child routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Jul 8, 2019
1 parent 0edbbd5 commit 2fc7b25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions index.js
Expand Up @@ -67,22 +67,19 @@ export const Router = props => {
});
};

export const Route = props => {
const [matches, params] = useRoute(props.path);
export const Route = ({ path, match, component, children }) => {
const useRouteMatch = useRoute(path);

if (!matches && !props.match) {
return null;
}
// `props.match` is present - Route is controlled by the Switch
const [matches, params] = match || useRouteMatch;

if (!matches) return null;

// React-Router style `component` prop
if (props.component) {
return h(props.component, { params: params });
}
if (component) return h(component, { params: params });

// support render prop or plain children
return typeof props.children === "function"
? props.children(params)
: props.children;
return typeof children === "function" ? children(params) : children;
};

export const Link = props => {
Expand Down Expand Up @@ -118,16 +115,18 @@ export const Switch = ({ children, location }) => {
children = children && children.length ? children : [children];

for (const element of children) {
let match = 0;

if (
isValidElement(element) &&
// we don't require an element to be of type Route,
// but we do require it to contain a truthy `path` prop.
// this allows to use different components that wrap Route
// inside of a switch, for example <AnimatedRoute />.
element.props.path &&
matcher(element.props.path, location || originalLocation)[0]
(match = matcher(element.props.path, location || originalLocation))[0]
)
return cloneElement(element, { match: true });
return cloneElement(element, { match });
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion test/switch.test.js
Expand Up @@ -88,7 +88,7 @@ it("allows to specify which routes to render via `location` prop", () => {
expect(rendered[0].type).toBe(Route);
});

xit("always ensures the consistency of inner routes rendering", done => {
it("always ensures the consistency of inner routes rendering", done => {
history.replaceState(0, 0, "/foo/bar");

const { unmount } = render(
Expand Down

0 comments on commit 2fc7b25

Please sign in to comment.