<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: James Uyi</title>
    <description>The latest articles on DEV Community by James Uyi (@kingjames_x).</description>
    <link>https://dev.to/kingjames_x</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F462502%2Ffaac9674-ba98-41b6-ba94-8d88bb62be33.jpg</url>
      <title>DEV Community: James Uyi</title>
      <link>https://dev.to/kingjames_x</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kingjames_x"/>
    <language>en</language>
    <item>
      <title>How to Fix React Router's Scroll Position on Page Transitions</title>
      <dc:creator>James Uyi</dc:creator>
      <pubDate>Sun, 26 May 2024 03:41:58 +0000</pubDate>
      <link>https://dev.to/kingjames_x/how-to-fix-react-routers-scroll-position-on-page-transitions-7cb</link>
      <guid>https://dev.to/kingjames_x/how-to-fix-react-routers-scroll-position-on-page-transitions-7cb</guid>
      <description>&lt;p&gt;When working on a React project recently, I noticed an odd behavior: whenever I navigated from one page to another using the navigation bar, the new page wouldn't start from the top. If I was on the footer section or anywhere below the navbar, the transition seemed abrupt, as if there was no smooth scrolling to the top of the new page.&lt;br&gt;
The quick fix for this issue is to create a root layout if you don't have one already, and then make all other routes children of that layout element. Let's dive into the code.&lt;br&gt;
Initially, my router configuration looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = createBrowserRouter([
  {
    path: "/",
    element: &amp;lt;Root /&amp;gt;,
    errorElement: &amp;lt;ErrorPage /&amp;gt;,
  },
  {
    path: "/about",
    element: &amp;lt;About /&amp;gt;,
    errorElement: &amp;lt;ErrorPage /&amp;gt;,
  },
  {
    path: "/contact us",
    element: &amp;lt;Contact /&amp;gt;,
    errorElement: &amp;lt;ErrorPage /&amp;gt;,
  },
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To resolve the scrolling issue, the first step is to create a layout component (e.g., layout.tsx or layout.jsx):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Outlet, useLocation } from "react-router-dom";
import { useLayoutEffect } from "react";

const Layout = () =&amp;gt; {
  const location = useLocation();

  useLayoutEffect(() =&amp;gt; {
    document.documentElement.scrollTo({ top: 0, left: 0, behavior: "instant" });
  }, [location.pathname]);

  return (
    &amp;lt;div&amp;gt;
{/* you could render your navbar  component here */}
      &amp;lt;Outlet /&amp;gt;
{/* you could render your  footer component here */}
    &amp;lt;/div&amp;gt;
  );
};

export default Layout;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: It's recommended to render your navbar and footer components within this layout instead of repeating them on every component.&lt;/p&gt;

&lt;p&gt;Next, in your main entry point (e.g., main.tsx or wherever you have your router configuration), import the layout and use it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = createBrowserRouter([
  {
    path: "/",
    element: &amp;lt;Layout /&amp;gt;,
    children: [
      {
        path: "/",
        element: &amp;lt;Root /&amp;gt;,
        errorElement: &amp;lt;ErrorPage /&amp;gt;,
      },
      {
        path: "/about",
        element: &amp;lt;About /&amp;gt;,
        errorElement: &amp;lt;ErrorPage /&amp;gt;,
      },
      // Add other routes here
    ],
  },
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this configuration, whenever you navigate from one page to another, the new page should always start from the top, providing a smooth scrolling experience.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactjsdevelopment</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
