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

Bug: Does React automatically handle scrolling for me? #26690

Closed
kajweb opened this issue Apr 21, 2023 · 6 comments
Closed

Bug: Does React automatically handle scrolling for me? #26690

kajweb opened this issue Apr 21, 2023 · 6 comments
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@kajweb
Copy link

kajweb commented Apr 21, 2023

React version:
NULL

Steps To Reproduce

I provided both a vanilla JS version and a React version and found that there are differences between the two.
After entering the webpage, click the "L button" first, move the scrollbar to the bottom,
then click the "M button", and then click the "L button" again.

Link to code example:
https://codesandbox.io/s/react-scroll-top-cvge7z
https://codesandbox.io/s/vanilla-scroll-top-yckxwd

The expected behavior

You can see that in React, the scrollbar is at the bottom, while in vanilla JS it behaves as expected.

image
image

@kajweb kajweb added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 21, 2023
@umang-bhatt
Copy link

@kajweb
Try this to achieve the same behaviour as expected in vanilla script

import "../App.css";
import { useRef, useEffect, useState } from "react";

export default function Scroller() {
    const [data, setData] = useState("");

    const ref = useRef(null);
    const [scrollPosition, setScrollPosition] = useState(0);

    useEffect(() => {
        ref.current.scrollTop = scrollPosition;
    }, [scrollPosition]);


    const changeText = (type) => {
        setData(
            [
                ...Array(
                    {
                        S: 1,
                        M: 1000,
                        L: 5000,
                        LP: 5000
                    }[type]
                )
            ]
                .map((i) => type)
                .join(" ")
        );
        setScrollPosition(ref.current.scrollTop);
    }

    return (
        <div>
            <div className="flex items-center justify-center">
                <div
                    style={{
                        height: 600,
                        width: 300,
                        border: "1px solid #ccc",
                        overflow: "auto"
                    }}
                    ref={ref}
                >
                    {data}
                </div>
            </div>

            <div className="flex gap-2">
                {/* <button onClick={() => changeText("S")}>S</button> */}
                <button onClick={() => changeText("M")}>M</button>
                <button onClick={() => changeText("L")}>L</button>
                <button onClick={() => changeText("LP")}>LP</button>
            </div>
        </div>
    );
}

Hope this helps !

@kajweb
Copy link
Author

kajweb commented Apr 24, 2023

It worked.

Copy link

github-actions bot commented Apr 9, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 9, 2024
@kajweb
Copy link
Author

kajweb commented Apr 10, 2024

any comment

@github-actions github-actions bot removed the Resolution: Stale Automatically closed due to inactivity label Apr 10, 2024
Copy link

github-actions bot commented Jul 9, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Jul 9, 2024
Copy link

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants