yyy rrr
yyy rrr

Reputation: 67

react I want to move the scroll position to top

We are using react.
I want to move the scroll position to the top when the button is pressed.
How do I set the scroll position to the top?

enter link description here

import "./styles.css";

const App = () => {
  const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  return (
    <div className="App">
      {list.map((list) => (
        <div
          style={{ height: "200px", background: "blue", marginBottom: "20px" }}
        >
          {list}
        </div>
      ))}
      <button>button</button>
    </div>
  );
};

export default App;

Upvotes: 0

Views: 441

Answers (1)

Shubham J.
Shubham J.

Reputation: 646

You can add method like this,

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

<button onClick={scrollToTop}>Scroll</button>

Upvotes: 6

Related Questions