Skip to content

Commit fc53733

Browse files
authored
Separate history object to its own context (#7103)
1 parent 973cf52 commit fc53733

File tree

8 files changed

+40
-18
lines changed

8 files changed

+40
-18
lines changed

packages/react-router-native/BackButton.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import { BackHandler } from "react-native";
33

4-
import { __RouterContext as RouterContext } from "react-router";
4+
import { __HistoryContext as HistoryContext } from "react-router";
55

66
class BackButton extends React.PureComponent {
77
handleBack = () => {
@@ -23,12 +23,12 @@ class BackButton extends React.PureComponent {
2323

2424
render() {
2525
return (
26-
<RouterContext.Consumer>
27-
{context => {
28-
this.history = context.history;
26+
<HistoryContext.Consumer>
27+
{history => {
28+
this.history = history;
2929
return this.props.children || null;
3030
}}
31-
</RouterContext.Consumer>
31+
</HistoryContext.Consumer>
3232
);
3333
}
3434
}

packages/react-router-native/DeepLinking.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import { Linking } from "react-native";
33

4-
import { __RouterContext as RouterContext } from "react-router";
4+
import { __HistoryContext as HistoryContext } from "react-router";
55

66
const protocolAndSlashes = /.*?:\/\//g;
77

@@ -27,12 +27,12 @@ class DeepLinking extends React.PureComponent {
2727

2828
render() {
2929
return (
30-
<RouterContext.Consumer>
31-
{context => {
32-
this.history = context.history;
30+
<HistoryContext.Consumer>
31+
{history => {
32+
this.history = history;
3333
return this.props.children || null;
3434
}}
35-
</RouterContext.Consumer>
35+
</HistoryContext.Consumer>
3636
);
3737
}
3838
}

packages/react-router-native/Link.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import { TouchableHighlight } from "react-native";
33
import PropTypes from "prop-types";
44

5-
import { __RouterContext as RouterContext } from "react-router";
5+
import { __HistoryContext as HistoryContext } from "react-router";
66

77
export default class Link extends React.PureComponent {
88
static defaultProps = {
@@ -28,14 +28,14 @@ export default class Link extends React.PureComponent {
2828
const { component: Component, to, replace, ...rest } = this.props;
2929

3030
return (
31-
<RouterContext.Consumer>
32-
{context => (
31+
<HistoryContext.Consumer>
32+
{history => (
3333
<Component
3434
{...rest}
35-
onPress={event => this.handlePress(event, context.history)}
35+
onPress={event => this.handlePress(event, history)}
3636
/>
3737
)}
38-
</RouterContext.Consumer>
38+
</HistoryContext.Consumer>
3939
);
4040
}
4141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import createNamedContext from "./createNameContext";
2+
3+
const historyContext = /*#__PURE__*/ createNamedContext("Router-History");
4+
export default historyContext;

packages/react-router/modules/Router.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import PropTypes from "prop-types";
33
import warning from "tiny-warning";
44

5+
import HistoryContext from "./HistoryContext.js";
56
import RouterContext from "./RouterContext.js";
67

78
/**
@@ -53,14 +54,18 @@ class Router extends React.PureComponent {
5354
render() {
5455
return (
5556
<RouterContext.Provider
56-
children={this.props.children || null}
5757
value={{
5858
history: this.props.history,
5959
location: this.state.location,
6060
match: Router.computeRootMatch(this.state.location.pathname),
6161
staticContext: this.props.staticContext
6262
}}
63-
/>
63+
>
64+
<HistoryContext.Provider
65+
children={this.props.children || null}
66+
value={this.props.history}
67+
/>
68+
</RouterContext.Provider>
6469
);
6570
}
6671
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// TODO: Replace with React.createContext once we can assume React 16+
2+
import createContext from "mini-create-react-context";
3+
4+
const createNamedContext = name => {
5+
const context = createContext();
6+
context.displayName = name;
7+
8+
return context;
9+
};
10+
11+
export default createNamedContext;

packages/react-router/modules/hooks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import invariant from "tiny-invariant";
33

44
import Context from "./RouterContext.js";
5+
import HistoryContext from "./HistoryContext.js";
56
import matchPath from "./matchPath.js";
67

78
const useContext = React.useContext;
@@ -14,7 +15,7 @@ export function useHistory() {
1415
);
1516
}
1617

17-
return useContext(Context).history;
18+
return useContext(HistoryContext);
1819
}
1920

2021
export function useLocation() {

packages/react-router/modules/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export { default as withRouter } from "./withRouter.js";
3535
import { useHistory, useLocation, useParams, useRouteMatch } from "./hooks.js";
3636
export { useHistory, useLocation, useParams, useRouteMatch };
3737

38+
export { default as __HistoryContext } from "./HistoryContext.js";
3839
export { default as __RouterContext } from "./RouterContext.js";

0 commit comments

Comments
 (0)