<?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>Forem: nj145</title>
    <description>The latest articles on Forem by nj145 (@nj145).</description>
    <link>https://forem.com/nj145</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%2F1041329%2F8b8cbea0-27ec-42d4-b5a9-ce225d7bd3f6.jpeg</url>
      <title>Forem: nj145</title>
      <link>https://forem.com/nj145</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nj145"/>
    <language>en</language>
    <item>
      <title>5 Ways to improve productivity and focus as a developer</title>
      <dc:creator>nj145</dc:creator>
      <pubDate>Thu, 23 Mar 2023 21:41:05 +0000</pubDate>
      <link>https://forem.com/nj145/5-ways-to-improve-productivity-and-focus-as-a-developer-57e0</link>
      <guid>https://forem.com/nj145/5-ways-to-improve-productivity-and-focus-as-a-developer-57e0</guid>
      <description>&lt;p&gt;As a developer, you're no stranger to those moments when you're staring at your screen, trying to get work done, but the productivity fairy just won't sprinkle her magic dust on you.&lt;br&gt;
I've faced this myself a lot of times in my career and with several tried and tested methods I feel that I have got some control over the situation now. So, I would like to share a few tips here that has helped me personally to stay focused and improve my productivity over the past several years.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Planning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This might sound uncool or boring, but this is the key. Make a &lt;strong&gt;to-do list&lt;/strong&gt;.&lt;br&gt;
The first thing I do when I start my day at work is make a to-do list. I write down every single thing I want to accomplish, weather it's a coding task or following up on certain topic with a co-worker or replying to that email which you have been putting off for a while now.&lt;br&gt;
As you get done with each of these tasks you get a sense of accomplishment and that'll help you keep motivated to check off the remainder of your tasks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid multi-tasking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do you ever feel like you're juggling five different tasks at once, and they all come crashing down on your head? I've been there too. What has helped me there is to focus on one thing at a time and give it all your attention. This is where the to-do list you created in Step 1. can help you prioritize your tasks. Once you do that, you'll be amazed at how much faster you'll complete your tasks without the stress of trying to do everything at once.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take breaks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have you ever noticed that the longer you work without a break, the less productive you become? It's science! Take a quick break every hour to stretch your legs, grab a snack, or do a quick walk. You'll come back feeling refreshed and ready to take on the world (or at least your coding tasks). But... beware..do not get carried away during this break by endless scrolling through Instagram or Tiktok, or else before you realize your brain would've consumed a ton of unwanted junk that will further keep your focus away from your task. I learned this the hard way. Finally, with some practice now I'm at a point where I can stop myself from being pulled into the social media loop before it's too late!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep your workspace organized like a neat freak&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Clutter and chaos are productivity killers. Keep your workspace organized and tidy so you can focus on what really matters – coding like a boss! Plus, you'll feel like a superhero when you can find that one file or note you need in seconds. I've certainly seen a huge difference in the way I function (especially when working from home) when I sit at my work desk vs when I'm on the couch. I tend to be more focused and productive when I'm at my desk with very little distractions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn new skills like a kid in a candy store&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Learning new things is like getting an extra life in a video game. Take advantage of all the amazing resources out there, like online courses, forums, and YouTube tutorials. You can take breaks in between your coding tasks to shift focus to learn something new that can probably open you up to some new ideas which in turn can help you with your current task.&lt;/p&gt;

&lt;p&gt;I'd love to hear from you as to what are some of your favorite tips and tricks to improve productivity and stay focused in your daily coding routine.. :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>All you need to know about Next.js Image Optimization and how to effectively make use of it</title>
      <dc:creator>nj145</dc:creator>
      <pubDate>Tue, 14 Mar 2023 20:00:56 +0000</pubDate>
      <link>https://forem.com/nj145/all-you-need-to-know-about-nextjs-image-optimization-and-how-to-effectively-make-use-of-it-4pmh</link>
      <guid>https://forem.com/nj145/all-you-need-to-know-about-nextjs-image-optimization-and-how-to-effectively-make-use-of-it-4pmh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Images can have a significant impact on website loading times, which can in turn affect user experience and website performance.&lt;br&gt;
