Open In App

ReactJS UI Ant Design BackTop Component

BackTop is a component in Ant Design that provides a button when the user has scrolled through the page to go back to the top of the page without the need to scroll back to the top. It will take the user to the top of the page with a smooth scrolling animation.

Ant Design Library has this component pre-built, and it is very easy to integrate as well. We can use this BackTop component using the following approach easily.



Syntax:

<BackTop>
      Scroll to Top
</BackTop>

BackTop Property:

Creating React Application and Installing Module:

Project Structure:

Example: Now write the following code in filename App.js.




import { BackTop } from "antd";
import "./App.css";
import "antd/dist/antd.css";
  
const style = {
  height: 40,
  width: 40,
  lineHeight: "40px",
  borderRadius: 4,
  backgroundColor: "#1088e9",
  color: "#fff",
  textAlign: "center",
  fontSize: 14,
};
  
function App() {
  return (
    <div className="App">
      <div style={{ height: "600vh"
                    padding: 8, 
                    margin: "auto 20rem" }}>
        <div>Scroll to bottom</div>
        <div>Scroll to bottom</div>
        <div>Scroll to bottom</div>
        <div>Scroll to bottom</div>
        <div>Scroll to bottom</div>
        <div>Scroll to bottom</div>
        <div>Scroll to bottom</div>
        <BackTop>
          <div style={style}>UP</div>
        </BackTop>
      </div>
    </div>
  );
}
  
export default App;

Running the application: Run the application by using the following command.

npm start

Output: Now open your browser and go to http://localhost:3000/, You will see the following output.


Article Tags :