Skip to content

Commit 449a528

Browse files
timdorrmjackson
authored andcommitted
Use new Context API in React 16.3 (#5908)
* Use the new 16.3.0 context API. * Export so others can use it. * Only children again. * Better default value * Make Prompt use the new API * Make Redirect use the new context API * Export RouterContext in rrd * Use RouterContext in Link * Make StaticRouter inheritence work * Directly consume context in Link. * Update to final component-based context API * Use create-react-context for BC * Revert back to 16.2 to check BC and sync up with RRN. * Update create-react-context and let it pass through. * Fix packaging/build issues. * im -> ex * Downgrade babel-plugin-transform-react-remove-prop-types 0.4.16 has a bug. See oliviertassinari/babel-plugin-transform-react-remove-prop-types#160
1 parent 505925d commit 449a528

25 files changed

+12858
-986
lines changed

packages/react-router-config/package-lock.json

+159-149
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-router-config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"babel-jest": "^23.0.1",
3636
"babel-plugin-dev-expression": "^0.2.1",
3737
"babel-plugin-external-helpers": "^6.22.0",
38-
"babel-plugin-transform-react-remove-prop-types": "^0.4.13",
38+
"babel-plugin-transform-react-remove-prop-types": "0.4.15",
3939
"babel-preset-es2015": "^6.14.0",
4040
"babel-preset-react": "^6.5.0",
4141
"babel-preset-stage-1": "^6.5.0",

packages/react-router-dom/modules/Link.js

+23-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import PropTypes from "prop-types";
33
import invariant from "invariant";
44
import { createLocation } from "history";
5+
import RouterContext from "./RouterContext";
56

67
const isModifiedEvent = event =>
78
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
@@ -22,17 +23,7 @@ class Link extends React.Component {
2223
replace: false
2324
};
2425

25-
static contextTypes = {
26-
router: PropTypes.shape({
27-
history: PropTypes.shape({
28-
push: PropTypes.func.isRequired,
29-
replace: PropTypes.func.isRequired,
30-
createHref: PropTypes.func.isRequired
31-
}).isRequired
32-
}).isRequired
33-
};
34-
35-
handleClick = event => {
26+
handleClick = history => event => {
3627
if (this.props.onClick) this.props.onClick(event);
3728

3829
if (
@@ -43,7 +34,6 @@ class Link extends React.Component {
4334
) {
4435
event.preventDefault();
4536

46-
const { history } = this.context.router;
4737
const { replace, to } = this.props;
4838

4939
if (replace) {
@@ -57,22 +47,30 @@ class Link extends React.Component {
5747
render() {
5848
const { replace, to, innerRef, ...props } = this.props; // eslint-disable-line no-unused-vars
5949

60-
invariant(
61-
this.context.router,
62-
"You should not use <Link> outside a <Router>"
63-
);
64-
6550
invariant(to !== undefined, 'You must specify the "to" property');
6651

67-
const { history } = this.context.router;
68-
const location =
69-
typeof to === "string"
70-
? createLocation(to, null, null, history.location)
71-
: to;
72-
73-
const href = history.createHref(location);
7452
return (
75-
<a {...props} onClick={this.handleClick} href={href} ref={innerRef} />
53+
<RouterContext.Consumer>
54+
{({ router }) => {
55+
invariant(router, "You should not use <Link> outside a <Router>");
56+
57+
const { history } = router;
58+
const location =
59+
typeof to === "string"
60+
? createLocation(to, null, null, history.location)
61+
: to;
62+
63+
const href = history.createHref(location);
64+
return (
65+
<a
66+
{...props}
67+
onClick={this.handleClick(history)}
68+
href={href}
69+
ref={innerRef}
70+
/>
71+
);
72+
}}
73+
</RouterContext.Consumer>
7674
);
7775
}
7876
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Written in this round about way for babel-transform-imports
2+
import { RouterContext } from "react-router";
3+
export default RouterContext;

packages/react-router-dom/modules/__tests__/Link-test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ describe("A <Link>", () => {
5151
ReactDOM.render(<Link to="/">link</Link>, node);
5252
}).toThrow(/You should not use <Link> outside a <Router>/);
5353

54-
expect(console.error.calls.count()).toBe(3);
55-
expect(console.error.calls.argsFor(0)[0]).toContain(
56-
"The context `router` is marked as required in `Link`"
57-
);
54+
expect(console.error.calls.count()).toBe(2);
5855
});
5956

6057
it('throws with no "to" prop', () => {

packages/react-router-dom/modules/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export Prompt from "./Prompt";
77
export Redirect from "./Redirect";
88
export Route from "./Route";
99
export Router from "./Router";
10+
export RouterContext from "./RouterContext";
1011
export StaticRouter from "./StaticRouter";
1112
export Switch from "./Switch";
1213
export generatePath from "./generatePath";

0 commit comments

Comments
 (0)