Let's look at the importance of image optimization and how Next.js provides us this capability out-of-the-box.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's say you need to display an image on your web page. How would you traditionally do this?
By adding an image to your UI using the HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element. Something like this:&lt;/li&gt;
&lt;/ul&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%2F7cpn8vqkdizodh613wws.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%2F7cpn8vqkdizodh613wws.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The img here is being loaded from an external server and let's examine the file size and download 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%2Fsvtne5rln1fzibm6zv8m.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%2Fsvtne5rln1fzibm6zv8m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, you may think 675ms is fairly quick, but if you had multiple images on your web page you would see it considerably slowing down your page load.&lt;/p&gt;
&lt;h2&gt;
  
  
  Next.js Image Component to the rescue
&lt;/h2&gt;

&lt;p&gt;The Next.js Image component includes a variety of built-in performance optimizations and here are some of them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Improved Performance - The Image component can automatically resize images to the correct size for the container they are displayed in. This helps to reduce the file size of images and improve loading times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lazy loading - The Image component uses lazy loading to defer the loading of images until they are needed. Images are only loaded when they enter the viewport.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Responsive images - The Image component supports responsive images, which allows different images to be displayed depending on the user's screen size. This helps to ensure that images are displayed at the correct size on different devices, while also reducing the file size of images.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Visual Stability - Prevent &lt;em&gt;Cumulative Layout Shift&lt;/em&gt; (CLS) is a metric that measures the visual stability of a web page during the loading process. It measures how much the page layout changes as elements load and shift around on the screen.&lt;br&gt;
You can read more about CLS here - &lt;a href="https://nextjs.org/learn/seo/web-performance/cls" rel="noopener noreferrer"&gt;https://nextjs.org/learn/seo/web-performance/cls&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Let's see this in action
&lt;/h2&gt;

&lt;p&gt;Next.js Image component can be used in different ways and let's compare it's performance with that of a generic html img in all of these cases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scenario 1:
&lt;strong&gt;Loading a Static Image using Next.js Image component&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Head from 'next/head'
import Image from 'next/image'
import SampleImg from '../../public/sampleImg.jpg'

export default function Home() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Image Optimization Demo&amp;lt;/title&amp;gt;
      &amp;lt;/Head&amp;gt;
      &amp;lt;main&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;img src='https://m.media-amazon.com/images/I/71ZOjO-tDRL._SX3000_.jpg' alt='sample' width={960} height={400} /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;Image src={SampleImg} alt='Sample' height={400} width={960}&amp;gt;&amp;lt;/Image&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/main&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In this Next.js application, I've loaded 2 images - one using the HTML img element and the other using Next.js Image component by using a local image.&lt;/p&gt;

&lt;p&gt;Now, let's compare the loading times and file sizes of both the image files:&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%2Ftlmejx9qx3hbbj04bgo9.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%2Ftlmejx9qx3hbbj04bgo9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see here, the Next.js Image loads much faster&lt;br&gt;
and also it uses the webp format for the image which offers better compression and smaller file sizes than JPEG, without sacrificing image quality.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scenario 2:
&lt;strong&gt;Loading a remote Image&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Head from 'next/head'
import Image from 'next/image'
import SampleImg from '../../public/sampleImg.jpg'

export default function Home() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Image Optimization Demo&amp;lt;/title&amp;gt;
      &amp;lt;/Head&amp;gt;
      &amp;lt;main&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;img src='https://m.media-amazon.com/images/I/71ZOjO-tDRL._SX3000_.jpg' alt='sample' width={960} height={400} /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;Image src='https://m.media-amazon.com/images/I/71ZOjO-tDRL._SX3000_.jpg' alt='Sample' width={960} height={400}&amp;gt;&amp;lt;/Image&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/main&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To use a remote image, the src property should be a URL string, which can be relative or absolute. Because Next.js does not have access to remote files during the build process, you'll need to provide the width and height.&lt;/p&gt;

&lt;p&gt;However, if you do this, you will most likely get the following error:&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%2Favp4muh3mks8ktooo5z7.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%2Favp4muh3mks8ktooo5z7.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The error indicates that in order to protect your application from malicious users, configuration is required in order to use external images. This ensures that only external images from your account can be served from the Next.js Image Optimization API. These external images can be configured with the remotePatterns property in your next.config.js file, as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'm.media-amazon.com',
        port: '',
        pathname: '/images/**',
      },
    ],
  },
}

