fluxible-router icon indicating copy to clipboard operation
fluxible-router copied to clipboard

[Question] save scroll position between navigations

Open AbrahamAlcaina opened this issue 9 years ago • 12 comments

Hi, I used the fluxible-generator and it have the HandleHistory

export default handleHistory(provideContext(connectToStores(
    Application,
    [ApplicationStore, WallStore],
    function (stores, props) {
        const appStore = stores.ApplicationStore;
        const wallStore = stores.WallStore;
        return {
            currentPageName: appStore.getCurrentPageName(),
            pageTitle: appStore.getPageTitle(),
            pages: appStore.getPages(),
            wall: wallStore.getWall()
        };
    }
)));

I read the documentation and it restore automatically the scroll position. It's working fine if I use the refresh button in the browser, but if I navigated to another page and comeback it do not restore the scroll position.

I used the option preserveScrollPosition

 <NavLink routeName={link.page} preserveScrollPosition={true}>{link.title}</NavLink>

I miss something?

here you can test Sorry if it is a noov question.

AbrahamAlcaina avatar Jul 04 '15 08:07 AbrahamAlcaina

Can you try changing the order of the HoCs to provideContext(handleHistory(connectToStores(...)))? I wonder if handleHistory is not able to access the context correctly.

mridgway avatar Jul 08 '15 18:07 mridgway

@mridgway I changed it and still fail. Could be related with add action to the configuration? This is my code:

import {getNews} from '../actions/newsActions';
export default {
    home: {
        path: '/',
        method: 'get',
        page: 'home',
        title: 'Home',
        handler: require('../components/Home'),
        action: getNews
    },
    about: {
        path: '/about',
        method: 'get',
        page: 'about',
        title: 'About',
        handler: require('../components/About')
    }
};

PS today I uploaded all the app to github

AbrahamAlcaina avatar Jul 08 '15 19:07 AbrahamAlcaina

One of my components it is listening 'scroll' & 'resize'.

window.addEventListener('scroll', this.scrollListener);
window.addEventListener('resize', this.scrollListener);

To do an infinite scroll (aka load more items)

AbrahamAlcaina avatar Jul 08 '15 20:07 AbrahamAlcaina

I think I can confirm this. I originally wrote the preserveScrollPosition feature for flux-router-component. Both versions seem to differ in one very basic aspect. flux-router-component changes the current route after the route action has been executed, fluxible-router changes it directly on NAVIGATE_START.

In case of routes with async actions, the scroll position is being restored before the current route handler has anything to display.

@mridgway Can you confirm this?

tbo avatar Jul 18 '15 16:07 tbo

Confirmed, we moved pushState to the beginning of navigation. We've had some debates internally whether the scrollTo should be moved to NAVIGATE_SUCCESS and I think we're leaning towards doing just this. I think this would help with the issue at hand as well.

mridgway avatar Jul 18 '15 23:07 mridgway

@mridgway Have you made a decision on how to proceed?

tbo avatar Aug 17 '15 16:08 tbo

I believe @dnewcome has been diving into page transition behavior and may have some insight into the how to handle scrolling during navigation.

mridgway avatar Aug 18 '15 20:08 mridgway

@dnewcome @mridgway Do you have you any updates regarding the issue?

malaman avatar Sep 26 '15 21:09 malaman

I believe #87 will fix this.

Vijar avatar Sep 28 '15 17:09 Vijar

We're going to recommend that you manage the scroll positions on your own and we will provide access to the saved scroll position for pop state.

We have found that there's no silver bullet solution for scrolling. It needs to be synchronized with the flow of your application rather than before or after NAVIGATE_START.

We are not going to remove the current functionality, but we will suggest that if you need better handling, then you should disable scrolling with enableScroll: false and then manage it yourself.

mridgway avatar Sep 28 '15 18:09 mridgway

@mridgway thank you for answer. Could you please describe in more details how to receive access to the saved scroll positions. Thxs in advance.

malaman avatar Oct 01 '15 20:10 malaman

Scroll position is available on window.history.state.scroll.

mridgway avatar Mar 21 '16 21:03 mridgway