<?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: Leo Miranda</title>
    <description>The latest articles on DEV Community by Leo Miranda (@leomiranda).</description>
    <link>https://dev.to/leomiranda</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F676027%2Fdd44ea0d-4faf-4209-89ae-5415ac934e92.jpeg</url>
      <title>DEV Community: Leo Miranda</title>
      <link>https://dev.to/leomiranda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leomiranda"/>
    <language>en</language>
    <item>
      <title>Remix JS: Sending state on redirect without using parameters</title>
      <dc:creator>Leo Miranda</dc:creator>
      <pubDate>Mon, 25 Mar 2024 12:39:16 +0000</pubDate>
      <link>https://dev.to/leomiranda/remix-js-sending-state-on-redirect-without-using-parameters-3jg</link>
      <guid>https://dev.to/leomiranda/remix-js-sending-state-on-redirect-without-using-parameters-3jg</guid>
      <description>&lt;p&gt;Working with Remix means working with web standards. Just as we use search params to send data to another URL, we can also use a &lt;strong&gt;native browser feature&lt;/strong&gt;, &lt;code&gt;without exposing the information in the URL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will use the following hooks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://reactrouter.com/en/main/hooks/use-navigate" rel="noopener noreferrer"&gt;useNavigate&lt;/a&gt; - to redirect by sending the state&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://reactrouter.com/en/main/hooks/use-location" rel="noopener noreferrer"&gt;useLocation&lt;/a&gt; - to receive the state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;* You can use the &lt;a href="https://remix.run/docs/en/main/components/link" rel="noopener noreferrer"&gt;Link&lt;/a&gt; component too, instead of useNavigate.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem to solve
&lt;/h2&gt;

&lt;p&gt;I am implementing a &lt;code&gt;Save As&lt;/code&gt; button, when it is clicked, I need to redirect to a new route with the new ID.&lt;/p&gt;

&lt;p&gt;When opening the new cloned content, I want to show a message to the user.&lt;/p&gt;




&lt;h2&gt;
  
  
  Redirecting with state
&lt;/h2&gt;

&lt;p&gt;After processing the data on the server side with the &lt;a href="https://remix.run/docs/en/main/route/action" rel="noopener noreferrer"&gt;action function&lt;/a&gt;, we can validate the &lt;code&gt;actiondData&lt;/code&gt; and create a redirect using the useNavigate hook.&lt;/p&gt;

&lt;p&gt;Use &lt;a href="https://reactrouter.com/en/main/hooks/use-navigate#optionsstate" rel="noopener noreferrer"&gt;option.state&lt;/a&gt; to send the state to the landing link.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const navigate = useNavigate();

...

navigate('your/path/here', {
  state: {
    message: 'successfully-copied',
  },
});


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

&lt;/div&gt;




&lt;h2&gt;
  
  
  Get the state in the landing page
&lt;/h2&gt;

&lt;p&gt;Now, after being redirected to a new page, get the state using the &lt;a href="https://reactrouter.com/en/main/hooks/use-location#locationstate" rel="noopener noreferrer"&gt;useLocation&lt;/a&gt; hook.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const location = useLocation();
const stateMessage = location?.state?.message;


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

&lt;/div&gt;

&lt;p&gt;See a real example using toast:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

useEffect(() =&amp;gt; {
  if (stateMessage) {
    toast.success(t(stateMessage), {
      id: stateMessage,
    });
  }
}, [stateMessage, creative]);


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

&lt;/div&gt;




&lt;h2&gt;
  
  
  Using the Link component instead of the useNavigate hook
&lt;/h2&gt;

&lt;p&gt;You can also use the &lt;code&gt;Link&lt;/code&gt; component to pass the state, instead of the useNavigate hook: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://remix.run/docs/en/main/components/link#state" rel="noopener noreferrer"&gt;https://remix.run/docs/en/main/components/link#state&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Under the hood
&lt;/h2&gt;

&lt;p&gt;Under the hood we have the History interface of the History API, allowing manipulation of the browser session history. Simple, right?&lt;/p&gt;

&lt;p&gt;History: state property: &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History/state" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/History/state&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zm6ieokdx5jy9dwrafu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zm6ieokdx5jy9dwrafu.png" alt="Using Dev Tools console to update history api"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it, &lt;a href="https://remix.run/" rel="noopener noreferrer"&gt;Remix&lt;/a&gt; is simple and it uses web standards to the fullest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And you?&lt;/strong&gt; Do you like working with states differently? Tell us in the comments!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
