Skip to content

Commit f0328c3

Browse files
committed
Update to final component-based context API
1 parent 15e39d8 commit f0328c3

File tree

8 files changed

+59
-43
lines changed

8 files changed

+59
-43
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,29 @@ class Link extends React.Component {
4949

5050
invariant(to !== undefined, 'You must specify the "to" property');
5151

52-
return RouterContext.consume(({ router }) => {
53-
invariant(router, "You should not use <Link> outside a <Router>");
52+
return (
53+
<RouterContext.Consumer>
54+
{({ router }) => {
55+
invariant(router, "You should not use <Link> outside a <Router>");
5456

55-
const { history } = router;
56-
const location =
57-
typeof to === "string"
58-
? createLocation(to, null, null, history.location)
59-
: to;
57+
const { history } = router;
58+
const location =
59+
typeof to === "string"
60+
? createLocation(to, null, null, history.location)
61+
: to;
6062

61-
const href = history.createHref(location);
62-
return (
63-
<a
64-
{...props}
65-
onClick={this.handleClick(history)}
66-
href={href}
67-
ref={innerRef}
68-
/>
69-
);
70-
});
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>
74+
);
7175
}
7276
}
7377

packages/react-router/modules/Prompt.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ class InnerPrompt extends React.Component {
6262
}
6363
}
6464

65-
const Prompt = props =>
66-
RouterContext.consume(({ router }) => (
67-
<InnerPrompt {...props} router={router} />
68-
));
65+
const Prompt = props => (
66+
<RouterContext.Consumer>
67+
{({ router }) => <InnerPrompt {...props} router={router} />}
68+
</RouterContext.Consumer>
69+
);
6970

7071
export default Prompt;

packages/react-router/modules/Redirect.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ class InnerRedirect extends React.Component {
9393
}
9494
}
9595

96-
const Redirect = props =>
97-
RouterContext.consume(({ router }) => (
98-
<InnerRedirect {...props} router={router} />
99-
));
96+
const Redirect = props => (
97+
<RouterContext.Consumer>
98+
{({ router }) => <InnerRedirect {...props} router={router} />}
99+
</RouterContext.Consumer>
100+
);
100101

101102
export default Redirect;

packages/react-router/modules/Route.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,18 @@ class InnerRoute extends React.Component {
135135
}
136136

137137
render() {
138-
return RouterContext.provide(this.getChildContext(), this.renderChildren());
138+
return (
139+
<RouterContext.Provider value={this.getChildContext()}>
140+
{this.renderChildren()}
141+
</RouterContext.Provider>
142+
);
139143
}
140144
}
141145

142-
const Route = props =>
143-
RouterContext.consume(({ router }) => (
144-
<InnerRoute {...props} router={router} />
145-
));
146+
const Route = props => (
147+
<RouterContext.Consumer>
148+
{({ router }) => <InnerRoute {...props} router={router} />}
149+
</RouterContext.Consumer>
150+
);
146151

147152
export default Route;

packages/react-router/modules/Router.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class Router extends React.Component {
7575

7676
render() {
7777
const { children } = this.props;
78-
return RouterContext.provide(
79-
this.getChildContext(),
80-
children ? React.Children.only(children) : null
78+
return (
79+
<RouterContext.Provider value={this.getChildContext()}>
80+
{children ? React.Children.only(children) : null}
81+
</RouterContext.Provider>
8182
);
8283
}
8384
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
22

3-
const RouterContext = React.unstable_createContext({});
3+
const RouterContext = React.createContext({});
44

55
export default RouterContext;

packages/react-router/modules/Switch.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ class InnerSwitch extends React.Component {
6868
}
6969
}
7070

71-
const Switch = props =>
72-
RouterContext.consume(({ router }) => (
73-
<InnerSwitch {...props} router={router} />
74-
));
71+
const Switch = props => (
72+
<RouterContext.Consumer>
73+
{({ router }) => <InnerSwitch {...props} router={router} />}
74+
</RouterContext.Consumer>
75+
);
7576

7677
export default Switch;

packages/react-router/modules/__tests__/Router-test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ describe("A <Router>", () => {
5151

5252
describe("context", () => {
5353
let rootContext;
54-
const ContextChecker = () =>
55-
RouterContext.consume(context => {
56-
rootContext = context;
57-
return null;
58-
});
54+
const ContextChecker = () => (
55+
<RouterContext.Consumer>
56+
{context => {
57+
rootContext = context;
58+
return null;
59+
}}
60+
</RouterContext.Consumer>
61+
);
5962

6063
afterEach(() => {
6164
rootContext = undefined;

0 commit comments

Comments
 (0)