<?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: Moustafa Mahmoud</title>
    <description>The latest articles on DEV Community by Moustafa Mahmoud (@devmoustafa97).</description>
    <link>https://dev.to/devmoustafa97</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%2F345638%2F5322827e-c9f9-40b6-91fa-e05e9888a066.jpg</url>
      <title>DEV Community: Moustafa Mahmoud</title>
      <link>https://dev.to/devmoustafa97</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmoustafa97"/>
    <language>en</language>
    <item>
      <title>Do you know unstable_batchedUpdates in React ? (enforce batching state update)</title>
      <dc:creator>Moustafa Mahmoud</dc:creator>
      <pubDate>Mon, 21 Mar 2022 01:59:03 +0000</pubDate>
      <link>https://dev.to/devmoustafa97/do-you-know-unstablebatchedupdates-in-react-enforce-batching-state-update-5cn2</link>
      <guid>https://dev.to/devmoustafa97/do-you-know-unstablebatchedupdates-in-react-enforce-batching-state-update-5cn2</guid>
      <description>&lt;p&gt;It is ok if this is your first time to read about it, because it is one of the &lt;strong&gt;not documented&lt;/strong&gt; APIs in React, Yes you can not find it in the react's official documentation, although it is a part of the library.&lt;/p&gt;

&lt;p&gt;First we need to know &lt;strong&gt;What is batching state update?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;According to &lt;a href="//www.twitter.com/dan_abramov"&gt;Dan Abramov&lt;/a&gt; -co-authored the Create React App&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Batching is when React groups multiple state updates into a single re-render for better performance.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [count, setCount] = useState(0);
  const [isEven, setIsEven] = useState(false);

  function handleClick() {
    setCount(c =&amp;gt; c + 1); // Does not re-render yet
    setIsEven(f =&amp;gt; !f); // Does not re-render yet
    // React will only re-render once at the end (that's batching!)
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;counter{count}&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In earlier React versions (17 and earlier) there was batching only in browser event (like click), and it wasn't available in &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native DOM event listeners (e.g., el.addEventListener())&lt;/li&gt;
&lt;li&gt;Async callbacks (e.g., promises, timeouts, intervals)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and to make it available you had to use &lt;code&gt;unstable_batchedUpdates&lt;/code&gt; to enforce batching&lt;/p&gt;

&lt;p&gt;How to use it &lt;code&gt;unstable_batchedUpdates&lt;/code&gt; ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {unstable_batchedUpdates} from 'react-dom';

const batchUpdate = unstable_batchedUpdates(() =&amp;gt; {
  setName('Moustafa');
  setAge(25);
});


batchUpdate() //this will group all state changes inside and apply it in one re-render 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Good News&lt;/strong&gt;&lt;br&gt;
if you're using React 18 and above, you do not need it anymore because React 18 now support automatic batching.&lt;/p&gt;

&lt;p&gt;This means that updates inside of timeouts, promises, native event handlers or any other event will batch the same way as updates inside of React events&lt;/p&gt;

&lt;p&gt;you can read more about it &lt;a href="https://dev.to/jackbuchananconroy/react-18-what-s-changed-automatic-batching-13ec"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Warning ⚠️
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Since React 18 now supports automatic batching and it is &lt;br&gt;
a not documented API and it might get removed in a future major version of React after popular libraries no longer depend on its existence.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to share a single global object across your app (Singleton design pattern)</title>
      <dc:creator>Moustafa Mahmoud</dc:creator>
      <pubDate>Sun, 20 Mar 2022 01:48:29 +0000</pubDate>
      <link>https://dev.to/devmoustafa97/how-to-share-single-global-object-across-your-app-singleton-design-pattern-50hd</link>
      <guid>https://dev.to/devmoustafa97/how-to-share-single-global-object-across-your-app-singleton-design-pattern-50hd</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PU3uzk7g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65izrbt4fgi8z1zutc9m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PU3uzk7g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65izrbt4fgi8z1zutc9m.jpg" alt="Image description" width="880" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the most important features in OOP is you can create a single class and instantiate instances of it as many as you want.&lt;/p&gt;

&lt;p&gt;but what if you need to create only a single object to deal with or make sure your class will be instantiated once ?&lt;/p&gt;

&lt;p&gt;At this point implementing a Singleton will be a good choice.&lt;br&gt;
and the singleton can represent the following objects&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database Connection Object&lt;/li&gt;
&lt;li&gt;Current Logged-in user Object&lt;/li&gt;
&lt;li&gt;Provisioning an OTP Object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;or any kind of objects that you need to make sure there is only one instance of it in your app &lt;/p&gt;

&lt;h2&gt;
  
  
  How to implement
&lt;/h2&gt;

&lt;p&gt;let's say we wanna create a counter and that counter will be available throughout your app.&lt;/p&gt;

&lt;p&gt;Since in js we can create objects directly without creating a class first, &lt;/p&gt;

&lt;h3&gt;
  
  
  1- Create a variable that holds the current value of the counter and assign it an initial value
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;let currentValue = 0&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2- Create an object to change the value with it
&lt;/h3&gt;

&lt;p&gt;Since in js we can create objects directly without creating a class first, a simple &lt;code&gt;counter&lt;/code&gt; object well be enough to implement a singleton&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let currentValue = 0&lt;br&gt;
const counter = {

&lt;p&gt;increment(){&lt;br&gt;
  return ++currentValue&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;decrement(){&lt;br&gt;
  return --currentValue&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  3- Prevent modifying the &lt;code&gt;counter&lt;/code&gt; object and export it&lt;br&gt;
&lt;/h3&gt;
&lt;br&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let currentValue = 0&lt;br&gt;
const counter = {

&lt;p&gt;increment(){&lt;br&gt;
  return ++currentValue&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;decrement(){&lt;br&gt;
  return --currentValue&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
Object.freeze( counter )&lt;br&gt;
export { counter }&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  notes&lt;br&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We use const with counter object (the Singleton) to prevent reassigning it by mistake, also we freeze it to prevent adding or removing attributes or changing the value of one of them.&lt;/li&gt;
&lt;li&gt;Since objects are passed by reference, you'll get the same instance every time you import it in another file&lt;/li&gt;
&lt;li&gt;You can create more methods based on your requirements in your app, like creating a getter method to get the counter value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;hope that article answered your question and see you next time,&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>designpattern</category>
    </item>
  </channel>
</rss>
