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

Preserving scroll position when changing path query #432

Closed
johnthedebs opened this issue Oct 28, 2014 · 4 comments
Closed

Preserving scroll position when changing path query #432

johnthedebs opened this issue Oct 28, 2014 · 4 comments

Comments

@johnthedebs
Copy link

Reopening this issue (previously: #337) since the changes don't seem to encompass this use case.

I'd like to preserve scroll position on a page when adding some query arguments to the URL but haven't figured out a way to do it.

If I set preserveScrollPosition for the page's route, it fixes the issue but causes the scroll position to be preserved coming from other pages as well. I also tried matching the query arguments in another Route, but was unable to match a ? literal.

Example code:

<Routes location="history">
    <Route name="index" path="/" handler={App}>
        <Route name="itemDetail" path="/items/:id/" handler={ItemDetail} />
        <Route name="itemGallery" path="/items/" handler={ItemGallery} />
        <DefaultRoute handler={Homepage} />
    </Route>
    <NotFoundRoute handler={Error404} />
</Routes>

When transitioning from /items/ to /items/?filter=blue the page scrolls back up to the top and there's no way to control the behavior.

@ryanflorence
Copy link
Member

#439 seems related

@ryanflorence
Copy link
Member

(total hack, but for now you can do some work in componentWillReceiveProps to mess with scroll position when a query shows up)

@gaearon
Copy link
Contributor

gaearon commented Nov 13, 2014

If I set preserveScrollPosition for the page's route

There's no preserveScrollPosition anymore, the new setting is called ignoreScrollBehavior. Have you tried it?

it causes the scroll position to be preserved coming from other pages as well

That's precisely what ignoreScrollBehavior is intended to fix. Does this work?

<Routes location="history">
    <Route name="index" path="/" handler={App}>
        <Route name="itemDetail" path="/items/:id/" handler={ItemDetail} />
        <Route name="itemGallery" path="/items/" handler={ItemGallery} ignoreScrollBehavior />
        <DefaultRoute handler={Homepage} />
    </Route>
    <NotFoundRoute handler={Error404} />
</Routes>

(You'll need 0.9.x at least)

@johnthedebs
Copy link
Author

I just tried ignoreScrollBehavior and it seems to accomplish what I was looking for. I'd gone over the documentation several times (after each update) and still managed to miss it somehow... Thanks for pointing it out!

gaearon added a commit to gaearon/react-router that referenced this issue Nov 26, 2014
Previously, the only way to opt out of scroll updates for a route would be by using `ignoreScrollBehavior`.
This, however, made it hard to implement arguably the most common use case: resetting scroll when `params` change and preserving it when only `query` changes.

This commit completely disables scroll updates when only `query` has changed.
This provides a reasonable default behavior and leaves `ignoreScrollBehavior` for more complicated cases.

If you'd rather keep the old behavior and reset scroll on query changes, you can either promote `query` variables to be route `params` or reset scroll position yourself in response to `query` changes in route handler's `componentDidUpdate`.

Fixes remix-run#432, remix-run#439
@lock lock bot locked as resolved and limited conversation to collaborators Jan 25, 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 a pull request may close this issue.

3 participants