<?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: Christopher Evans</title>
    <description>The latest articles on DEV Community by Christopher Evans (@crevulus).</description>
    <link>https://dev.to/crevulus</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%2F569589%2F16b1db2f-660e-4555-9047-f4f72d160a86.jpeg</url>
      <title>DEV Community: Christopher Evans</title>
      <link>https://dev.to/crevulus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/crevulus"/>
    <language>en</language>
    <item>
      <title>Is it bad practice to make a POST call in useEffect?</title>
      <dc:creator>Christopher Evans</dc:creator>
      <pubDate>Fri, 29 Jan 2021 09:54:18 +0000</pubDate>
      <link>https://dev.to/crevulus/is-it-bad-practice-to-make-a-post-call-in-useeffect-1cg5</link>
      <guid>https://dev.to/crevulus/is-it-bad-practice-to-make-a-post-call-in-useeffect-1cg5</guid>
      <description>&lt;p&gt;Hi all,&lt;/p&gt;

&lt;p&gt;I am using &lt;code&gt;react-cropper&lt;/code&gt; for my users to upload/edit pictures, but when I try to console log the resulting &lt;code&gt;photoData&lt;/code&gt; in my &lt;code&gt;setPhotoData&lt;/code&gt; fn, the console log is one state behind. i.e. If I hit submit &amp;amp; upload and crop photo A, when &lt;code&gt;setPhotoData&lt;/code&gt; runs it'll log an empty line in the console. Then when I do photo B, I'll get a console log of photo A's data. Uploading photo C will console log photo B's data and so on.&lt;/p&gt;

&lt;p&gt;In a class component, you can add a callback after &lt;code&gt;setState&lt;/code&gt;. So if I were using a class component I'd just make the &lt;code&gt;POST&lt;/code&gt; there, after the state has been set. But apparently with react hooks you can't do that; my IDE gave me a warning and told me to use &lt;code&gt;useEffect&lt;/code&gt; instead. &lt;/p&gt;

&lt;p&gt;So now in &lt;code&gt;useEffect&lt;/code&gt; I'm checking if &lt;code&gt;photoData&lt;/code&gt; is populated in my state and then console logging once it is. This is working just fine for my little dev env: Nothing is logged until I hit submit, and then with the re-render that's triggered by the state change I get one console log with the correct cropped picture. &lt;/p&gt;

&lt;p&gt;But I'm concerned that in production this is going to cause some kind of loop or leak. Even if I'm checking for defined state before I make the call, is there some potential for this to go wrong that I'm overlooking? It would similarly feel strange to do a &lt;code&gt;POST&lt;/code&gt; in &lt;code&gt;componentDidMount&lt;/code&gt;, so that's why I'm suspicious.&lt;/p&gt;

&lt;p&gt;Here's the relevant code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const CropperWidget = () =&amp;gt; {
  const [cropData, setCropData] = useState("");
  const [cropper, setCropper] = useState();

  const onChange = (e) =&amp;gt; {
  // there's a fn to upload the file before cropping here
  }

  const getCropData = () =&amp;gt; {
    if (typeof cropper !== "undefined") {
      setCropData(cropper.getCroppedCanvas().toDataURL());
    }
  };

  useEffect(() =&amp;gt; {
    if (cropData) {
      console.log(cropData);
    }
  });

  return (
    // various irrelevant jsx
    &amp;lt;button onClick={getCropData}&amp;gt;Crop Image&amp;lt;/button&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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