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

Separate history object to its own context #7103

Merged
merged 4 commits into from May 8, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/react-router-native/BackButton.js
@@ -1,7 +1,7 @@
import React from "react";
import { BackHandler } from "react-native";

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

class BackButton extends React.Component {
handleBack = () => {
Expand All @@ -23,12 +23,12 @@ class BackButton extends React.Component {

render() {
return (
<RouterContext.Consumer>
{context => {
this.history = context.history;
<HistoryContext.Consumer>
{history => {
this.history = history;
return this.props.children || null;
}}
</RouterContext.Consumer>
</HistoryContext.Consumer>
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/react-router-native/DeepLinking.js
@@ -1,7 +1,7 @@
import React from "react";
import { Linking } from "react-native";

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

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

Expand All @@ -27,12 +27,12 @@ class DeepLinking extends React.Component {

render() {
return (
<RouterContext.Consumer>
{context => {
this.history = context.history;
<HistoryContext.Consumer>
{history => {
this.history = history;
return this.props.children || null;
}}
</RouterContext.Consumer>
</HistoryContext.Consumer>
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/react-router-native/Link.js
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { TouchableHighlight } from "react-native";
import PropTypes from "prop-types";

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

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

return (
<RouterContext.Consumer>
{context => (
<HistoryContext.Consumer>
{history => (
<Component
{...rest}
onPress={event => this.handlePress(event, context.history)}
onPress={event => this.handlePress(event, history)}
/>
)}
</RouterContext.Consumer>
</HistoryContext.Consumer>
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/react-router/modules/HistoryContext.js
@@ -0,0 +1,4 @@
import createNamedContext from "./createNameContext";

const historyContext = /*#__PURE__*/ createNamedContext("Router-History");
export default historyContext;
9 changes: 7 additions & 2 deletions packages/react-router/modules/Router.js
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import PropTypes from "prop-types";
import warning from "tiny-warning";

import HistoryContext from "./HistoryContext.js";
import RouterContext from "./RouterContext.js";

/**
Expand Down Expand Up @@ -53,14 +54,18 @@ class Router extends React.Component {
render() {
return (
<RouterContext.Provider
children={this.props.children || null}
value={{
history: this.props.history,
illuminist marked this conversation as resolved.
Show resolved Hide resolved
location: this.state.location,
match: Router.computeRootMatch(this.state.location.pathname),
staticContext: this.props.staticContext
}}
/>
>
<HistoryContext.Provider
children={this.props.children || null}
illuminist marked this conversation as resolved.
Show resolved Hide resolved
value={this.props.history}
/>
</RouterContext.Provider>
);
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/react-router/modules/createNameContext.js
@@ -0,0 +1,11 @@
// TODO: Replace with React.createContext once we can assume React 16+
import createContext from "mini-create-react-context";

const createNamedContext = name => {
const context = createContext();
context.displayName = name;

return context;
};

export default createNamedContext;
3 changes: 2 additions & 1 deletion packages/react-router/modules/hooks.js
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import invariant from "tiny-invariant";

import Context from "./RouterContext.js";
import HistoryContext from "./HistoryContext.js";
import matchPath from "./matchPath.js";

const useContext = React.useContext;
Expand All @@ -14,7 +15,7 @@ export function useHistory() {
);
}

return useContext(Context).history;
return useContext(HistoryContext);
}

export function useLocation() {
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/modules/index.js
Expand Up @@ -35,4 +35,5 @@ export { default as withRouter } from "./withRouter.js";
import { useHistory, useLocation, useParams, useRouteMatch } from "./hooks.js";
export { useHistory, useLocation, useParams, useRouteMatch };

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