Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Set focus on element after clicking 'scroll to top' button

I have a ScrollToTop component, which essentially scrolls to the top after clicking on the button however I've noticed that the focus stays on the button. How would I go about setting the focus to an element in another component?

Here's my ScrollToTop component:

const ScrollToTop = () => {
    const [showScrollButton, setShowScrollButton] = useState<boolean>(false)

    const checkScrollToTop = () => {
        if (!showScrollButton && window.pageYOffset > 400) {
            setShowScrollButton(true)
        } else if (showScrollButton && window.pageYOffset <= 400) {
            setShowScrollButton(false)
        }
    };

    const scrollToTop = () => {
        window.scrollTo({ top: 0, behavior: 'smooth' });
    };

    window.addEventListener('scroll', checkScrollToTop)

    return (
        <button
            type='button'
            onClick={scrollToTop}
        >
        </button>
    );
}

export default ScrollToTop;

And then I simply import it into the page, however the element I want the focus to be set on is within the List component:

return (
  <div>
    <List collections={collections} />
  </div>
  <ScrollToTop />
);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

document.getElementById("yourElementID").focus();

The above line focuses the element with ID yourElementID.

Try adding that line after scrolling.

Change

 const scrollToTop = () => {
    window.scrollTo({ top: 0, behavior: 'smooth' });
 };

To

const scrollToTop = () => {
    window.scrollTo({ top: 0, behavior: 'smooth' });
    document.getElementById("yourElementID").focus();
};
about 4 years ago · Juan Pablo Isaza Report

0

In your case, the ScrollToTop component changes the behaviour of the List component. Therefore I would extract the handling for this into the parent component, which is rendering both components.

I created a small demo (with some irrelevant typescript errors), which shows more clearly what I mean: https://codesandbox.io/s/scrolltotopdemo-dh3q3

Basically:

  1. Make a state in the "Parent component", which controls the focus of the List component
  2. Use a react ref to set the focus to the input element https://reactjs.org/docs/hooks-reference.html#useref
  3. Call a function in the ScrollToTop component when the button is clicked.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!