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

180
Views
React imported component is not working completely

I'm really new to React and I've been trying for some time now to make a scroll to top function. Using the existing tutorials and everything on google i've made this component :

Import React, { useState, useEffect } from "react";


export default function ScrollToTop() {
const [isVisible, setIsVisible] = useState(false)

const toggleVisibility = () => {
    if(window.pageYOffset > 300) {
        setIsVisible(true);
    } else {
        setIsVisible(false);
    }
};

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

useEffect(() => {
    window.addEventListener('scroll', toggleVisibility);

    return () => {
        window.removeEventListener('scroll', toggleVisibility);
    };
}, []);

if (!isVisible) {
    return false
}

return (
<button className="button" onClick={ScrollToTop}>
    <div className="button__arrow button__arrow--up"></div>
</button>
)
};

the problem is that when i import it in the App.js it doesn't work properly, the scrolling part works perfectly, but the button it just stays at bottom of the page instead of appearing after a certain amount of scroll. This is the App.js:

return (
<div>
  {loading ? (
    <h1>Loading...</h1>
  ) : (
    <>
    <Navbar />
      <div className="Grid-container">
        {pokemonData.map((pokemon, i) => {
          return <Card key={i} pokemon={pokemon} />;
        })}
      </div>
    <ScrollToTop/>
    </>
  )}
</div>
);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't see any overt issues in your ScrollTop implementation that would cause the scroll-to-top button to remain on the page even after scrolling back to the top, but I do see room for optimization.

  1. For onScroll events it's best to use passive listeners.
  2. Invoke toggleVisibility once immediately in the mounting useEffect to ensure proper initial state.
  3. Apply Boolean Zen to the visibility toggler; Update the isVisible state with the result of the boolean comparison.
  4. Just conditionally render the button in a single return.

Code:

function ScrollToTop() {
  const [isVisible, setIsVisible] = useState(false);

  const toggleVisibility = () => {
    setIsVisible(window.pageYOffset > 300);
  };

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

  useEffect(() => {
    toggleVisibility();

    window.addEventListener("scroll", toggleVisibility, { passive: true });

    return () => {
      window.removeEventListener("scroll", toggleVisibility, { passive: true });
    };
  }, []);

  return isVisible ? (
    <button className="button" onClick={ScrollToTop}>
      <div className="button__arrow button__arrow--up">TOP</div>
    </button>
  ) : null;
}

Edit react-imported-component-is-not-working-completely

If you're still having issues with your implementation and usage try to create a codesandbox that reproduces the issue that we can inspect and debug live.

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!