Skip to content

Add context.route.location #4589

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

Merged
merged 5 commits into from
Mar 3, 2017
Merged

Add context.route.location #4589

merged 5 commits into from
Mar 3, 2017

Conversation

pshrmn
Copy link
Contributor

@pshrmn pshrmn commented Feb 23, 2017

When a <Switch> or a <Route> has a location prop, its children should match using that location instead of the current history.location.

This also adds a modal example since the PR changes enable it and that seems to be a popular request.

if (!this.props.location) {
// Start listening here so we can <Redirect> on the initial render.
this.unlisten = parentRouter.listen(() => {
Object.assign(this.router, parentRouter, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Object.assign? We support object spread (it's 8 lines above this 😄 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spread would create a new object.

this.router = {
  ...this.router,
  ...parentRouter,
  match: computeMatch(...)
}
// compiles to
this.router = _extends({}, this.router, parentRouter, {
    match: computeMatch(...)
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timdorr We mutate this.context.router intentionally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I've got it now 👍

@pshrmn pshrmn mentioned this pull request Feb 23, 2017
@pshrmn pshrmn mentioned this pull request Feb 24, 2017
@pshrmn
Copy link
Contributor Author

pshrmn commented Feb 26, 2017

This can be simplified once #4598 is merged. Will hold off on updating this until then.

@pshrmn pshrmn changed the title Prefer prop location over parent location Add context.route.location Feb 28, 2017
@pshrmn
Copy link
Contributor Author

pshrmn commented Feb 28, 2017

This has been updated to work with the new context API. context.route.location is the location used to render the parent.

Each <Route> will set this value in getChildContext. Typically, this will be the same as history.location. When a <Route> is given a location prop, this will be that object.

The <Switch> passes the location that it uses to match routes to the <Route> that it renders. This allows its children to know what location its parent <Route> was rendered "in" without having to have the <Switch> implement gCC

pshrmn referenced this pull request Feb 28, 2017
we can just use a Route to make a match, no need for special-case code
@mjackson
Copy link
Member

mjackson commented Mar 1, 2017

Hey @pshrmn, I did my best to resolve conflicts with this branch and current v4. Please give it the thumbs up and I'll merge away.

@pshrmn
Copy link
Contributor Author

pshrmn commented Mar 1, 2017

There are a couple of tweaks that I think need to be made still. I will commit those later tonight. Was there ever any decision on whether to add context.route directly in <Router> or to have it render a <Route path='/' />?

The default `context.route.location` value is the same as
`context.history.location`. However, if a <Route> has a `location` prop,
then all of its children will see that location object as
`context.route.location`.

This also updates the `<Switch>` component to pass its location as a prop to the
component that it renders.
When calling computeMatch from componentWillReceiveProps, we need to use nextContext, not this.context
Copy link
Contributor Author

@pshrmn pshrmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving a few comments on new changes.

@@ -61,7 +61,7 @@ class FakeBrowser extends React.Component {

return (
<MemoryRouter getUserConfirmation={getUserConfirmation}>
<Route render={({ canGo, goBack, goForward, push, location }) => (
<Route render={({ history, location }) => (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to fix a few things in the examples to get them working with the new { match, location, history } props.

@@ -62,15 +63,15 @@ class Route extends React.Component {
)

this.setState({
match: this.computeMatch(nextProps)
match: this.computeMatch(nextProps, nextContext)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use the next context object here. Otherwise when there is no location prop, the previous location will be used when computing the new match.

const { history } = this.context
const { location } = history
const { history, route } = this.context
const location = this.props.location || route.location
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure that the component rendered by the route receives the correct location.

@mjackson
Copy link
Member

mjackson commented Mar 2, 2017

@pshrmn We're going to add context.route directly in <Router> instead of rendering a root <Route>. I think this will make it easier for folks to understand using the DevTools.

@pshrmn
Copy link
Contributor Author

pshrmn commented Mar 2, 2017

@mjackson Yes, I saw that you switched it back shortly after I had asked the question. This should be good to merge if everything looks fine to you.

@mjackson mjackson merged commit b9be9a9 into remix-run:v4 Mar 3, 2017
@mjackson
Copy link
Member

mjackson commented Mar 3, 2017

Thanks @pshrmn

@pshrmn pshrmn deleted the pass-props branch March 3, 2017 07:36
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants