<?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: ShreenidhiBatavi</title>
    <description>The latest articles on Forem by ShreenidhiBatavi (@shreenidhibatavi).</description>
    <link>https://forem.com/shreenidhibatavi</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%2F624884%2Fa17f000a-9299-421c-afdd-5abe47184a9d.jpeg</url>
      <title>Forem: ShreenidhiBatavi</title>
      <link>https://forem.com/shreenidhibatavi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shreenidhibatavi"/>
    <language>en</language>
    <item>
      <title>React useTransition Hook : The Tool for Non-Blocking Updates</title>
      <dc:creator>ShreenidhiBatavi</dc:creator>
      <pubDate>Sun, 23 Feb 2025 05:58:50 +0000</pubDate>
      <link>https://forem.com/shreenidhibatavi/react-usetransition-hook-the-tool-for-non-blocking-updates-f55</link>
      <guid>https://forem.com/shreenidhibatavi/react-usetransition-hook-the-tool-for-non-blocking-updates-f55</guid>
      <description>&lt;p&gt;React offers various hooks to efficiently manage state and side effects. One such hook, introduced in React 18, is useTransition, which enhances UI responsiveness. It allows developers to handle state updates in a non-blocking manner, ensuring smoother performance and a better user experience.&lt;/p&gt;

&lt;p&gt;What is the &lt;code&gt;useTransition&lt;/code&gt; Hook ?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useTransition&lt;/code&gt; hook is a built-in React hook that helps defer state updates by marking them as low-priority. This means that high-priority updates, like user interactions, remain smooth, while other updates can wait for a more optimal time to execute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;const [isPending, startTransition] = useTransition();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;isPending&lt;/code&gt;: A boolean that indicates whether the transition is still pending.&lt;br&gt;
&lt;code&gt;startTransition&lt;/code&gt;: A function that wraps the state update to mark it as a transition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Using &lt;code&gt;useTransition&lt;/code&gt; for List Filtering&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhfvr4p7gmjxd3st2vchs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhfvr4p7gmjxd3st2vchs.png" alt="App component" width="712" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a App component that renders the &lt;code&gt;ItemsList&lt;/code&gt; component. To effectively demonstrate &lt;code&gt;useTransition&lt;/code&gt;, we generate a large list of items with the help pf &lt;code&gt;generateLargeList&lt;/code&gt; function and pass it as a prop to &lt;code&gt;ItemsList&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Foyflm6h8zsrxc7cj52qj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Foyflm6h8zsrxc7cj52qj.png" alt="ItemList Component" width="760" height="707"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ItemsList component receives the items prop and displays them inside a &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; element.It also includes an input field for filtering the list dynamically.Within ItemList,we maintain two state variables:&lt;/p&gt;

&lt;p&gt;One for storing the input field’s value.Another for managing the filtered list of items.After setting up and running the application, you’ll notice a lag in the input field updates.The typed values don’t appear instantly because the UI is blocked while rendering the filtered list of items&lt;/p&gt;

&lt;p&gt;Let’s address this issue with the help of &lt;code&gt;useTransition&lt;/code&gt;hook&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhlkwqa02uz5r2xqwa4k6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhlkwqa02uz5r2xqwa4k6.png" alt="useTransition usage" width="800" height="758"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we’ve now imported the &lt;code&gt;useTransition&lt;/code&gt; hook. Using &lt;code&gt;startTransition&lt;/code&gt;, we mark &lt;code&gt;filteredItems&lt;/code&gt; as a low-priority update, ensuring smoother UI interactions. Additionally, &lt;code&gt;isPending&lt;/code&gt; returns a boolean value, indicating whether a transition is in progress. This can be used to display a loading indicator while the filtering process is happening.&lt;/p&gt;

&lt;p&gt;Now, the input field updates instantly without any lag,as&lt;code&gt;startTransition&lt;/code&gt; ensures that filtering happens in the background.Once the filtering process is complete, the UI updates smoothly without blocking user interactions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use &lt;code&gt;useTransition&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You should consider using &lt;code&gt;useTransition&lt;/code&gt; in the following cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Filtering Large Data: Ensuring smooth typing experience while filtering data in the background.&lt;/li&gt;
&lt;li&gt;Rendering Heavy Components: Deferring complex re-renders while keeping the UI responsive.&lt;/li&gt;
&lt;li&gt;Optimizing UI Performance: Preventing lag in interactive components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;useTransition&lt;/code&gt; hook is a powerful tool for improving UI responsiveness by deferring non-urgent updates. By marking updates as transitions, React can prioritize user interactions, making applications feel smoother and more performant. If you're dealing with slow UI updates due to expensive renders, &lt;code&gt;useTransition&lt;/code&gt; can be a great solution.&lt;/p&gt;

&lt;p&gt;That’s it for now! Thanks for reading. See you in the next post!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>structuredClone - A modern way to deep clone objects in javascript</title>
      <dc:creator>ShreenidhiBatavi</dc:creator>
      <pubDate>Mon, 30 Sep 2024 10:39:31 +0000</pubDate>
      <link>https://forem.com/shreenidhibatavi/structuredclone-a-modern-way-to-deep-clone-objects-in-javascript-bbe</link>
      <guid>https://forem.com/shreenidhibatavi/structuredclone-a-modern-way-to-deep-clone-objects-in-javascript-bbe</guid>
      <description>&lt;p&gt;In JavaScript, when working with objects or arrays, especially within React components, we often need to take a copy of the current state or variable&lt;/p&gt;

&lt;p&gt;Many of us use the spread operator &lt;code&gt;(...)&lt;/code&gt; for this purpose. While this is great for creating shallow copies, it falls short when it comes to deep copies, which can lead to unintended behavior. Let's understand this with an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxqr082e2bfl5c4i8s2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxqr082e2bfl5c4i8s2r.png" alt="Example using spread operator" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;: The spread operator only creates a new reference for top-level properties. Since &lt;code&gt;arr&lt;/code&gt; is a nested, so it is referenced in both &lt;code&gt;obj&lt;/code&gt; and &lt;code&gt;obj_copy&lt;/code&gt; changes made in &lt;code&gt;obj_copy&lt;/code&gt; affect the original &lt;code&gt;obj&lt;/code&gt; object as well.&lt;/p&gt;

&lt;p&gt;To solve this problem, we need a deep copy. There are a few ways to achieve this:&lt;br&gt;
&lt;strong&gt;Libraries like Lodash:&lt;/strong&gt; You can use libraries such as Lodash &lt;code&gt;(_.cloneDeep)&lt;/code&gt; to handle deep copies.&lt;br&gt;
&lt;strong&gt;JSON Methods:&lt;/strong&gt; A common approach is using &lt;code&gt;JSON.stringify()&lt;/code&gt; and &lt;code&gt;JSON.parse()&lt;/code&gt; to create a deep copy:&lt;/p&gt;

&lt;p&gt;Using third-party libraries for deep cloning adds extra weight to your bundle, Instead of relying on libraries or using the JSON methods, structuredClone is a native, efficient way to deep clone JavaScript objects, introduced in recent versions of modern browser&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcw4qv1hf4zfjuuhphty7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcw4qv1hf4zfjuuhphty7.png" alt="Example using structuredClone" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While the spread operator is handy for creating shallow copies, it falls short for deep cloning nested objects or arrays. Modern JavaScript provides a built-in, efficient solution in the form of &lt;code&gt;structuredClone()&lt;/code&gt;, which ensures that objects are properly copied without unexpected reference sharing. This makes &lt;code&gt;structuredClone()&lt;/code&gt; a perfect fit for use cases where immutability is crucial, such as state management in React applications.&lt;/p&gt;

&lt;p&gt;That's it for now! Thanks for reading. See you in the next post!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to useActionState -New hook in React</title>
      <dc:creator>ShreenidhiBatavi</dc:creator>
      <pubDate>Thu, 26 Sep 2024 03:34:21 +0000</pubDate>
      <link>https://forem.com/shreenidhibatavi/introduction-to-useactionstate-new-hook-in-react-49b1</link>
      <guid>https://forem.com/shreenidhibatavi/introduction-to-useactionstate-new-hook-in-react-49b1</guid>
      <description>&lt;p&gt;The &lt;code&gt;useActionState&lt;/code&gt; hook is a new feature introduced in React 19, designed to simplify state management based on form actions. It offers a powerful way to handle asynchronous form submissions and automatically manage the state and loading states associated with these actions. In this post, we'll dive into the useActionState hook and explore how it works with a practical example.&lt;/p&gt;

&lt;p&gt;Practical example on usage of useActionState&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Before getting started, please ensure that React 19 is installed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To illustrate how the &lt;code&gt;useActionState&lt;/code&gt;hook works, let's implement a basic counter application for simplicity. The same principles, however, can be easily applied to more complex forms and interactions&lt;/p&gt;

&lt;p&gt;First, import the &lt;code&gt;useActionState&lt;/code&gt; hook from React 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%2F4nf0in6g6n8ryowif1mw.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%2F4nf0in6g6n8ryowif1mw.png" alt="importing useActionState from react"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we’ve imported the hook, let’s look at how to use it in our component.&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%2F5ueze2joclkqdhwqq7a8.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%2F5ueze2joclkqdhwqq7a8.png" alt="using useActionState hook in component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s try to understand the syntax of &lt;code&gt;useActionState&lt;/code&gt; , this hook accepts two primary arguments, one is from submit handler and then second argument is initial state (this also takes third arguments which is an optional, but for simplicity, we’ll focus on the two required arguments in this post)&lt;/p&gt;

&lt;p&gt;This hook returns an array with three elements&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;counterValue&lt;/strong&gt;— The first value returned, representing the current state. In this case, it’s the counter value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;formAction&lt;/strong&gt;- This is the function to be called when the form is submitted&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;isPending&lt;/strong&gt;— A boolean indicating whether the form action is still processing (useful for showing loading indicators).&lt;/p&gt;

&lt;p&gt;Now that we’ve covered the basic import and syntax of &lt;code&gt;useActionState&lt;/code&gt;, let's see how it works in the example&lt;/p&gt;

&lt;p&gt;Here’s the complete example using useActionState to increment a counter:&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%2F0hc3217l00e2bcxwmdq2.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%2F0hc3217l00e2bcxwmdq2.png" alt="component illustrating useActionState hook with counter example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s try to understand the code written in above snippet.As mentioned earlier we have imported &lt;code&gt;useActionState&lt;/code&gt; from react and using it in the App component&lt;/p&gt;

&lt;p&gt;1 . This hook is taking two arguments&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;increment&lt;/strong&gt;: This is an asynchronous function executed when the form is submitted. It takes the previous counter state and returns a new state after a 1-second delay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initial State (0)&lt;/strong&gt;: The initial value of the counter is set to 0&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Increment Function&lt;/code&gt;: let’s understand the functionality of increment function. This function takes two parameters&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;previousState&lt;/code&gt;: The last known state of the counter.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;formData&lt;/code&gt;: Data from the form submission (not used in this case, but from this &lt;code&gt;formData&lt;/code&gt; we can access from field values).&lt;/p&gt;

&lt;p&gt;This function returns a Promise that simulates an asynchronous operation (like a server call) using &lt;code&gt;setTimeout&lt;/code&gt;. After one second, it resolves with the new state, which is &lt;code&gt;previousState + 1&lt;/code&gt;. This effectively increments the counter by one.&lt;/p&gt;

&lt;p&gt;In jsx we are rendering &lt;code&gt;form&lt;/code&gt; within that we are rendering our counter and a button to increment the counter. The &lt;code&gt;action&lt;/code&gt; prop of the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;is set to &lt;code&gt;formAction&lt;/code&gt;, which will handle the submission upon clicking Increment button.&lt;/p&gt;

&lt;p&gt;inside the form we are conditionally rendering either &lt;code&gt;"updating..."&lt;/code&gt; if isPending is true (indicating the form is processing), or displays the current &lt;code&gt;counterValue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The “Increment” button is disabled while &lt;code&gt;isPending&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; to prevent additional submissions during the ongoing update.&lt;/p&gt;

&lt;p&gt;Finally we have implemented a simple counter using the &lt;code&gt;useActionState&lt;/code&gt; hook in React, where the counter is incremented asynchronously by one second each time the form is submitted. It also displays a loading message ("Updating...") while the increment action is in progress.&lt;/p&gt;

&lt;p&gt;Now we’ve understood the &lt;code&gt;useActionState&lt;/code&gt; , let’s see some of the benefits of it.&lt;/p&gt;

&lt;p&gt;Benefits of &lt;code&gt;useActionState&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simplified State Management&lt;/strong&gt;: The &lt;code&gt;useActionState&lt;/code&gt; hook automatically updates the component's state based on the result of the action function, reducing the need for manually managing state changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loading State Handling&lt;/strong&gt;: The &lt;code&gt;isPending&lt;/code&gt; value provided by the hook makes it easy to display loading indicators or disable form elements during asynchronous actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automatic Form Reset&lt;/strong&gt;: After a successful submission, the form is automatically reset to its initial state, making the development process more efficient.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cleaner, More Maintainable Code&lt;/strong&gt;: By encapsulating form state and actions,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useActionState&lt;/code&gt; promotes a more concise and maintainable codebase.&lt;br&gt;
The &lt;code&gt;useActionState&lt;/code&gt; hook is a valuable addition to React for handling form actions and asynchronous state updates. It simplifies common tasks like managing loading states and resetting forms after submission. By using this hook, you can write cleaner, more efficient, and maintainable code.&lt;/p&gt;

&lt;p&gt;That’s it for now! Thanks for reading. See you in the next post!&lt;/p&gt;

</description>
      <category>react</category>
      <category>react19</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Lesser know JavaScript array methods part -1</title>
      <dc:creator>ShreenidhiBatavi</dc:creator>
      <pubDate>Wed, 10 Apr 2024 15:38:29 +0000</pubDate>
      <link>https://forem.com/shreenidhibatavi/lesser-know-javascript-array-methods-11d4</link>
      <guid>https://forem.com/shreenidhibatavi/lesser-know-javascript-array-methods-11d4</guid>
      <description>&lt;p&gt;Arrays are fundamental building blocks in programming.We are familiar with accessing any element and changing its value with tradition method i.e using index and square bracket notation&lt;/p&gt;

&lt;p&gt;while that is a common way, JavaScript offers new &amp;amp; alternative method i.e array.with(). This method is used to replace the value of an element at a specific index in an array with a new value, while returning a new array with the updated elements. The method takes two arguments: the index of the element to be replaced, and the new value to be assigned to that index&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.with(index,value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8og1h7dyt02sf713xn9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8og1h7dyt02sf713xn9.png" alt="array.with() method" width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Advantages of this method over bracket notation is, by giving negative index, it is possible to traverse in reverse order of array eliminating the need for calculations like array.length - number&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gcz022hdg0hqq4w443w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gcz022hdg0hqq4w443w.png" alt="array.with() method" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there are empty slots in original array , then this method replaces it with undefined and return a new array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, , 4,];
console.log(arr.with(1, 8)); // [1, 8, undefined ,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
      <category>svelte</category>
    </item>
    <item>
      <title>A Quick Introduction to CSS container queries</title>
      <dc:creator>ShreenidhiBatavi</dc:creator>
      <pubDate>Mon, 06 Nov 2023 18:20:37 +0000</pubDate>
      <link>https://forem.com/shreenidhibatavi/a-quick-introduction-to-css-container-queries-45o0</link>
      <guid>https://forem.com/shreenidhibatavi/a-quick-introduction-to-css-container-queries-45o0</guid>
      <description>&lt;p&gt;CSS container queries are a new feature that is added into CSS and that can be a game changer in developing responsive layouts, especially in the era of component based ui development.&lt;/p&gt;

&lt;p&gt;we all have been using media queries from long time and this container queries will be an alternative to media queries and on top of that it provides more flexibility.&lt;/p&gt;

&lt;p&gt;responsive layouts are developed with help of media queries, but we all know that media queries works based on screen resolution&lt;/p&gt;

&lt;p&gt;but the container queries gives us more flexibility by styling the element based on the size of the container rather than resolution of screen. Which means that we can apply styling to all the elements inside a container based on the size of container&lt;/p&gt;

&lt;p&gt;Let’s try to understand this with simple and basic example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bHNUGjj8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2ezmt23fyvyzlhdsm3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bHNUGjj8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2ezmt23fyvyzlhdsm3s.png" alt="Image description" width="800" height="623"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have a div with class name as card which contains an image and some dummy text inside p tag.This card is wrapped inside another div with class as main and this main div will be our container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JXprSd-K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f688s6n1ud02bjz6jq9e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JXprSd-K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f688s6n1ud02bjz6jq9e.png" alt="Image description" width="800" height="435"&gt;&lt;/a&gt;&lt;br&gt;
with some basic styling i.e by displaying it as flex and some background colour the image and text are displayed adjacent to each other in a row.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hTH_FbcC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8j4ufk2qlapl0lsg087.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hTH_FbcC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8j4ufk2qlapl0lsg087.png" alt="Image description" width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The adjacent placement of the image and card looks good on larger screens, but on smaller screens, they need to be stacked in a column.&lt;/p&gt;

&lt;p&gt;Until now, we have used media queries to achieve this.Now let’s try with help of container queries.In order to do that the main div needs to be defined as container.&lt;/p&gt;

&lt;p&gt;To make this main div as container we have to define container type property as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aiLkstWu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11gamaowf1cn8ym8ooz1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aiLkstWu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11gamaowf1cn8ym8ooz1.png" alt="Image description" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To make it as container we have to set container-type property to as inline-size,basically container-type property can have two of types of values one is inline-size and another one is size. lets focus on inline-size as most probably we will be using this value itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GWPPnOjr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0c8kchrg6v9gqrq7cp3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GWPPnOjr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0c8kchrg6v9gqrq7cp3.png" alt="Image description" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to write the responsive styling using container queries. Container queries are similar to media queries, but instead of the media keyword, we use the @container keyword. We then define the size of the container. Remember that the size we define is for the container, not for the screen. In the above code, for card class we are defining the flex direction as column. This will be applied as soon as the container size goes less than 600px, and the image and paragraph will be stacked one below the other as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PE-TA8Ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04ldaxr8edva7sxuetu8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PE-TA8Ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04ldaxr8edva7sxuetu8.png" alt="Image description" width="590" height="843"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;yes that is it! This is how you can write container queries.&lt;/p&gt;

&lt;p&gt;Before ending this article, lets try to understand few points about container queries&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nested container queries: If an element is nested within multiple containers, the styles within the inner most containers are influenced by its close or immediate parent container.&lt;/li&gt;
&lt;li&gt;Named container queries: We can also use named container queries to target specific container elements. To define a named container, we use the container-name property and give it any value we want.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---OxOXe_---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e78pusis5emglblfncsg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---OxOXe_---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e78pusis5emglblfncsg.png" alt="Image description" width="800" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we are defining named container with container-name as property and card as value&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rWKhbRtg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsu0gnym7xocoe0102da.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rWKhbRtg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsu0gnym7xocoe0102da.png" alt="Image description" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This container query will only apply the styles to the .card element if the container named card has an inline size of 600px or less.&lt;/p&gt;

&lt;p&gt;That’s it for now! Thanks for reading. See you in the next post!&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
