<?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: lmeromy</title>
    <description>The latest articles on Forem by lmeromy (@lmeromy).</description>
    <link>https://forem.com/lmeromy</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%2F176309%2F0a7cd595-6931-4cff-858e-102debae7906.jpeg</url>
      <title>Forem: lmeromy</title>
      <link>https://forem.com/lmeromy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lmeromy"/>
    <language>en</language>
    <item>
      <title>Help! I have useEffect dependency array warnings</title>
      <dc:creator>lmeromy</dc:creator>
      <pubDate>Fri, 04 Feb 2022 04:51:01 +0000</pubDate>
      <link>https://forem.com/lmeromy/help-i-have-useeffect-dependency-array-warnings-428o</link>
      <guid>https://forem.com/lmeromy/help-i-have-useeffect-dependency-array-warnings-428o</guid>
      <description>&lt;p&gt;Running into issues when using useEffect in a React component is nothing new. How to understand and properly use useEffect (including my current issue) has been extensively covered by people like &lt;a href="https://overreacted.io/a-complete-guide-to-useeffect/#dont-lie-to-react-about-dependencies"&gt;Dan Abramov&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And yet, here I am! This is a short, hopefully clear explanation for the benefit of my future self when I inevitably forget what I've just learned. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt;&lt;br&gt;
Including an object as a dependency in my useEffect dependency array causes infinite looping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const params = {name: 'myName', id: 1}
&amp;lt;MyComponent params={params}/&amp;gt;

const MyComponent = ({ params }) =&amp;gt; {
  const [nodes, setNodes] = useState([]);

  useEffect(() =&amp;gt; {
    const res = axios.get('/my/request/url', params );
    setNodes(res);
  }, [params]);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And removing those dependencies from my useEffect dependency array brings up this warning:&lt;br&gt;
&lt;code&gt;React Hook useEffect has a missing dependency: 'params'. Either include it or remove the dependency array.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solution(s):&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Think about whether or not I actually need the object in my useEffect block (answer: probably yes or I wouldn't have put it in the dependency array in the first place, but good to be thoughtful).&lt;/li&gt;
&lt;li&gt;If I do need the object, try to specify the values I am using, don't just reference the object or the array as whole.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    const res = axios.get('/my/request/url', { name: params.name, id: params.id, });
    setNodes(res);
  }, [params.name, params.id]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Another option is to suppress the linter warning, but this is generally &lt;em&gt;not&lt;/em&gt; best practice, and may hide other bugs in the future):&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; {
    const res = axios.get('/my/request/url', params);
    setNodes(res);
// eslint-disable-line react-hooks/exhaustive-deps
  }, [params]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this issue occurs:&lt;/strong&gt;&lt;br&gt;
Basically, object equality in Javascript is confusing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const object1 = { id: 1 };
const object2 = { id: 1 };
object1 === object2 // returns false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Objects (including arrays, dates, and plain objects) are compared by their reference. The comparison by reference checks to see if the objects given refer to the same location in memory...and here, they don't. &lt;/p&gt;

&lt;p&gt;For my case, &lt;code&gt;params' does not change between renders, but the useEffect only 'sees' the&lt;/code&gt;params&lt;code&gt;object it has on a given render and compares it to the previous&lt;/code&gt;params` object from the previous render. And comparing two objects as equal will return false, so the code inside the useEffect will execute again and again. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other related issues and possible solutions:&lt;/strong&gt;&lt;br&gt;
I read about another solution using refs explained well in (&lt;a href="https://betterprogramming.pub/stop-lying-to-react-about-missing-dependencies-10612e9aeeda"&gt;this article&lt;/a&gt;). &lt;br&gt;
It didn't work for my particular situation since I needed the useEffect to execute with the initial props I was passing, but it seems like a useful idea for other scenarios. &lt;/p&gt;

&lt;p&gt;Hope this is helpful to someone else! &lt;/p&gt;

</description>
      <category>react</category>
      <category>useeffect</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Testing React Components: How to mock imports with Jest</title>
      <dc:creator>lmeromy</dc:creator>
      <pubDate>Thu, 23 Apr 2020 23:14:05 +0000</pubDate>
      <link>https://forem.com/lmeromy/testing-react-components-how-to-mock-imports-with-jest-1k90</link>
      <guid>https://forem.com/lmeromy/testing-react-components-how-to-mock-imports-with-jest-1k90</guid>
      <description>&lt;p&gt;&lt;em&gt;Thanks to &lt;a href="https://www.blackillustrations.com/"&gt;Black Illustrations&lt;/a&gt; for this cover image!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Recently, I was writing some tests for a few React components at work, and  I had to mock a module from another part of my app in order to properly set things up. &lt;br&gt;
The following is a short guide for how to mock a module with Jest...written for myself as a reference for the next time I have to do this so I don't need ask the senior dev on my team again. I still find testing React components challenging, so any help for future me (or other interested folks!) is a good thing.&lt;br&gt;
For anyone unfamiliar: &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; is shipped automatically with create-react-app, so it is a commonly used testing framework in React apps, and it's what I'm using at work.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Three steps to mock an import:&lt;/strong&gt;&lt;br&gt;
1) Import what you need as a module object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;FooModule&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../relative/path/to/foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2) Tell Jest to watch the path to that module. Above your 'describe' block, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../relative/path/to/foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3) In the test, access the exported function that you need from the module (they are all replaced with mock functions now) and tell it what to return or assert if it has been called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;FooModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;methodName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mockReturnValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// or .mockImplementation()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When using mocks, you typically want to mimic the original behavior of the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final notes:&lt;/strong&gt; &lt;br&gt;
Make sure to clear the mocked module in a beforeEach block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;beforeEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;FooModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;methodName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mockClear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;...and clear all mocks in an afterEach block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;afterEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;restoreAllMocks&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Link to more information about &lt;a href="https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options"&gt;Jest module mocks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Interesting side note: I was seeing strange, intermittently failing tests after I thought I had properly set everything up to mock the module in question. Turns out I had a sneaky, hidden dependency in several child components! I was testing an overly large component which had several child components depending on the module I needed to mock. I was mocking it in the child component tests, but not in the parent's test because the parent component did not use the module. Once I mocked the module in the parent's spec file, all seemed well. What a great argument for smaller components!&lt;/p&gt;

</description>
      <category>react</category>
      <category>testing</category>
    </item>
    <item>
      <title>Dipping a toe in the waters of RedwoodJS</title>
      <dc:creator>lmeromy</dc:creator>
      <pubDate>Thu, 26 Mar 2020 02:02:53 +0000</pubDate>
      <link>https://forem.com/lmeromy/dipping-a-toe-in-the-waters-of-redwoodjs-49k5</link>
      <guid>https://forem.com/lmeromy/dipping-a-toe-in-the-waters-of-redwoodjs-49k5</guid>
      <description>&lt;p&gt;&lt;em&gt;Illustration by Katerina Limpitsouni, what an artist! &lt;a href="https://undraw.co/"&gt;https://undraw.co/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;OK, I admit....I was initially excited about the release of &lt;a href="https://redwoodjs.com/"&gt;RedwoodJS&lt;/a&gt; simply because of the name. I was born and raised in Northern California, and I think Redwood trees are really quite special. Tom Preston-Werner, the author of this new JAMstack framework, chose the name for &lt;a href="https://redwoodjs.com/docs/introduction#why-is-it-called-redwood"&gt;similar reasons&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Recently, I have been considering which tools to use for my next side-project, and I though it would be fun to try a React-based static site generator since I use a lot of 'plain' React professionally. I had been considering Gatsby and a few other options, but for now I'll describe my brief foray into the Redwoods. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overall Verdict: pretty cool!&lt;/strong&gt; &lt;br&gt;
Some folks may not like the fact that it is an opinionated framework and you cannot pick and choose your preferred technologies, but others will see that as a strength. I found this to be a good thing as I am still relatively new to software engineering and have plenty to learn in all areas. &lt;/p&gt;

&lt;p&gt;The tutorial was great! It was a fabulous and very detailed introduction, it really goes from the very beginning to deploying a live site, and I learned a ton. Of the various technologies used in RedwoodJs, GraphQL and Netlify were the two completely new things for me....honestly I could not have defined what the GraphQL API was before. So I learned a bit about those too via hands-on experience and reading.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other things I liked:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically code-splits for great efficiency&lt;/li&gt;
&lt;li&gt;A CDN (Content Delivery Network) is easy to set up even if you know very little about devops. &lt;/li&gt;
&lt;li&gt;The short, technical explanations sprinkled throughout the tutorial were extremely helpful (e.g. server-side validation: when we need it and when the database takes care of it). &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scaffolds&lt;/strong&gt; is a really slick bit of functionality which creates all the necessary pieces to perform CRUD actions on a given database table AND creates the various web components required to do all CRUD stuff from the UI. And all with a single yarn command.&lt;/li&gt;
&lt;li&gt;Cells are a new abstraction used to fetch data in a declarative way (versus imperative). It is just a file which exports a GraphQL query, a 'Success' constant (which receives data following a successful GraphQL call), a 'Failure' constant to handle failures, 'Loading', and 'Empty'.  Then these constants are available to pass to other React components to use however you want. The tricky, asynchronous stuff is abstracted away. Nifty! &lt;/li&gt;
&lt;li&gt;Redwood is optimized for building web apps, so it fills a different space in the ecosystem compared to Gatsby and others like it. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Not as cool:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because it is so new, there has not yet been time to grow a community of users and resources outside of the official docs (which are great for a start). &lt;/li&gt;
&lt;li&gt;An opinionated framework like this is arguably great for beginners and/or developers who want to just go (which is one of the reasons the founder built RedwoodJS as heard in &lt;a href="https://changelog.com/jsparty/119"&gt;this pretty great episode&lt;/a&gt; of the Changelog JSParty podcast), but I also think there is a lot of value in going through and building and configuring things without a lot of frameworks (or any frameworks at all), especially at first so that people can understand how things all work together. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Questions I have:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why is the default in the tutorial to leave out semi-colons at the end of a line of javascript? Is this no longer considered best practice in Javascript? What have I missed?&lt;/li&gt;
&lt;li&gt;Why do the two default NotFoundPage.js and FatalErrorPage.js components have dangerouslySetInnerHTML? It doesn't seem so hard to me to just create the relevant content with jsx and a separate CSS/SCSS file as I'd do in any other React component?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, I had a lot of fun, and I will definitely try building something beyond the tutorial! Have you tried playing around with RedwoodJS? What did you think? Can you answer my questions? &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>redwoodjs</category>
      <category>react</category>
      <category>jamstack</category>
    </item>
    <item>
      <title>Improve your code: do yoga</title>
      <dc:creator>lmeromy</dc:creator>
      <pubDate>Fri, 13 Mar 2020 21:37:23 +0000</pubDate>
      <link>https://forem.com/lmeromy/improve-your-code-do-yoga-4l2i</link>
      <guid>https://forem.com/lmeromy/improve-your-code-do-yoga-4l2i</guid>
      <description>&lt;p&gt;&lt;em&gt;Credit and thanks to Katerina Limpitsouni for illustrations at &lt;a href="https://undraw.co/"&gt;https://undraw.co/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimers&lt;/strong&gt; &lt;br&gt;
&lt;em&gt;I acknowledge and am grateful for my two-fold privilege:&lt;/em&gt;&lt;br&gt;
  &lt;em&gt;1. I am physically able to do yoga, and&lt;/em&gt; &lt;br&gt;
  &lt;em&gt;2. I have a flexible work situation which allows for daily yoga&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Anyone who has tried it knows that coding is hard! It is a challenge for our minds as well as our bodies. It certainly challenges me on both fronts. So how can yoga help? &lt;/p&gt;

&lt;p&gt;Perhaps the more obvious benefits are the physical ones. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Get up! Step away from your screen!&lt;/strong&gt; &lt;br&gt;
We all have heard about how bad sitting in one position for hours on end can be for our health. So taking even a little bit of time to change your physical position from the traditional &lt;em&gt;shoulders-hunched-neck-forward-eyes-strained-face-scrunched&lt;/em&gt; is probably a good idea. There are many great poses you can do to counter the most common forms of back/hip/neck/wrist strain that occur when working at a computer all day. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Deeper breathing ==  health&lt;/strong&gt;&lt;br&gt;
If you are ever stressed (and who is never stressed?), taking a few moments for a couple deep breaths can reduce your heart rate, reduce your levels of cortisol, and help calm you down. Yoga practices often focus on breathing and linking your breath to movement. &lt;br&gt;
A healthier body is not necessary for writing good code, but it is certainly much more conducive to writing good code. &lt;/p&gt;

&lt;p&gt;The slightly less obvious ways yoga may benefit your code:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Give your brain a break&lt;/strong&gt; &lt;br&gt;
Hopefully most people do this already when spending lots of time and energy on problem-solving and hitting a wall. Often, when I let my brain switch off my problem du jour, I can come back to the problem a bit later refreshed instead of fatigued. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Yoga == self-care&lt;/strong&gt; &lt;br&gt;
Software engineering is a challenging profession. Sometimes the technical skills are difficult to learn/implement, or sometimes your work situation is challenging (resource issues, deadlines, stakeholder management, etc). Anything that you can do to help care for yourself will allow you to show up to do your best possible work. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Yoga facilitates mindfulness&lt;/strong&gt;&lt;br&gt;
 Software engineering (as well as many other challenging professions) has a tendency to brew up some serious imposter syndrome. If you can practice mindfulness, then ideally you can learn to allow the negativity that comes up to simply exist and let it go without making it mean anything bad about your skills or worth as a programmer (or as a human being). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tips for getting started&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start small. I mean really small if that is all you want or are able to do. Some ideas:       

&lt;ul&gt;
&lt;li&gt;30 seconds of deep breathing while sitting at your desk &lt;/li&gt;
&lt;li&gt;Stretch your neck, arms, and shoulders while sitting or standing at your desk
&lt;/li&gt;
&lt;li&gt;2 minute sun salutation next to your desk, in the bathroom, in an empty meeting room&lt;/li&gt;
&lt;li&gt;5 minutes of breathing and sun salutations &lt;/li&gt;
&lt;li&gt;block out a specific amount of time each day to do one or more of the above, even just a minute or two is a great start&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Slowly build up the habit one day at a time&lt;/li&gt;
&lt;li&gt;Be kind to yourself. Just because you don't manage to do 1 hour of power yoga every day doesn't mean you failed and that you should give up forever. Just start again with a small amount as soon as you are able. &lt;/li&gt;
&lt;li&gt;Try out a local yoga class&lt;/li&gt;
&lt;li&gt;Try a paid or free online yoga class&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I love Yoga With Adriene: &lt;a href="https://www.youtube.com/user/yogawithadriene"&gt;https://www.youtube.com/user/yogawithadriene&lt;/a&gt;. She is hilarious, beginner-friendly, and very inclusive. Also...she has hundreds of &lt;strong&gt;free&lt;/strong&gt; videos of all themes and lengths (5 minutes to 1+ hours). What a deal! &lt;/li&gt;
&lt;li&gt;Search the internet. There are tons of options. &lt;a href="https://www.thecut.com/2016/01/best-free-yoga-classes-online.html"&gt;https://www.thecut.com/2016/01/best-free-yoga-classes-online.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Share any other resources for doing yoga in the comments! &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>yoga</category>
      <category>impostersyndrome</category>
      <category>health</category>
    </item>
  </channel>
</rss>
