<?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: Aymen Sakouhi</title>
    <description>The latest articles on DEV Community by Aymen Sakouhi (@aymensakouhi).</description>
    <link>https://dev.to/aymensakouhi</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%2F1090608%2Fa7bdf24f-8d2d-49d4-935c-ef33aa966259.jpeg</url>
      <title>DEV Community: Aymen Sakouhi</title>
      <link>https://dev.to/aymensakouhi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aymensakouhi"/>
    <language>en</language>
    <item>
      <title>Jotai: Simplifying State Management in React Applications</title>
      <dc:creator>Aymen Sakouhi</dc:creator>
      <pubDate>Wed, 28 Jun 2023 09:04:50 +0000</pubDate>
      <link>https://dev.to/aymensakouhi/jotai-simplifying-state-management-in-react-applications-1k3o</link>
      <guid>https://dev.to/aymensakouhi/jotai-simplifying-state-management-in-react-applications-1k3o</guid>
      <description>&lt;p&gt;Let's start with definition of Jotai and what does it do?&lt;br&gt;
&lt;u&gt;Jotai is a state management library for React applications. It provides a simple and scalable approach to managing state in your React components. Jotai is built on the concept of atoms, which are individual units of state that can be shared and composed to create complex application states.&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;To understand Jotai, let's start by explaining what state management is in the context of React?&lt;br&gt;
State management involves managing the data that is used by components to render and respond to user interactions. &lt;br&gt;
In React, state is typically managed using the built-in useState hook or other external libraries like Redux or MobX. However, as applications grow larger and more complex, managing state becomes challenging, especially when it needs to be shared between multiple components.&lt;/p&gt;

&lt;p&gt;This is where Jotai comes in:&lt;br&gt;
Jotai introduces the concept of atoms, which are similar to React's useState but can be shared and accessed from multiple components without the need for prop drilling or complex Redux setups. Atoms are created using the atom function provided by Jotai. Here's an example:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;atom&lt;/span&gt; &lt;span class="p"&gt;}&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;jotai&lt;/span&gt;&lt;span class="dl"&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;countAtom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;atom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, countAtom is an atom that holds the value 0. Any component can read and update this atom using Jotai's useAtom hook:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useAtom&lt;/span&gt; &lt;span class="p"&gt;}&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;jotai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Counter&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useAtom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;countAtom&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;increment&lt;/span&gt; &lt;span class="o"&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;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prevCount&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;prevCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&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;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&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;Increment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;With Jotai, the state stored in atoms can be modified using the setCount function, which works similarly to setState in useState. The difference is that multiple components can access and update the same atom without any complex state management setup.&lt;/p&gt;

&lt;p&gt;Jotai also provides additional features like derived atoms, which are atoms that compute their value based on other atoms. This allows for easy composition of atoms to build more complex states. Jotai's atoms are also fully reactive, meaning that any change to an atom triggers a re-render of all components that depend on that atom.&lt;/p&gt;

&lt;p&gt;One of the key benefits of Jotai is its simplicity and lightweight nature. Unlike other state management libraries, Jotai has a minimal API surface and relies on React's built-in mechanisms for reactivity. It offers a more straightforward and intuitive approach to state management, especially for smaller to medium-sized applications.&lt;/p&gt;

&lt;p&gt;Jotai is an open-source library and is actively maintained by its community. It has gained popularity among developers for its simplicity and performance. To get started with Jotai, you can install it via npm or yarn and explore the official documentation and examples available on the &lt;a href="https://jotai.org/docs/introduction"&gt;Jotai&lt;/a&gt; website.&lt;/p&gt;

&lt;p&gt;In conclusion, Jotai is a state management library for React that provides a simple and scalable approach to managing state in your applications. By introducing the concept of atoms, Jotai simplifies the sharing and composition of state across components. With its lightweight and intuitive API, Jotai offers a refreshing alternative for state management in React applications.&lt;/p&gt;

</description>
      <category>atom</category>
      <category>react</category>
      <category>states</category>
      <category>provider</category>
    </item>
    <item>
      <title>React useEffect() wonders that you should know!</title>
      <dc:creator>Aymen Sakouhi</dc:creator>
      <pubDate>Tue, 13 Jun 2023 12:08:21 +0000</pubDate>
      <link>https://dev.to/aymensakouhi/react-useeffect-wonders-1gdg</link>
      <guid>https://dev.to/aymensakouhi/react-useeffect-wonders-1gdg</guid>
      <description>&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; is a built-in hook in React that allows you to perform side effects in your functional components. It's a fundamental hook that is used extensively in React applications. Here are some important details about &lt;code&gt;useEffect&lt;/code&gt; that everybody should know:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Purpose: &lt;code&gt;useEffect&lt;/code&gt; is used for handling side effects such as data fetching, subscriptions, or manually manipulating the DOM. It enables you to perform these operations after the component has rendered or when certain dependencies have changed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Syntax: The &lt;code&gt;useEffect&lt;/code&gt; hook takes two arguments: a callback function and an optional array of dependencies. The callback function contains the side effect logic, while the dependency array specifies which values the effect depends on.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;   &lt;span class="nx"&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="c1"&gt;// Side effect logic&lt;/span&gt;
     &lt;span class="c1"&gt;// ...&lt;/span&gt;

     &lt;span class="c1"&gt;// Cleanup logic (optional)&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="c1"&gt;// Cleanup code&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="nx"&gt;dependency1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dependency2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Execution Timing: By default, &lt;code&gt;useEffect&lt;/code&gt; runs after every render of the component. This means it will run on the initial render and subsequent re-renders. You can control the execution of the effect by specifying dependencies. If the dependencies array is empty, the effect will only run once, after the initial render.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dependency Array: The dependency array determines when the effect should be re-evaluated. If any value in the dependency array changes between renders, the effect will be re-executed. If the dependency array is omitted, the effect will run on every render.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cleanup Function: If the effect requires any cleanup, such as canceling subscriptions or removing event listeners, you can return a cleanup function from the effect's callback. The cleanup function will be invoked before the component unmounts or before the effect is re-executed. This ensures that resources are properly released.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dependencies and Equality: React determines whether the dependencies have changed by performing a shallow comparison (using &lt;code&gt;Object.is&lt;/code&gt;) between the current and previous values. It's important to note that if the dependencies contain objects or arrays, you should ensure they are referentially stable or use proper techniques like immutability to avoid unintentional re-renders.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiple Effects: You can use multiple &lt;code&gt;useEffect&lt;/code&gt; hooks in a single component to separate concerns and keep the code organized. Each effect operates independently of others.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asynchronous Effects: If you need to perform asynchronous operations within the effect, you can mark the callback function as &lt;code&gt;async&lt;/code&gt; and use &lt;code&gt;await&lt;/code&gt; for promises. However, you should be aware that returning a promise from an effect does not cancel the effect. You may need additional cleanup logic to handle such scenarios.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Warning for Missing Dependencies: If you omit dependencies in the dependency array, React will display a warning in the console. It's important to provide all the dependencies that the effect relies on to ensure correct behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Best Practices: To write efficient and predictable effects, consider the following best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep the effect's logic focused and self-contained.&lt;/li&gt;
&lt;li&gt;Avoid unnecessary re-execution of the effect by providing accurate dependencies.&lt;/li&gt;
&lt;li&gt;Perform any cleanup to prevent memory leaks and resource waste.&lt;/li&gt;
&lt;li&gt;Understand the order in which effects are executed by considering their dependencies.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By understanding and effectively using the &lt;code&gt;useEffect&lt;/code&gt; hook, you can handle various side effects in your React components while adhering to the declarative nature of React's programming model.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
