<?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: Renan Monteiro</title>
    <description>The latest articles on DEV Community by Renan Monteiro (@remonts).</description>
    <link>https://dev.to/remonts</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%2F774323%2F7eac2a38-c3ad-4335-8de2-4a65d7f31320.jpeg</url>
      <title>DEV Community: Renan Monteiro</title>
      <link>https://dev.to/remonts</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/remonts"/>
    <language>en</language>
    <item>
      <title>Save state to localStorage</title>
      <dc:creator>Renan Monteiro</dc:creator>
      <pubDate>Tue, 14 Dec 2021 12:42:13 +0000</pubDate>
      <link>https://dev.to/remonts/save-state-to-localstorage-37ml</link>
      <guid>https://dev.to/remonts/save-state-to-localstorage-37ml</guid>
      <description>&lt;p&gt;Good morning,&lt;/p&gt;

&lt;p&gt;I need to save a state (which is a list of objects) in a localStorage, but the first value is always "[]", because I initialized the state that way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [favorites, setFavorites] = useState&amp;lt;IPodcast[]&amp;gt;([]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On localStorage it looks like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UyWLRN4o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/445nkalisaf7at9vujcu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UyWLRN4o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/445nkalisaf7at9vujcu.png" alt="Image description" width="708" height="65"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I need the first value saved in localStorage to be the first value in my list. Below is the code responsible for saving the state in localStorage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function adicionarFavorito(evento: React.FormEvent&amp;lt;HTMLFormElement&amp;gt;) {
evento.preventDefault();
podcastList.filter((p) =&amp;gt; p.id === idPodcast)
.map((p) =&amp;gt; (setFavorites((oldFavorites) =&amp;gt; [...oldFavorites, { ...podcastList[p.num] }])));
localStorage.setItem("favorites", JSON.stringify(favorites));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can someone help me?&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
