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

Page scrolled back to the top when redirecting to same the page from useFetcher Form #3145

Closed
ghost opened this issue May 9, 2022 · 8 comments · Fixed by remix-run/react-router#9963
Labels
bug Something isn't working feat:scroll Issues related to scroll restoration

Comments

@ghost
Copy link

ghost commented May 9, 2022

What version of Remix are you using?

1.4.1

Steps to Reproduce

Submit a form from useFetcher

import type { ActionFunction } from '@remix-run/node';
import { redirect } from '@remix-run/node';
import { useFetcher } from '@remix-run/react';

export const action: ActionFunction = () => {
    return redirect('/test');
};

export default function Test() {
    const fetcher = useFetcher();

    return (
        <div>
            <fetcher.Form method="post" style={{ marginTop: '150vh' }}>
                <button type="submit">Submit</button>
            </fetcher.Form>
        </div>
    );
}

Expected Behavior

Page should stay at the same scroll position

Actual Behavior

Page is scrolled to the top

@ghost ghost added the bug:unverified label May 9, 2022
@ghost ghost changed the title Scroll goes back to top when redirecting to same page from useFetcher form Paged scrolled back to the top when redirecting to same the page from useFetcher Form May 9, 2022
@kiliman
Copy link
Collaborator

kiliman commented May 9, 2022

Do you have the <ScrollRestoration/> component in your root.tsx?

@ghost
Copy link
Author

ghost commented May 9, 2022

I do which is what's causing it, I'm just not sure that it's the right behaviour

@kiliman
Copy link
Collaborator

kiliman commented May 9, 2022

Hmm.. interesting. ScrollRestoration should only navigate to the top on route changes. I guess it sees a redirect as a route change despite it being the same route.

Since you're using fetcher.Form, is there a reason why you're redirecting? fetcher.Form doesn't do navigation, so it will just return the result and you can stay on the same page.

@ghost
Copy link
Author

ghost commented May 10, 2022

This is for JS/non-JS support where I'm using the same action. In the non-JS version I want a redirect whereas in the JS version I want to stay on the same page, the logic is close the the Newsletter Sign Up example.

As far as I'm aware useFetcher will redirect if it recieves a redirect response but I don't think it should if you're on the same page. By the looks of it this is the funcionality of a normal Form submission see here.

I could write some custom logic to only return a redirect if I provide a certain field but that feels messy.

@ghost ghost changed the title Paged scrolled back to the top when redirecting to same the page from useFetcher Form Page scrolled back to the top when redirecting to same the page from useFetcher Form May 11, 2022
@matt-kinton
Copy link
Contributor

matt-kinton commented May 11, 2022

I've added a PR which fixes the problem for me but I don't know if it's what the Remix team will want.

Also fixed the PR to use my actual personal account and not my work one :(

@machour machour added the feat:scroll Issues related to scroll restoration label Dec 1, 2022
@machour machour linked a pull request Dec 1, 2022 that will close this issue
2 tasks
@brophdawg11 brophdawg11 self-assigned this Jan 23, 2023
@brophdawg11
Copy link
Contributor

@kiliman is correct in that a fetcher redirecting is a new location so it will cause scroll position to be reset. We recently added <Form preventScrollReset> in react-router@6.7.0 but it looks like that doesn't make it through for <fetcher.Form> so we'll get that fixed.

Once we fix that, <fetcher.Form method="post" preventScrollReset={true}> should be what you need

@brophdawg11 brophdawg11 added bug Something isn't working and removed bug:unverified labels Jan 23, 2023
@brophdawg11 brophdawg11 added the awaiting release This issue has been fixed and will be released soon label Jan 24, 2023
@brophdawg11
Copy link
Contributor

This should be fixed in Remix 1.12.0 which should be deployed in the next hour or two

@brophdawg11
Copy link
Contributor

Released in 1.12.0

@brophdawg11 brophdawg11 removed the awaiting release This issue has been fixed and will be released soon label Jan 31, 2023
@brophdawg11 brophdawg11 removed their assignment Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feat:scroll Issues related to scroll restoration
Projects
None yet
4 participants