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

Add useRouter hook for React 16.7+ #6453

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 1 addition & 2 deletions packages/react-router/modules/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class Route extends React.Component {

let { children, component, render } = this.props;

// Preact uses an empty array as children by
// default, so use null if that's the case.
// Preact uses an empty array as children by default, so use null instead.
if (Array.isArray(children) && children.length === 0) {
children = null;
}
Expand Down
34 changes: 34 additions & 0 deletions packages/react-router/modules/__tests__/useRouter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from "react";
import ReactDOM from "react-dom";
import { MemoryRouter, Route, useRouter } from "react-router";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix tests failing :)

Suggested change
import { MemoryRouter, Route, useRouter } from "react-router";
import { MemoryRouter, Route, unstable_useRouter as useRouter } from "react-router";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjackson Is it really that hard to change this and release it? :) Looks like you are deliberately waiting for React 16.7 to come out and stalling :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be patient, @FredyC. Thanks :)


import renderStrict from "./utils/renderStrict";

describe("useRouter", () => {
let node = document.createElement("div");

afterEach(() => {
ReactDOM.unmountComponentAtNode(node);
});

it("provides the <Route> props", () => {
let router;
function TestComponent() {
router = useRouter();
return null;
}

renderStrict(
<MemoryRouter initialEntries={["/one"]}>
<Route path="/:id" component={TestComponent} />
</MemoryRouter>,
node
);

expect(router).toBeDefined();
expect(router.match.url).toBe("/one");
expect(router.match.path).toBe("/:id");
expect(router.match.params).toBeDefined();
expect(router.match.params.id).toBe("one");
});
});
2 changes: 2 additions & 0 deletions packages/react-router/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export { default as generatePath } from "./generatePath";
export { default as matchPath } from "./matchPath";
export { default as withRouter } from "./withRouter";

export { default as unstable_useRouter } from "./useRouter";

export { default as __RouterContext } from "./RouterContext";
20 changes: 20 additions & 0 deletions packages/react-router/modules/useRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { useContext } from "react";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should remove the unused default React symbol import ?

import invariant from "tiny-invariant";

import RouterContext from "./RouterContext";
import matchPath from "./matchPath";

export default function useRouter(options = {}) {
invariant(
mjackson marked this conversation as resolved.
Show resolved Hide resolved
typeof useContext === "function",
"The useRouter hook requires React 16.7 or greater"
);

let context = useContext(RouterContext);
let location = options.location || context.location;
pshrmn marked this conversation as resolved.
Show resolved Hide resolved
let match = options.path
? matchPath(location.pathname, options)
Copy link
Contributor

@edygar edygar Nov 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could useMemo here like:

let match = options.path
  ? useMemo(() => matchPath(location.pathname, options), [
      location.pathname,
      options.path,
      options.exact,
      options.strict
    ])
  : context.match;

So it won't loop through all routes at matchPath on every usage that route remains unchanged after a render.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling that at the moment the useMemo came to the light, people are trying to memoize everything :) Bear in mind, that even such memoization has some overhead and without proper benchmarks, it's just a speculation what would be more performant.

Copy link

@danielkcz danielkcz Nov 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and also a conditional use of hook is a big NO-NO, so I really wouldn't do this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops! I'm really getting used to it.

And you're right, it looks like premature optimization for now.

: context.match;

return { ...context, location, match };
mjackson marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 14 additions & 13 deletions packages/react-router/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"jest": "^23.6.0",
"jest-circus": "^23.6.0",
"raf": "^3.4.0",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react": "^16.7.0-alpha.0",
"react-dom": "^16.7.0-alpha.0",
"rollup": "^0.66.6",
"rollup-plugin-babel": "^4.0.3",
"rollup-plugin-commonjs": "^9.2.0",
Expand Down