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 1 commit
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
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 HstoryContext from "./HistoryContext.js";
timdorr marked this conversation as resolved.
Show resolved Hide resolved
import matchPath from "./matchPath.js";

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

return useContext(Context).history;
return useContext(HstoryContext);
timdorr marked this conversation as resolved.
Show resolved Hide resolved
}

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";