module.exports = nextConfig

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding this configuration should load the image successfully.&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%2F4l1eo0k5oj125zq9jblf.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%2F4l1eo0k5oj125zq9jblf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, again if we compare the results, you can see that the Next image loads much faster.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scenario 3:
&lt;strong&gt;The size of the image is unknown in case of a remote Image&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the ways that images most commonly hurt performance is through layout shift The way to avoid image-based layout shifts is to always size your images. This allows the browser to reserve precisely enough space for the image before it loads.&lt;br&gt;
&lt;em&gt;But, What if I don't know the size of my images?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you are accessing images from a source without knowledge of the images' sizes, you can use &lt;strong&gt;fill&lt;/strong&gt; prop which allows your image to be sized by its parent element.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Head from 'next/head'
import Image from 'next/image'
import SampleImg from '../../public/sampleImg.jpg'

export default function Home() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Image Optimization Demo&amp;lt;/title&amp;gt;
      &amp;lt;/Head&amp;gt;
      &amp;lt;main&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;img src='https://m.media-amazon.com/images/I/71ZOjO-tDRL._SX3000_.jpg' alt='sample' width={960} height={400}&amp;gt;&amp;lt;/img&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div style={{ position: 'relative', width: '960px', height: '400px' }}&amp;gt;
            &amp;lt;Image src='https://m.media-amazon.com/images/I/71ZOjO-tDRL._SX3000_.jpg' alt='Sample' fill&amp;gt;&amp;lt;/Image&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/main&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, when loading the Next Image, we have not specified the size, instead used the &lt;strong&gt;fill&lt;/strong&gt; prop which will size the image based on it's parent div's dimensions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly, consider a case where a image needs to be on the immediate viewport when a web page is loaded.

&lt;ul&gt;
&lt;li&gt;In such case we can use the &lt;strong&gt;priority&lt;/strong&gt; property on the image. Doing so, allows Next.js to specially prioritize the image for loading. When an image is marked with a &lt;strong&gt;priority&lt;/strong&gt;, the browser will attempt to download and render that image before other images on the page.
This way we can prevent any kind of flicker that may occur when loading a web page with an Image in the immediate viewport.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Responsive Image loading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;As we already discussed, the Next image component supports responsive images, which allows different images to be displayed depending on the user's screen size.
Let's examine this in action.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you resize the screen to different sizes, it can be seen that for each of the Next image requests, query parameters of the width and height are passed in depending on the screen size.&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%2Fqc7vd9vb6xgsrhjqxtd2.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%2Fqc7vd9vb6xgsrhjqxtd2.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
 Here's an image request for screen size of a tablet.&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%2Ftftvjuhd0h7geqzf7bhh.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%2Ftftvjuhd0h7geqzf7bhh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is another request for screen size of a mobile.&lt;/p&gt;

&lt;p&gt;So to conclude, Next.js Image component provides us with various Image Optimization techniques which we can leverage to improve the page load and overall web page performance which otherwise would require us to manually implement these optimization techniques thereby saving us developers a ton of time! All we need to do is &lt;strong&gt;remember to use the Image component&lt;/strong&gt; in a Next.js application.&lt;br&gt;
However, I'd also like to point out that if you are pulling in the images dynamically from external sites that you do not trust, be cautious when using Next Image component as you need to white-list the domains.&lt;/p&gt;

&lt;p&gt;Happy Coding! :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>nextjs</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React - Multi-select/deselect selection input using MUI Autocomplete</title>
      <dc:creator>nj145</dc:creator>
      <pubDate>Fri, 10 Mar 2023 23:40:26 +0000</pubDate>
      <link>https://forem.com/nj145/reactjs-multi-selectdeselect-selection-input-using-mui-autocomplete-56c8</link>
      <guid>https://forem.com/nj145/reactjs-multi-selectdeselect-selection-input-using-mui-autocomplete-56c8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Autocomplete is a popular UI component used in web applications to enhance user experience and productivity. It allows users to quickly search and select from a large set of options.&lt;br&gt;
Material UI (MUI) provides an AutoComplete component which is very useful especially if you need to search and select multiple options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://mui.com/material-ui/react-autocomplete/#multiple-values" rel="noopener noreferrer"&gt;https://mui.com/material-ui/react-autocomplete/#multiple-values&lt;/a&gt;&lt;br&gt;
Here's what MUI offers us out of the box.&lt;/p&gt;

