<?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: Dre</title>
    <description>The latest articles on DEV Community by Dre (@ikeeptrying).</description>
    <link>https://dev.to/ikeeptrying</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%2F228535%2F90091547-d7c6-486f-be3d-2776ce7dace2.jpeg</url>
      <title>DEV Community: Dre</title>
      <link>https://dev.to/ikeeptrying</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ikeeptrying"/>
    <language>en</language>
    <item>
      <title>Fullstackopen part 5: createRef and forwardRef causing problems with autofocus</title>
      <dc:creator>Dre</dc:creator>
      <pubDate>Fri, 22 May 2020 18:17:36 +0000</pubDate>
      <link>https://dev.to/ikeeptrying/fullstackopen-part-5-createref-and-forwardref-causing-problems-with-autofocus-4cen</link>
      <guid>https://dev.to/ikeeptrying/fullstackopen-part-5-createref-and-forwardref-causing-problems-with-autofocus-4cen</guid>
      <description>&lt;p&gt;Hi, I'm currently in part 5 of the &lt;a href="https://fullstackopen.com/en/about"&gt;University of Helsinki fullstackopen&lt;/a&gt; course. &lt;/p&gt;

&lt;p&gt;I'm not officially enrolled, (not even sure how to do that) and this fact is relevant - and I'm fine with it - because it lets me be free to take as long as I wish to complete each part of the course. &lt;/p&gt;

&lt;p&gt;I don't know if I am a slow learner or not, but one reason my progress is slow, is because I insist on &lt;em&gt;understanding&lt;/em&gt; each concept that is introduced, and I test myself by &lt;em&gt;attempting&lt;/em&gt; to use each new thing on a different app. In addition to doing the exercises, I keep a third &amp;amp; fourth app updated with new features. &lt;/p&gt;

&lt;p&gt;This practice has, I believe, resulted in gradually achieving deeper understanding and more confidence that I actually learned the lesson. &lt;/p&gt;

&lt;p&gt;HOWEVER... &lt;/p&gt;

&lt;p&gt;It has also caused me, on more than one occasion, to become deeply frustrated and on the verge of despair, when I am unable to successfully replicate a feature in a slightly different way. Sometimes I try to expand on a feature in the exercise app (the blog list) by using it in a slightly different way, and/or I try to replicate it in a different app (like the notes and phonebook apps from the earlier exercises). &lt;/p&gt;

&lt;p&gt;The problem I am currently working on, and the reason for this post, is the &lt;code&gt;createRef()&lt;/code&gt; function introduced in &lt;a href="https://fullstackopen.com/en/part5/props_children_and_proptypes/"&gt;part 5b&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I was already using &lt;code&gt;useRef&lt;/code&gt; to make the focus on initial render to be in the &lt;code&gt;new note&lt;/code&gt; input, so that immediately when the page loads, you can just start typing, since that was the only field on the page at that moment. &lt;/p&gt;

&lt;p&gt;Now, after using &lt;code&gt;createRef&lt;/code&gt; to handle the &lt;code&gt;toggle&lt;/code&gt; functionality, my &lt;code&gt;useRef&lt;/code&gt; focus does not work any more, and I have spent the entire last two days (I'm not working, so I actually work on this all day, every day) trying to learn how to use &lt;code&gt;createRef&lt;/code&gt;, &lt;code&gt;useRef&lt;/code&gt;, and &lt;code&gt;forwardRef&lt;/code&gt;. It's tricky because the toggle hides and unhides the login and the new note forms, which, in my opinion, should also be auto focused on the &lt;code&gt;username&lt;/code&gt; and then &lt;code&gt;new note&lt;/code&gt; fields, when each comes into view (this happens separately; I'm not trying to autofocus two things that are rendered at the same time). &lt;/p&gt;

&lt;p&gt;Hundreds of google and stackoverflow searches have produced nothing but the most basic implementation; meaning, they work for a single component, but break when the component is factored out, or, it is only used for a single form on a single page. &lt;/p&gt;

&lt;p&gt;What I have now is a situation where I need to use these &lt;code&gt;ref&lt;/code&gt;s in two different ways, and I have tweaked the code at least 100 different ways that seemed to make sense, but nothing works - meaning, I can &lt;em&gt;either&lt;/em&gt; have the toggle functionality, &lt;em&gt;or&lt;/em&gt; the auto focus, but not both. I want focus to happen when the &lt;code&gt;login&lt;/code&gt; button or the &lt;code&gt;new note&lt;/code&gt; button is clicked.&lt;/p&gt;

&lt;p&gt;Important note: I am using strictly functional components and not class components. I understand that using &lt;code&gt;ref&lt;/code&gt;s with functional components requires &lt;code&gt;useEffect&lt;/code&gt;, since there is no mount/unmount. I am not interested in having a hybrid app with both functional and class components (does anyone actually do that? Maybe I'm wrong?). I am convinced that there must be a way to make this work, or perhaps there is a package that can help with auto focus? I try to avoid adding packages unless absolutely necessary, but this problem is super frustrating, and I'm about ready to move on. &lt;/p&gt;

&lt;p&gt;Here are the links to my &lt;a href="https://github.com/I-keep-trying/fullstack2020-notes-frontend"&gt;frontend repo&lt;/a&gt;, &lt;a href="https://github.com/I-keep-trying/notes-backend"&gt;backend repo&lt;/a&gt;, and the &lt;a href="https://floating-bayou-03011.herokuapp.com/"&gt;heroku deployed&lt;/a&gt; notes app. There are usernames and passwords in the &lt;code&gt;.rest&lt;/code&gt; file on the backend, but if you just want to try out the heroku app, you may use &lt;code&gt;username: Bart_Simpson&lt;/code&gt; with &lt;code&gt;password: password&lt;/code&gt; and that should work. If you want to clone the repos to try them out, you need a mongodb database to connect to in order for it to work. &lt;/p&gt;

&lt;p&gt;When I figure out this focus problem, the next thing I need to do is figure out how to expire the jwt tokens. I have added code that &lt;em&gt;looks like&lt;/em&gt; it's supposed to make the token expire in 1 hour, but it doesn't. Logging out does indeed remove the locally stored token, for me anyway.&lt;/p&gt;

&lt;p&gt;In the meantime, I guess I'll be doing some deep dives into how the DOM works with react, and perhaps &lt;a href="https://overreacted.io/a-complete-guide-to-useeffect/"&gt;this article&lt;/a&gt; will help. &lt;/p&gt;

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