Smooth Scroll to Top of Page After Submitting From in React

Hello devs,

In this tutorial, I will show you how to scroll to top automatically after render or submit a form in react js. So scroll to the top of the page after submitting the form in react js is today's tutorial. You can also use this code in your core javascript code.

Sometimes we need to scroll to top automatically after submitting the form. Suppose we have a big form and after submit we need to show a success message. That time we scroll top after submitting the form. We don't need any hook to create this. 

Example code:

import { useEffect } from "react";
import { useLocation } from "react-router-dom";
 
function ScrollToTop() {

   //use this code after submitting form
   window.scrollTo(0, 0);

}
 
export default ScrollToTop;

 

Read also: React Redux Complete Setup Example with Api Call

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using Laravel Vue and React Js Application.