<?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: Ahmed Arafa</title>
    <description>The latest articles on DEV Community by Ahmed Arafa (@ahmed_aarafa).</description>
    <link>https://dev.to/ahmed_aarafa</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%2F1072124%2Fb541a699-64f2-4891-a0a9-74ca522232de.jpg</url>
      <title>DEV Community: Ahmed Arafa</title>
      <link>https://dev.to/ahmed_aarafa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmed_aarafa"/>
    <language>en</language>
    <item>
      <title>Your Guide to Use React 18 Hooks useTransition and useDeferredValue</title>
      <dc:creator>Ahmed Arafa</dc:creator>
      <pubDate>Wed, 03 May 2023 07:22:47 +0000</pubDate>
      <link>https://dev.to/ahmed_aarafa/your-guide-to-use-react-18-hooks-usetransition-and-usedeferredvalue-52d1</link>
      <guid>https://dev.to/ahmed_aarafa/your-guide-to-use-react-18-hooks-usetransition-and-usedeferredvalue-52d1</guid>
      <description>&lt;p&gt;In this post, we will demonstrate what are react 18 hooks useTransition and useDeferredValue and how can we use them to improve our app performance.&lt;/p&gt;

&lt;p&gt;The version 18 of React was released a few months ago, which means that we are not breaking new ground, reading and exploring what's new in the updated version is good but what's much better to involve it in a practical application.&lt;/p&gt;

&lt;p&gt;So let's begin our journey.&lt;/p&gt;

&lt;p&gt;Assume you have a list of employees in your database and you make API requests to filter those employees by name.&lt;br&gt;
If the number of employees is huge and the data is not returned paginated from the server-side or the server capability is limited or any other reason, You may run into trouble. This can result in the app appearing sluggish when filtering employees.&lt;/p&gt;

&lt;p&gt;let's mockup this process.&lt;/p&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%2Fuploads%2Farticles%2F92zmdvadilpa0bu282ec.png" 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%2Fuploads%2Farticles%2F92zmdvadilpa0bu282ec.png" alt="Implement input to filter employees"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we have implemented an input that takes the name of an employee to display it inside the input box and to filter through the employees' array at the same time. &lt;br&gt;
The result of this filtering process is unsatisfactory. It takes a long time to display the characters in the search box, as well as to display the results. This delay becomes even more obvious when typing many characters simultaneously, The process can take multiple seconds, which is a significant amount of time.&lt;/p&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%2Fuploads%2Farticles%2Fdziz08dgxo2zici5dqr2.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%2Fuploads%2Farticles%2Fdziz08dgxo2zici5dqr2.gif" alt="Filtering employees without useTransition"&gt;&lt;/a&gt;&lt;br&gt;
⚠️if the GIF not loaded view it from &lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1kueyx5sgyeg311ak6a.gif" rel="noopener noreferrer"&gt;HERE&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useTransition()
&lt;/h2&gt;

&lt;p&gt;This is where useTransition() comes into play.&lt;br&gt;
&lt;strong&gt;useTransition()&lt;/strong&gt; can be used to tell React that certain state updates have a lower priority (i.e., all other state updates or UI rendering triggers have a higher priority).&lt;br&gt;
These state updates will be executed in parallel with other state updates, but the rendering of the component will not wait for these less important state updates.&lt;/p&gt;

&lt;p&gt;When calling useTransition(), you get back an array with exactly two elements: An isPending boolean value, telling you whether the low-priority state update is still pending (we can use it to show a spinner while data is processing), and a startTransition() function that can be wrapped around a state update to tell React, that it is a low-priority update.&lt;/p&gt;

&lt;p&gt;To make the app more responsive, we need to set a lower priority for listing the employees so that it doesn't delay the display of characters in the search box.&lt;br&gt;
So We will wrap the setState of the employees array "setEmployeesList" in startTransition() callback function as follows:&lt;/p&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%2Fuploads%2Farticles%2Fs82ytsj2fqzdliaciex4.png" 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%2Fuploads%2Farticles%2Fs82ytsj2fqzdliaciex4.png" alt="Using useTransition to low-prioritize listing employees"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we are telling React that "setEmployeesList" is a low-priority state. This is to ensure that it does not cause any delay to other states, including the UI rendering triggers.&lt;/p&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%2Fuploads%2Farticles%2Fky67pentxhd2izyllztw.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%2Fuploads%2Farticles%2Fky67pentxhd2izyllztw.gif" alt="Filtering employees with useTransition"&gt;&lt;/a&gt;&lt;br&gt;
⚠️if the GIF not loaded view it from &lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ine8c8ugrgut0sxtxup0.gif" rel="noopener noreferrer"&gt;HERE&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useDeferredValue()
&lt;/h2&gt;

