Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useParams): fix issue with useParams reading from null object #6940

Merged
merged 2 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/react-router/modules/__tests__/useParams-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,25 @@ describe("useParams", () => {
});
});
});

describe("when the route isn't matched", () => {
it("returns empty object", () => {
let params;

function HomePage() {
params = useParams();
return null;
}

renderStrict(
<MemoryRouter initialEntries={["/home"]}>
<Route path="/not-the-current-route" children={() => <HomePage />} />
</MemoryRouter>,
node
);

expect(typeof params).toBe("object");
expect(Object.keys(params)).toHaveLength(0);
});
});
});
3 changes: 2 additions & 1 deletion packages/react-router/modules/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function useParams() {
);
}

return useContext(Context).match.params;
const match = useContext(Context).match;
return match ? match.params : {};
}

export function useRouteMatch(path) {
Expand Down