<?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>Forem: Fazliddin</title>
    <description>The latest articles on Forem by Fazliddin (@fazliddin04).</description>
    <link>https://forem.com/fazliddin04</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%2F833452%2F37205956-3775-4e7a-aeb5-281606d41579.png</url>
      <title>Forem: Fazliddin</title>
      <link>https://forem.com/fazliddin04</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fazliddin04"/>
    <language>en</language>
    <item>
      <title>React-Router v6: Animated Transitions DIY</title>
      <dc:creator>Fazliddin</dc:creator>
      <pubDate>Fri, 25 Mar 2022 17:31:22 +0000</pubDate>
      <link>https://forem.com/fazliddin04/react-router-v6-animated-transitions-diy-3e6l</link>
      <guid>https://forem.com/fazliddin04/react-router-v6-animated-transitions-diy-3e6l</guid>
      <description>&lt;p&gt;Thanks to &lt;a href="https://dev.to/anxiny"&gt;Anxin.Y&lt;/a&gt; for &lt;a href="https://dev.to/anxiny/react-router-animated-transitions-diy-4opd"&gt;the article&lt;/a&gt; about making a transition DIY through animation on React-router v5. &lt;/p&gt;

&lt;p&gt;Now, I will try to make it with v6.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, Let's start!
&lt;/h2&gt;

&lt;p&gt;First, let's create the App component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  return (
    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;div className={`App`}&amp;gt;
        &amp;lt;nav&amp;gt;
          &amp;lt;Link to="/"&amp;gt;Home&amp;lt;/Link&amp;gt;
          &amp;lt;Link to="/other"&amp;gt;Other&amp;lt;/Link&amp;gt;
        &amp;lt;/nav&amp;gt;
        &amp;lt;Content /&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, the Content component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Content() {
    return (
    &amp;lt;div&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;section&amp;gt;Home&amp;lt;/section&amp;gt;} /&amp;gt;
        &amp;lt;Route path="/other" element={&amp;lt;section&amp;gt;Other&amp;lt;/section&amp;gt;} /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Now, we need to stop route from switching. By default, the &lt;code&gt;&amp;lt;Routes /&amp;gt;&lt;/code&gt; (in v5, &lt;code&gt;&amp;lt;Switch /&amp;gt;&lt;/code&gt;) will use the current url for matching the route, but we can stop it from doing that by assigning it a Location.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Routes location={displayLocation}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will need a state to keep the current location before 'Out' animation finish, and we can assign the current location as the default value. we can use useLocation to get the current location.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ...
  const location = useLocation();
  const [displayLocation, setDisplayLocation] = useState(location);
  ...
  &amp;lt;Routes location={displayLocation}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you click the Link, you will notice that even the URL is changed, the content stay the same.&lt;/p&gt;

&lt;p&gt;Next, we need add a state for controlling the stage of the transition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [transitionStage, setTransistionStage] = useState("fadeIn");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we can use useEffect to check if location is changed, and start the 'fadeOut'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  useEffect(() =&amp;gt; {
    if (location !== displayLocation) setTransistionStage("fadeOut");
  }, [location, displayLocation]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we need a way to update the stage and location when animation is over. For that we can use the onAnimationEnd event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Content() {
  ...
  return (
    &amp;lt;div
      className={`${transitionStage}`}
      onAnimationEnd={() =&amp;gt; {
        if (transitionStage === "fadeOut") {
          setTransistionStage("fadeIn");
          setDisplayLocation(location);
        }
      }}
    &amp;gt;
      ...
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before completion, You must add these to your CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.fadeIn {
  animation: 0.5s fadeIn forwards;
}

.fadeOut {
  animation: 0.5s fadeOut forwards;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translate(-20px, 0);
  }
  to {
    opacity: 1;
    transform: translate(0px, 0px);
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
    transform: translate(0px, 0px);
  }
  to {
    transform: translate(-20px, 0);
    opacity: 0;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, Here is the demo and the finished code:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/x6299o"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from "react";
import {
  BrowserRouter,
  Link,
  useLocation,
  Route,
  Routes
} from "react-router-dom";
import "./styles.css";

export default function App() {
  return (
    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;div className={`App`}&amp;gt;
        &amp;lt;nav&amp;gt;
          &amp;lt;Link to="/"&amp;gt;Home&amp;lt;/Link&amp;gt;
          &amp;lt;Link to="/other"&amp;gt;Other&amp;lt;/Link&amp;gt;
        &amp;lt;/nav&amp;gt;
        &amp;lt;Content /&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
  );
}

function Content() {
  const location = useLocation();

  const [displayLocation, setDisplayLocation] = useState(location);
  const [transitionStage, setTransistionStage] = useState("fadeIn");

  useEffect(() =&amp;gt; {
    if (location !== displayLocation) setTransistionStage("fadeOut");
  }, [location, displayLocation]);

  return (
    &amp;lt;div
      className={`${transitionStage}`}
      onAnimationEnd={() =&amp;gt; {
        if (transitionStage === "fadeOut") {
          setTransistionStage("fadeIn");
          setDisplayLocation(location);
        }
      }}
    &amp;gt;
      &amp;lt;Routes location={displayLocation}&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;section&amp;gt;Home&amp;lt;/section&amp;gt;} /&amp;gt;
        &amp;lt;Route path="/other" element={&amp;lt;section&amp;gt;Other&amp;lt;/section&amp;gt;} /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you &lt;a href="https://dev.to/anxiny"&gt;AnxinYang&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>react</category>
      <category>reactrouter</category>
      <category>animation</category>
    </item>
  </channel>
</rss>
