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

Reflecting UI changes to the route #104

Closed
ImanMh opened this issue Aug 16, 2017 · 8 comments
Closed

Reflecting UI changes to the route #104

ImanMh opened this issue Aug 16, 2017 · 8 comments

Comments

@ImanMh
Copy link

ImanMh commented Aug 16, 2017

Sometimes this would be grate to have UI state in route. for example in a list that is filtered with some check boxes. When the user clicks on a checkbox I like the route to change so that the user is able to share that route or bookmark it. I don't want to use # since Google will ignore it. currently if I do something like this:

import history from './history';
history.replace('/itemts/filter=discount,new');

It will change the position of my scroll. How can I implement such a thing?

@frenzzy frenzzy mentioned this issue Aug 16, 2017
20 tasks
@frenzzy
Copy link
Member

frenzzy commented Aug 16, 2017

The trick is to disable native History API Scroll Restoration behavor and control scroll manually.
One of implementation examples you can find here: React-Starter-Kit/src/client.js

@ImanMh
Copy link
Author

ImanMh commented Aug 17, 2017

Yea but sounds like it can never find the pos because current key is not present in the scrollPositionsHistory[location.key]

keys
current key

So pos object is never created. What am I doing wrong?

@frenzzy
Copy link
Member

frenzzy commented Aug 17, 2017

If location key does not exist that means the page is opened first time and we usually scroll to top in this case (as for non SPA apps).

@ImanMh
Copy link
Author

ImanMh commented Aug 17, 2017

For me changing line 163 of client.js to this solved scroll jump on history.push or history.replace

      () => onRenderComplete(route, { ...location, key: Object.keys(scrollPositionsHistory).pop() }),

@frenzzy I took a look at client.js and I don't know what I have to change to preserve scroll position during route change. Can you help me out?

@frenzzy
Copy link
Member

frenzzy commented Aug 18, 2017

You need to decide in which case you don't need to scroll. For example you may skip scrolling if path wasn't changed or if history.replace were used.

if (!(previousLocation.pathname === location.pathname && action === 'REPLACE')) {
  window.scrollTo(scrollX, scrollY);
}

@ImanMh
Copy link
Author

ImanMh commented Aug 18, 2017

That should do the job but my question is since scrollPositionsHistory is an object and I don't have previous location(and also it's key), How can I access last position history in scrollPositionsHistory ?

@frenzzy
Copy link
Member

frenzzy commented Aug 18, 2017

let previousLocation = history.location;
function onLocationChange(location) {
  // here you have both location and previousLocation
  previousLocation = location;
}

@ImanMh
Copy link
Author

ImanMh commented Aug 18, 2017

Thanks a lot you are so helpful.

@ImanMh ImanMh closed this as completed Aug 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants