<?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: Nans Dumortier</title>
    <description>The latest articles on DEV Community by Nans Dumortier (@nans).</description>
    <link>https://dev.to/nans</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%2F71766%2Fcaa62b49-29b3-4d15-932b-f3166fa74ec6.png</url>
      <title>DEV Community: Nans Dumortier</title>
      <link>https://dev.to/nans</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nans"/>
    <language>en</language>
    <item>
      <title>Exploit Airtable inside your React app</title>
      <dc:creator>Nans Dumortier</dc:creator>
      <pubDate>Wed, 11 May 2022 16:57:09 +0000</pubDate>
      <link>https://dev.to/nans/exploit-airtable-inside-your-react-app-i3d</link>
      <guid>https://dev.to/nans/exploit-airtable-inside-your-react-app-i3d</guid>
      <description>&lt;p&gt;Cover photo by &lt;a href="https://unsplash.com/@jankolar?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Jan Antonin Kolar&lt;/a&gt; on Unsplash&lt;/p&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;At &lt;a href="https://www.karbon.earth/"&gt;Karbon 🌱&lt;/a&gt;, we iterate fast and we are always looking for ways to improve our products and services. &lt;/p&gt;

&lt;p&gt;We had the idea of building an idea box, allowing users to create, share and vote for ideas of new features.&lt;/p&gt;

&lt;p&gt;This is the typical project where we have to store unsensitive data entered by users. However we wanted to avoid impacting the rest of our application (e.g we don't want to change our database schema).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://airtable.com/"&gt;Airtable&lt;/a&gt; is a great tool for this kind of usecase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's build this idea box ! 🗳️
&lt;/h2&gt;

&lt;p&gt;Airtable provides an &lt;a href="https://www.npmjs.com/package/airtable"&gt;npm package&lt;/a&gt; to interact with their API.&lt;br&gt;
However, since I was working with multiple tables (one for votes, another one for ideas), I found myself repeating a lot of boilerplate code.&lt;br&gt;
I landed on a &lt;a href="https://www.npmjs.com/package/use-airtable"&gt;hook&lt;/a&gt; that seemed to provide a nice abstraction to interact with Airtable, but it had some bugs and the author didn't seem to willing to spend time on it. So I decided to write my own hook.&lt;/p&gt;

&lt;p&gt;On top of fixing a few issues, I worked on the following points : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;provide an option to handle errors when fetching, updating or deleting data&lt;/li&gt;
&lt;li&gt;provide an option to filter records using Airtable's &lt;code&gt;QueryParams&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Usage 👩‍💻👨‍💻
&lt;/h2&gt;

&lt;p&gt;Install the hook and Airtable's package with your favorite package manager, for example npm :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; airtable use-airtable-crud
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createRecord&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updateRecord&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteRecord&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getRecords&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useAirtable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TABLE_NAME&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;AIRTABLE_API_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TABLE_BASE_ID&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;filterByFormula&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{myField} = TRUE()&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&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;You can find the source code of the hook &lt;a href="https://github.com/NansD/use-airtable-crud"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Outro
&lt;/h2&gt;

&lt;p&gt;I hope this hook can be useful for you too! Prototyping and iterating is a great way to learn new things.&lt;/p&gt;

</description>
      <category>react</category>
      <category>npm</category>
      <category>database</category>
      <category>webdev</category>
    </item>
    <item>
      <title>An elegant solution for memory leaks in React</title>
      <dc:creator>Nans Dumortier</dc:creator>
      <pubDate>Thu, 30 Jul 2020 16:35:26 +0000</pubDate>
      <link>https://dev.to/nans/an-elegant-solution-for-memory-leaks-in-react-1hol</link>
      <guid>https://dev.to/nans/an-elegant-solution-for-memory-leaks-in-react-1hol</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Cover image from &lt;a href="https://unsplash.com/@thefreak1337" rel="noopener noreferrer"&gt;Valentin Petkov&lt;/a&gt; from &lt;a href="https://unsplash.com/" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  An elegant &lt;del&gt;solution&lt;/del&gt; for memory leaks in React
&lt;/h1&gt;




&lt;h2&gt;
  
  
  🔴 UPDATE
&lt;/h2&gt;

&lt;p&gt;This "solution" doesn't seem to &lt;em&gt;really&lt;/em&gt; avoid leaks, and I'm investigating for a true and elegant way to solve this. Even AbortController doesn't seem to be the silver bullet against memory leaks 😰.&lt;br&gt;
Check out the discussion in the comments!&lt;/p&gt;



&lt;p&gt;When working with asynchronous calls, for example API calls, you might have encountered this error :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Can't perform a React state update on an unmounted component. This is  a no-op, but it indicates a memory leak in your application. To fix,  cancel all subscriptions and asynchronous tasks in a useEffect cleanup  function.&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  A GIF is worth a thousand words ...
&lt;/h2&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%2Fi%2Fxrip6rz9yrvbwhbpe59e.gif" 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%2Fi%2Fxrip6rz9yrvbwhbpe59e.gif" alt="A GIF showing a component that is being unmounted while it was performing some asynchronous logic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a small page that simulates some asynchronous logic on load, and then updates the view accordingly. Here, I unmount the component before the async work has been finished, and trigger the Error. (I took this example from &lt;a href="https://stackoverflow.com/questions/56442582/react-hooks-cant-perform-a-react-state-update-on-an-unmounted-component/63176693#63176693" rel="noopener noreferrer"&gt;this&lt;/a&gt; StackOverFlow post)&lt;/p&gt;

&lt;p&gt;This is caused by this code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;waiting...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="nf"&gt;simulateSlowNetworkRequest&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&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="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;done!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ⚠️ what if the component is no longer mounted ?&lt;/span&gt;
      &lt;span class="c1"&gt;// =&amp;gt; Warning: Can't perform a React state update on an unmounted component.&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&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;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;When running into that issue, I found multiple solutions, the most used one seems to be this one :&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OtherExample&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;waiting...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isMounted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 👈&lt;/span&gt;
    &lt;span class="nf"&gt;simulateSlowNetworkRequest&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isMounted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// 👈&lt;/span&gt;
        &lt;span class="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;done!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// no more error&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return &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;isMounted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 👈&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&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;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;But&lt;/strong&gt; it requires you to add quite a lot of stuff into your component, having to deal with that &lt;code&gt;isMounted&lt;/code&gt; variable all over the place ...&lt;/p&gt;

&lt;p&gt;There are other interesting solutions, like making your Promises cancellable :&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/viclafouch" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F264616%2Fbb2fd5c3-b52c-49fd-962f-2e9e1f67c381.jpeg" alt="viclafouch"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/viclafouch/cancel-properly-http-requests-in-react-hooks-and-avoid-memory-leaks-pd7" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;🍦 Cancel Properly HTTP Requests in React Hooks and avoid Memory Leaks 🚨&lt;/h2&gt;
      &lt;h3&gt;Victor de la Fouchardière ・ Jul 29 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#showdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;h2&gt;
  
  
   You told me there would be an elegant solution !
&lt;/h2&gt;

&lt;p&gt;I wasn't lying! The solution I came up with is a very simple &lt;a href="https://www.npmjs.com/package/use-state-if-mounted" rel="noopener noreferrer"&gt;hook&lt;/a&gt;. It works just like React's useState, but it basically checks if the component is mounted before updating the state !&lt;/p&gt;

&lt;p&gt;Here is an example of the refactored code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OtherExample&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useStateIfMounted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;waiting...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 👈&lt;/span&gt;

  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useEffect&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="nf"&gt;simulateSlowNetworkRequest&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&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="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;done!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// no more error&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&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;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the &lt;a href="https://codesandbox.io/s/use-local-variable-to-avoid-setstate-on-unmouted-component-6k08n?file=/src/index.js:459-695" rel="noopener noreferrer"&gt;CodeSandBox&lt;/a&gt; if you wanna play around !&lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR
&lt;/h2&gt;

&lt;p&gt;Use &lt;a href="https://www.npmjs.com/package/use-state-if-mounted" rel="noopener noreferrer"&gt;useStateIfMounted&lt;/a&gt; hook, that will only update the state if your component is mounted ! 🚀&lt;/p&gt;

&lt;p&gt;I hope this might be helpful, feel free to reach me out in any case ! 🤗&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>hooks</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Struggle with React and unit tests</title>
      <dc:creator>Nans Dumortier</dc:creator>
      <pubDate>Mon, 23 Mar 2020 18:21:09 +0000</pubDate>
      <link>https://dev.to/nans/struggle-with-react-and-unit-tests-5dl7</link>
      <guid>https://dev.to/nans/struggle-with-react-and-unit-tests-5dl7</guid>
      <description>&lt;h2&gt;
  
  
   Context
&lt;/h2&gt;

&lt;p&gt;I'm trying to learn TDD at the moment, and so I decided to exercise on my &lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby&lt;/a&gt; project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/NansD/nans-random-thoughts"&gt;This&lt;/a&gt; is the blog I'm working on.&lt;/p&gt;

&lt;h2&gt;
  
  
   What do I want to achieve ?
&lt;/h2&gt;

&lt;p&gt;I wanted to add a theme toggler to it, simply by using &lt;a href="https://www.gatsbyjs.org/packages/gatsby-plugin-dark-mode/"&gt;gatsby-plugin-dark-mode&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;So I went ahead and created a &lt;code&gt;theme-toggle.js&lt;/code&gt; along with a &lt;code&gt;theme-toggle.test.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt; Then, I decided to write my first test. But for that, I had to ...&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup tests on a Gatsby Project
&lt;/h2&gt;

&lt;p&gt;I followed &lt;a href="https://www.gatsbyjs.org/docs/unit-testing/"&gt;this tutorial&lt;/a&gt; which is well made, along with &lt;a href="https://www.gatsbyjs.org/docs/testing-react-components"&gt;this one&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Now I can run &lt;code&gt;npm run test -- --watch&lt;/code&gt; and everything seems to be working.&lt;/p&gt;

&lt;p&gt;Here I am, with &lt;code&gt;jest&lt;/code&gt;, &lt;code&gt;@testing-library/react&lt;/code&gt; and everything ready to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now, what should I do ?
&lt;/h2&gt;

&lt;p&gt;I've been able to create a simple React component that contains a label with a check box, and successfully tested it with :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ThemeToggle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&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;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;renders an input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;themeToggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ThemeToggle&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;themeToggle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByLabelText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dark mode&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toBeNull&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// this passes ✅&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;But then, I "cheated" for a bit, and looked at &lt;a href="https://www.gatsbyjs.org/packages/gatsby-plugin-dark-mode/"&gt;the tutorial&lt;/a&gt; to see what I had to build to make it work.&lt;/p&gt;

&lt;p&gt;Here is how it should look like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ThemeToggle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ThemeToggler&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggleTheme&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt; &lt;span class="na"&gt;htmlFor&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"themeToggle"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"themeToggle"&lt;/span&gt;
            &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt;
            &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;toggleTheme&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checked&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;checked&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          Dark mode
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ThemeToggler&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;So now my question is : how do I write suitable tests for that component ?&lt;/p&gt;

&lt;p&gt;I'm a bit lost 😅&lt;/p&gt;

&lt;p&gt;Thank you for your help !&lt;/p&gt;

</description>
      <category>unittests</category>
      <category>react</category>
      <category>help</category>
      <category>tdd</category>
    </item>
    <item>
      <title>Nevertheless, my team mate Coded</title>
      <dc:creator>Nans Dumortier</dc:creator>
      <pubDate>Fri, 06 Mar 2020 20:35:37 +0000</pubDate>
      <link>https://dev.to/nans/nevertheless-my-team-mate-coded-1omh</link>
      <guid>https://dev.to/nans/nevertheless-my-team-mate-coded-1omh</guid>
      <description>&lt;h2&gt;
  
  
  I'm lucky because I have a great team mate !
&lt;/h2&gt;

&lt;p&gt;Despite an extremely stressful context and a solid imposter syndrome, she coded. She joined the team 1 year ago, and has made great progress although the project was tense.&lt;br&gt;
She's started to express herself more and more, and she still sometimes encounters some setbacks.&lt;br&gt;
I admire her for her perseverance and for the great mood she brings to the team.&lt;br&gt;
Even though the management did not trust her, she has solved some extremely complex problems !&lt;/p&gt;

&lt;p&gt;I'm trying my best to support her in tough situations, and we have great time pair programming. &lt;/p&gt;

&lt;h2&gt;
  
  
  My advice for allies to support self-identifying women and non-binary folks who code is ...
&lt;/h2&gt;

&lt;p&gt;Try and be helpful. On tough problems, simply propose to give a hand, and be comprehensive when the person is feeling insecure.&lt;br&gt;
Do not hesitate to compliment your team mates if you feel so, and ensure they have a healthy work atmosphere.&lt;/p&gt;

</description>
      <category>wecoded</category>
    </item>
    <item>
      <title>I'm Nans Dumortier, and I support women devs.</title>
      <dc:creator>Nans Dumortier</dc:creator>
      <pubDate>Tue, 05 Mar 2019 17:53:45 +0000</pubDate>
      <link>https://dev.to/nans/im-nans-dumortier-and-i-support-women-devs-3db8</link>
      <guid>https://dev.to/nans/im-nans-dumortier-and-i-support-women-devs-3db8</guid>
      <description>&lt;h2&gt;
  
  
  I will advocate for gender equality by...
&lt;/h2&gt;

&lt;p&gt;Doing my best to be a good teammate, being respectful to anyone !&lt;/p&gt;

&lt;h2&gt;
  
  
  I hope to see my school/work/developer/tech community...
&lt;/h2&gt;

&lt;p&gt;Having more and more "gender-balanced" teams. People with different backgrounds can bring a lot to a project, and can teach a lot to each other !&lt;/p&gt;

</description>
      <category>wecoded</category>
    </item>
    <item>
      <title>What should we, developers, tell our relatives about ?</title>
      <dc:creator>Nans Dumortier</dc:creator>
      <pubDate>Sun, 10 Feb 2019 18:19:53 +0000</pubDate>
      <link>https://dev.to/nans/what-should-we-developers-tell-our-relatives-about--2f8p</link>
      <guid>https://dev.to/nans/what-should-we-developers-tell-our-relatives-about--2f8p</guid>
      <description>&lt;p&gt;As a web/mobile developer, I feel like my job is very obscure to most of my relatives. &lt;br&gt;
On the other hand, it seems to me that some technical knowledge might be useful for anyone, to have a better understanding of the web in general.&lt;br&gt;
&lt;strong&gt;What should we, developers, teach our relatives about ?&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
