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.

 

#react-js