&lt;p&gt;But, the MUI Autocomplete has some limitations as well. Suppose, I want to do a select all from a large list, it is a tedious task to keep clicking and selecting. Also, there is no way to select/deselect all of the filtered items from the list easily.&lt;/p&gt;

&lt;p&gt;So, this is the exact problem I had to solve. I wanted to filter and select from a very large list of items and also provide the ability to select/deselect all options or select/deselect all of the filtered options.&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%2Fpr04yiblj3td8319fs72.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%2Fpr04yiblj3td8319fs72.png" alt="MUI Autocomplete" width="622" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Suppose we have a list of Departments under different Companies and we need a way to either select/deselect them all or filter by a particular Department and select all filtered options.&lt;/p&gt;

&lt;p&gt;I've come up with a custom AutoComplete to solve this very problem which uses MUI Autocomplete and some javascript.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tools and Dependencies:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;React&lt;/li&gt;
&lt;li&gt;Material UI&lt;/li&gt;
&lt;li&gt;lodash&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Link to GitHub Repo - &lt;a href="https://github.com/nj145/multiselectDeselect-dropdown" rel="noopener noreferrer"&gt;https://github.com/nj145/multiselectDeselect-dropdown&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open in CodeSandbox -  &lt;a href="https://codesandbox.io/s/github/nj145/multiselectDeselect-dropdown" rel="noopener noreferrer"&gt;https://codesandbox.io/s/github/nj145/multiselectDeselect-dropdown&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Code Explained
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We have 2 components:

&lt;ul&gt;
&lt;li&gt;Selection&lt;/li&gt;
&lt;li&gt;MultiSelectAll&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Selection component&lt;/strong&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 React, { useState } from "react";
import { Grid, TextField, Typography } from "@mui/material";
import MultiSelectAll from "./MultiSelectAll";

const Selection = () =&amp;gt; {
  const initialValue = [{ label: "Uber/Sales", value: "14" }];

  const departmentNames = [
    { label: "Amazon/Sales", value: "1" },
    { label: "Amazon/Customer Support", value: "2" },
    { label: "Google/Operations", value: "3" },
    { label: "Google/Engineering", value: "4" },
    { label: "Google/Services", value: "5" },
    { label: "Google/Customer Support", value: "6" },
    { label: "Netflix/Sales", value: "7" },
    { label: "Netflix/Engineering", value: "8" },
    { label: "Netflix/Services", value: "9" },
    { label: "Netflix/Operations", value: "10" },
    { label: "Microsoft/Sales", value: "11" },
    { label: "Microsoft/Operations", value: "12" },
    { label: "Microsoft/Customer Support", value: "13" },
    { label: "Uber/Sales", value: "14" },
    { label: "Uber/Services", value: "15" },
    { label: "Uber/Customer Support", value: "16" },
    { label: "Uber/Engineering", value: "17" },
    { label: "Uber/Operations", value: "18" },
    { label: "Walmart/Sales", value: "19" },
    { label: "Walmart/Services", value: "20" },
    { label: "Walmart/Operations", value: "21" },
    { label: "Walmart/Engineering", value: "22" },
    { label: "Walmart/Customer Support", value: "23" },
    { label: "CVS/Sales", value: "24" },
    { label: "CVS/Customer Support", value: "25" },
    { label: "CVS/Engineering", value: "26" },
    { label: "CVS/Operations", value: "27" }
  ];

  const getTextBoxInputValue = (input) =&amp;gt; {
    return input.map((itm) =&amp;gt; itm.label).join(";");
  };

  const [currentSelection, setCurrentSelection] = useState(
    getTextBoxInputValue(initialValue)
  );

  const handleSelectionChange = (result) =&amp;gt; {
    const valueToSave = result.map((itm) =&amp;gt; itm.label).join(";");
    setCurrentSelection(valueToSave);
  };

  return (
    &amp;lt;Grid container flexDirection="column" alignItems="center"&amp;gt;
      &amp;lt;Grid item xs={12} sx={{ p: 2 }}&amp;gt;
        &amp;lt;MultiSelectAll
          sx={{ maxheight: "700px" }}
          items={departmentNames}
          selectAllLabel="Select All"
          value={initialValue}
          onChange={handleSelectionChange}
        /&amp;gt;
      &amp;lt;/Grid&amp;gt;
      &amp;lt;Grid item xs={12} sx={{ p: 2 }}&amp;gt;
        &amp;lt;Typography&amp;gt;Selected items:&amp;lt;/Typography&amp;gt;
        &amp;lt;TextField sx={{ width: "450px" }} value={currentSelection} /&amp;gt;
      &amp;lt;/Grid&amp;gt;
    &amp;lt;/Grid&amp;gt;
  );
};