&lt;p&gt;We can achieve the same result using useDeferredValue.&lt;br&gt;
The useDeferredValue hook allows us to fix this slow render problem by implementing a delay before some information is calculated.&lt;br&gt;
So that our deferred value will only be calculated after the important state updates have finished running.&lt;br&gt;
However, the difference is that useDeferredValue() is used to wrap values (states), not setter functions (setStates). If you have a component that takes a prop from a parent component or accesses global state (where you don't have control of the state and only access the value), then useDeferredValue() can be used in this scenario.&lt;/p&gt;

&lt;p&gt;for example:&lt;br&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%2Fuploads%2Farticles%2Fx8on2yq5iwnkou9ruo8a.png" 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%2Fuploads%2Farticles%2Fx8on2yq5iwnkou9ruo8a.png" alt="Using useDeferredValue to low-prioritize listing employees"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Both of &lt;strong&gt;useTransition&lt;/strong&gt; and &lt;strong&gt;useDeferredValue&lt;/strong&gt; are using to low-prioritize some updates.&lt;br&gt;
The difference is that useTransition() wraps the state updating code, whilst useDeferredValue() wraps a value that's affected by the state update. You don't need to (and shouldn't) use both together, since they achieve the same goal in the end.&lt;/p&gt;

&lt;p&gt;Instead, it makes sense to prefer useTransition(), if you have some state update that should be treated with a lower priority and you have access to the state updating code. If you don't have that access or you're accessing third-party library for example, use useDeferredValue().&lt;/p&gt;

&lt;p&gt;Feel free to write your thoughts in the comments section👇🏼&lt;br&gt;
Have a Good day.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Real-World Uses for :has() CSS Selector</title>
      <dc:creator>Ahmed Arafa</dc:creator>
      <pubDate>Thu, 27 Apr 2023 08:08:30 +0000</pubDate>
      <link>https://dev.to/ahmed_aarafa/real-world-uses-for-has-css-selector-344m</link>
      <guid>https://dev.to/ahmed_aarafa/real-world-uses-for-has-css-selector-344m</guid>
      <description>&lt;p&gt;In this post, we'll take a look at :has() CSS selector real-world uses and how it can make your code better by reducing the amount of code you need to write.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Case:
&lt;/h2&gt;

&lt;p&gt;Have you ever created an interactive component that requires the ability to modify styles elsewhere on the page? &lt;br&gt;
Suppose that we have a navbar with navigation items. Upon clicking a navigation item, a dropdown menu is displayed containing the relevant information.&lt;/p&gt;

&lt;p&gt;Let's create it.&lt;/p&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%2Fuploads%2Farticles%2Fo7wpe3r4xly5xt7kd1mw.jpeg" 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%2Fuploads%2Farticles%2Fo7wpe3r4xly5xt7kd1mw.jpeg" alt="Navbar implementation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is a common practice to modify the style of the navbar (e.g., change its height, color, opacity, etc...) and change the dropdown chevron related to the navigation item from "⌄" to "⌃" when displaying a dropdown.&lt;/p&gt;

&lt;p&gt;I feel like I need to do this kind of stuff all the time.&lt;/p&gt;

&lt;p&gt;As we know, It is not possible to directly modify the style of a parent selector from a child selector. Therefore, the old approach was to make this effect using JavaScript, as follows 👇&lt;/p&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%2Fuploads%2Farticles%2F0i3hw7wyag7hddbl663f.jpeg" 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%2Fuploads%2Farticles%2F0i3hw7wyag7hddbl663f.jpeg" alt="Nabar color change using JavaScript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Within the function that toggles the dropdown "toggleDropDownMenu" we get the navbar element and change its color based on the dropdown's current state.&lt;/p&gt;

&lt;p&gt;Whenever I implement something like this, I often find myself wondering if is it worth this hassle to just change color or height or whatever kind of style? &lt;/p&gt;

&lt;p&gt;So, let's do some magic✨&lt;/p&gt;

&lt;p&gt;We will modify the navbar styles-set to be as follows:&lt;/p&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%2Fuploads%2Farticles%2Fn7j3rlllp9gi2426fkn7.jpeg" 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%2Fuploads%2Farticles%2Fn7j3rlllp9gi2426fkn7.jpeg" alt="Altering navbar color from scss"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We utilized the :has() selector to verify whether the "nav" contains an element with both the class names "dropdown" and "show" within its descendants. If such an element exists, it indicates that the dropdown is currently displayed. Consequently, the color of the navbar will be changed.&lt;/p&gt;

&lt;p&gt;Here is the codepen for this case:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Ahmed_Arafa/embed/LYgyzWG?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Second Case:
&lt;/h2&gt;

&lt;p&gt;Assume we have a table of companies with their contacts and locations as follows:&lt;/p&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%2Fuploads%2Farticles%2F5pv6o9hq26gjgguljci2.jpeg" 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%2Fuploads%2Farticles%2F5pv6o9hq26gjgguljci2.jpeg" alt="Companies table with HTML"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Adding a colored hover effect to your table rows can be a nice UX improvement. Additionally, it helps your eyes keep track of which row you're on as you scan the table.&lt;/p&gt;

&lt;p&gt;But what if the table only has one row? This effect sounds useless, right?🤔&lt;br&gt;
any solutions?&lt;/p&gt;

&lt;p&gt;YES, exactly. we will use the :has() selector to decide whether we will add this hover effect to the table rows or not, depending on the number of rows.&lt;/p&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%2Fuploads%2Farticles%2Fihfggd7pxydlry5hcba8.jpeg" 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%2Fuploads%2Farticles%2Fihfggd7pxydlry5hcba8.jpeg" alt="Adding hover effect to table depending on number of rows"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code snippet checks if the table has at least two rows (excluding the header), and applies the hover effect accordingly. If the table contains only one row, the effect is not applied.&lt;/p&gt;

&lt;p&gt;Here is the codepen for this case:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Ahmed_Arafa/embed/ZEqKvOa?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And thus, we have explained two cases for using this awesome selector, It has ton of uses, You can utilize it in your own way.&lt;/p&gt;

&lt;p&gt;Feel free to write your thoughts in the comments section👇🏼&lt;br&gt;
Have a Good day.&lt;/p&gt;

</description>
      <category>html</category>
      <category>scss</category>
      <category>javascript</category>
      <category>codepen</category>
    </item>
  </channel>
</rss>
