<?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: Carlos</title>
    <description>The latest articles on DEV Community by Carlos (@cmermingas).</description>
    <link>https://dev.to/cmermingas</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%2F1037992%2F85eeb2c1-48f6-427b-974b-8d817e22cb1d.jpeg</url>
      <title>DEV Community: Carlos</title>
      <link>https://dev.to/cmermingas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cmermingas"/>
    <language>en</language>
    <item>
      <title>Using EventEmitter in React</title>
      <dc:creator>Carlos</dc:creator>
      <pubDate>Fri, 03 Mar 2023 17:32:18 +0000</pubDate>
      <link>https://dev.to/cmermingas/using-eventemitter-in-react-21p9</link>
      <guid>https://dev.to/cmermingas/using-eventemitter-in-react-21p9</guid>
      <description>&lt;p&gt;I have an issue where handlers subscribed to a custom event see all the updated state (from calls to &lt;code&gt;setSomething()&lt;/code&gt;) &lt;strong&gt;except the last one that was set&lt;/strong&gt;. Specifically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [counter1, setCounter1] = useState(1);
const [counter2, setCounter2] = useState(1);

const increment = useCallback(async () =&amp;gt; {
  events.emit('beforeIncrement');

  await someAsyncAction();  // &amp;lt;-- There's an async call.

  setCounter1((x: number) =&amp;gt; x + 1);
  setCounter2((x: number) =&amp;gt; x + 1);

  // React 17:
  //    Handlers of the following event will see counter1 updated value but not counter2.
  // React 18 with Automatic Batching:
  //    Handlers of the following event will not see see counter1 or counter2 updated value.
  // What is the best way to emit an event here and ensure that handlers see the updated state?
  //    setTimeout(() =&amp;gt; events.emit('afterIncrement')) ??

  events.emit('afterIncrement');
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following repo demonstrates this behavior:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/cmermingas/event-emitter-react-17"&gt;https://github.com/cmermingas/event-emitter-react-17&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why is this happening?&lt;/li&gt;
&lt;li&gt;What is the right way to emit an event to ensure that handlers have access to all the updated state?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks in advance!&lt;/p&gt;

&lt;p&gt;[edit]&lt;/p&gt;

&lt;p&gt;The example I provided is a simplified version of the real situation. A more generic description would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const doWork = useCallback(async () =&amp;gt; {
  events.emit('beforeWork');

  // Asynchronous calls where React state gets updated.

  events.emit('afterWork');
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Asynchronous calls&lt;/code&gt; section triggers state updates, possibly outside of the body of this callback.&lt;/p&gt;

&lt;p&gt;I want all &lt;code&gt;afterWork&lt;/code&gt; handlers to have the most up-to-date state from React. I imagine an API like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const doWork = useCallback(async () =&amp;gt; {
  events.emit('beforeWork');

  // Asynchronous calls / Promise chains here
  // where React state gets updated.

  // Work is done. When React has updated, emit an event:

  afterReactWork(() =&amp;gt; events.emit('afterWork'));
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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