export default Selection;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This component displays the MultiSelectAll component and a simple text box that lists all the selected values separated by a semi colon.&lt;/li&gt;
&lt;li&gt;The MultiSelectAll component takes in an array of options in the form of label-value pairs and renders them as a dropdown menu. It also displays a "Select All" option, allowing users to select all the available options with a single click.&lt;/li&gt;
&lt;li&gt;When a user selects or deselects an option, the handleSelectionChange function is called. This function takes in the updated selection and updates the state of the currentSelection variable by converting the selected options' labels into a semicolon-separated string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;MultiSelectAll Component&lt;/strong&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 React, { useState, useEffect, useRef } from "react";
import isEqual from "lodash/isEqual";
import debounce from "lodash/debounce";

import {
  Autocomplete,
  Checkbox,
  Grid,
  FormControlLabel,
  TextField
} from "@mui/material";
import { createFilterOptions } from "@mui/material/Autocomplete";

const MultiSelectAll = ({ items, selectAllLabel, onChange, value }) =&amp;gt; {
  const [selectedOptions, setSelectedOptions] = useState(value);
  const [filteredOptions, setFilteredOptions] = useState(null);
  const multiSelectRef = useRef(null);

  useEffect(() =&amp;gt; {
    onChange(selectedOptions);
  }, [selectedOptions]);

  const handleToggleOption = (selectedOptions) =&amp;gt;
    setSelectedOptions(selectedOptions);
  const handleClearOptions = () =&amp;gt; setSelectedOptions([]);
  const getOptionLabel = (option) =&amp;gt; `${option.label}`;

  const allItemsSelected = () =&amp;gt; {
    // if options are filtered, check to see if all filtered options are in selected items
    // if yes, selectAll - true, else selectAll - false
    // if options are not filtered, check to see if all items are selected or not
    if (filteredOptions?.length !== items.length) {
      const excludedFilteredOptions = filteredOptions?.filter(
        (opt) =&amp;gt; !selectedOptions.find((selOpt) =&amp;gt; selOpt.label === opt.label)
      );
      if (excludedFilteredOptions?.length &amp;gt; 0) {
        return false;
      }
      return true;
    }
    const allSelected =
      items.length &amp;gt; 0 &amp;amp;&amp;amp; items.length === selectedOptions.length;
    return allSelected;
  };

  const clearSelected = (selOptions) =&amp;gt; {
    // filter out the selOptions
    if (selOptions.length &amp;gt; 0) {
      setSelectedOptions(
        selectedOptions.filter(
          (item) =&amp;gt;
            !selOptions.find((selOption) =&amp;gt; selOption.label === item.label)
        )
      );
    } else {
      setSelectedOptions([]);
    }
  };

  const handleSelectAll = (isSelected) =&amp;gt; {
    let selectedList = [];
    if (
      filteredOptions?.length &amp;gt; 0 &amp;amp;&amp;amp;
      filteredOptions.length !== items.length
    ) {
      selectedList = items.filter((item) =&amp;gt;
        filteredOptions.find(
          (filteredOption) =&amp;gt; filteredOption.label === item.label
        )
      );
    }
    if (isSelected) {
      if (selectedList.length &amp;gt; 0) {
        setSelectedOptions([...selectedOptions, ...selectedList]);
      } else {
        setSelectedOptions(items);
      }
    } else {
      clearSelected(selectedList);
    }
  };

  const handleToggleSelectAll = () =&amp;gt; {
    handleSelectAll(!allItemsSelected());
  };

  const handleChange = (event, selectedOptions, reason) =&amp;gt; {
    let result = null;
    if (reason === "clear") {
      handleClearOptions();
    } else if (reason === "selectOption" || reason === "removeOption") {
      if (selectedOptions.find((option) =&amp;gt; option.value === "select-all")) {
        handleToggleSelectAll();
        // let result = [];
        result = items.filter((el) =&amp;gt; el.value !== "select-all");
        // onChange(result);
      } else {
        handleToggleOption(selectedOptions);
        result = selectedOptions;
        // onChange(selectedOptions);
      }
    }
  };

  const handleCheckboxChange = (e, option) =&amp;gt; {
    if (option.value === "select-all") {
      handleToggleSelectAll();
      // if (e.target.checked) {
      //     // onChange(items);
      // } else {
      //     // onChange([]);
      // }
    } else if (e.target.checked) {
      const result = [...selectedOptions, option];
      setSelectedOptions(result);
      // onChange(result);
    } else {
      const result = selectedOptions.filter(
        (selOption) =&amp;gt; selOption.value !== option.value
      );
      setSelectedOptions(result);
      // onChange(result);
    }
  };

  const optionRenderer = (props, option, { selected }) =&amp;gt; {
    const selectAllProps =
      option.value === "select-all" // To control the state of 'select-all' checkbox
        ? { checked: allItemsSelected() }
        : {};
    return (
      &amp;lt;Grid container key={option.label}&amp;gt;
        &amp;lt;Grid item xs={12} sx={{ pl: 1, pr: 1 }}&amp;gt;
          &amp;lt;FormControlLabel
            control={
              &amp;lt;Checkbox
                key={option.label}
                checked={selected}
                onChange={(e) =&amp;gt; handleCheckboxChange(e, option)}
                {...selectAllProps}
                sx={{ mr: 1 }}
              /&amp;gt;
            }
            label={getOptionLabel(option)}
            key={option.label}
          /&amp;gt;
        &amp;lt;/Grid&amp;gt;
      &amp;lt;/Grid&amp;gt;
    );
  };

  const debouncedStateValue = debounce((newVal) =&amp;gt; {
    // console.log(isEqual(newVal, filteredOptions));
    if (newVal &amp;amp;&amp;amp; !isEqual(newVal, filteredOptions)) {
      // console.log('setting filtered options');
      setFilteredOptions(newVal);
    }
  }, 1000);

  const updateFilteredOptions = (filtered) =&amp;gt; {
    debouncedStateValue(filtered);
  };

  const inputRenderer = (params) =&amp;gt; &amp;lt;TextField {...params} /&amp;gt;;

  const filter = createFilterOptions();

  return (
    &amp;lt;Autocomplete
      ref={multiSelectRef}
      sx={{
        width: "350px",
        maxHeight: "120px",
        overflowY: "scroll"
      }}
      multiple
      size="small"
      options={items}
      value={selectedOptions}
      disableCloseOnSelect
      getOptionLabel={getOptionLabel}
      isOptionEqualToValue={(option, val) =&amp;gt; option.value === val.value}
      filterOptions={(options, params) =&amp;gt; {
        const filtered = filter(options, params);
        updateFilteredOptions(filtered);
        return [{ label: selectAllLabel, value: "select-all" }, ...filtered];
      }}
      onChange={handleChange}
      renderOption={optionRenderer}
      renderInput={inputRenderer}
    /&amp;gt;
  );
};

export default MultiSelectAll;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The MultiSelectAll component takes in an array of items to be displayed, a label for the "Select All" option, a function to handle changes, and a value to represent the selected items.&lt;/li&gt;
&lt;li&gt;The allItemsSelected function determines if all options are selected or not.&lt;/li&gt;
&lt;li&gt;The handleToggleSelectAll function toggles the selection of all options when the "Select All" checkbox is checked or unchecked.&lt;/li&gt;
&lt;li&gt;The handleChange function handles changes to the selected options. If the "Select All" option is checked or unchecked, the handleToggleSelectAll function is called. Otherwise, the handleToggleOption function is called.&lt;/li&gt;
&lt;li&gt;The handleCheckboxChange function handles the change of an individual checkbox. If the "Select All" checkbox is checked, the handleToggleSelectAll function is called. Otherwise, the selected item is added or removed from the selected options list.&lt;/li&gt;
&lt;li&gt;The optionRenderer function is a helper function that renders each option in the dropdown.&lt;/li&gt;
&lt;li&gt;The Autocomplete component is used to render the multi-select dropdown. It uses the filterOptions function to filter the options based on user input. The getOptionLabel function is used to get the label of an option. The isOptionEqualToValue function is used to check if two options are equal. The renderInput function is used to render the input field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope this helped! :)&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
