<?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: khush</title>
    <description>The latest articles on DEV Community by khush (@khush).</description>
    <link>https://dev.to/khush</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%2F474673%2F04ce15c6-2b38-4c3b-a95c-a9cd84a7a6a3.png</url>
      <title>DEV Community: khush</title>
      <link>https://dev.to/khush</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khush"/>
    <language>en</language>
    <item>
      <title>Unveiling React 19: New hooks Explained!</title>
      <dc:creator>khush</dc:creator>
      <pubDate>Tue, 12 Mar 2024 09:21:25 +0000</pubDate>
      <link>https://dev.to/khush/unveiling-react-19-new-hooks-explained-cma</link>
      <guid>https://dev.to/khush/unveiling-react-19-new-hooks-explained-cma</guid>
      <description>&lt;p&gt;React 19 is here, and it's bringing some seriously cool stuff to the table. Among the highlights are four new hooks: useOptimistic, useFormStatus, useFormState, and the intriguingly named use. So, let's dive in and see how these hooks can sprinkle some magic into your coding adventures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring React 19’s New Hooks:
&lt;/h2&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;useOptimistic&lt;/em&gt;&lt;/u&gt;: Imagine this: you click a button, and bam! The UI updates instantly, no waiting around for the server to catch up. That's what optimistic UI is all about, and the useOptimistic hook makes it a piece of cake to implement. It allows you to update the UI optimistically before receiving confirmation from the server, providing a seamless user experience. With useOptimistic, you can ensure that your users never have to twiddle their thumbs waiting for feedback again.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/hsvs2d?module=%2Fsrc%2FApp.js"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;useFormStatus&lt;/em&gt;&lt;/u&gt;: The useFormStatus hook manages the status of form fields, handling validation logic and submission state, making form management a breeze. It gives you status information of the last form submission.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/2qyqy4?module=%2Fsrc%2Fapp.js"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;useFormState&lt;/em&gt;&lt;/u&gt;: The useFormState hook serves as a manager for form input states, offering a centralized mechanism to monitor and modify their values efficiently. It allows you to update state based on the outcome of form actions.&lt;/p&gt;

&lt;p&gt;This hook requires two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;fn: This parameter represents the function to execute when the form is submitted or a button is pressed. Upon invocation, this function receives the previous state of the form (initially the initialState that you pass, subsequently its previous return value) as its initial argument, followed by the arguments that a form action normally receives.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;initialState: This argument denotes the initial state value desired for the form. It can be any serializable value. Once the action is initially invoked, this parameter becomes irrelevant.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/yv2vzy?module=%2Fsrc%2FApp.js"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;use&lt;/em&gt;&lt;/u&gt; is a versatile React Hook designed to fetch and utilize the value of a resource, such as a Promise or context, within your components or custom hooks. What sets use apart from other React Hooks is its unique ability to be invoked within loops and conditional statements, such as if blocks. However, it still adheres to the standard requirement that the function utilizing use must be either a Component or another Hook.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/8dg646?module=%2Fsrc%2Fmessage.js"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And that's a wrap, folks! You can read about React 19’s new hooks in depth and their potential uses from the official react documentation:&lt;br&gt;
&lt;a href="https://react.dev/reference/react/use"&gt;&lt;em&gt;use&lt;/em&gt;&lt;/a&gt; , &lt;a href="https://react.dev/reference/react-dom/hooks/useFormState"&gt;&lt;em&gt;useFormState&lt;/em&gt;&lt;/a&gt;,&lt;br&gt;
&lt;a href="https://react.dev/reference/react-dom/hooks/useFormStatus"&gt;&lt;em&gt;useFormStatus&lt;/em&gt;&lt;/a&gt;,&lt;br&gt;
&lt;a href="https://react.dev/reference/react/useOptimistic"&gt;&lt;em&gt;useOptimistic&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, next time you're coding away, keep these hooks in mind. They'll make your life easier and your projects way more awesome. After all, in the world of web dev, it's all about staying fresh. Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>news</category>
    </item>
    <item>
      <title>find() vs filter()</title>
      <dc:creator>khush</dc:creator>
      <pubDate>Tue, 04 May 2021 08:12:07 +0000</pubDate>
      <link>https://dev.to/khush/find-vs-filter-2528</link>
      <guid>https://dev.to/khush/find-vs-filter-2528</guid>
      <description>&lt;p&gt;Many of us are accustomed to using to two common functions in js, that is, filter() and find(). A lot of the times we use these functions without paying a lot attention to it. Let's discuss in which situations should you actually use them.&lt;/p&gt;

&lt;h2&gt;
  
  
  find():
&lt;/h2&gt;

&lt;p&gt;find is a function that iterates through an array and returns the first element in the array that matches the given predicate.&lt;br&gt;
eg. find the person with name &lt;em&gt;Amy&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const arr = [{id: 1, name: 'Tom'}, {id: 2, name: 'Claudia'}, {id: 3, name: 'Richard'}, {id: 4, name: 'Amy'}, {id: 5, name: 'Rebecca'}];
console.log(arr.find((person) =&amp;gt; person.name === 'Amy'));
  &lt;/code&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  filter():
&lt;/h2&gt;

&lt;p&gt;filter is a function that iterates through an array and returns a new &lt;em&gt;array&lt;/em&gt; of all elements in the array that matches the given predicate.&lt;br&gt;
eg. finding all person whose name's start with &lt;em&gt;'R'&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const arr = [{id: 1, name: 'Tom'}, {id: 2, name: 'Claudia'}, {id: 3, name: 'Richard'}, {id: 4, name: 'Amy'}, {id: 5, name: 'Rebecca'}];
console.log(arr.filter((person) =&amp;gt; person.name[0] === 'R'));
  &lt;/code&gt;
&lt;/div&gt;


&lt;p&gt;Now, that we have seen what these two functions essentially do let's see when should we use them.&lt;/p&gt;

&lt;p&gt;filter() basically runs through the entire array to see if the given predicate is matched which means you should be only using it if you expect more than one item to match this predicate. Although, this seems like a pretty simple thing to remember a lot of the times we see people using filter in place of find().&lt;br&gt;
When you want to search for an element in the array use find() instead of returning an array from filter and doing something like &lt;code&gt;resultOfFilter[0]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The main advantage of using find() in those cases is that find returns as soon as a match is found, but filter would run through the entire array even if a match is found in the &lt;code&gt;1 st index position&lt;/code&gt; and you don't want to do that for large arrays when all you are concerned about is one match!&lt;/p&gt;

&lt;p&gt;See below how filter iterates through all the elements while find returns on the first match.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const arr = [{id: 1, name: 'Tom'}, {id: 2, name: 'Claudia'}, {id: 3, name: 'Richard'}, {id: 4, name: 'Amy'}, {id: 5, name: 'Rebecca'}];
console.log(arr.filter((person) =&amp;gt; { 
console.log('inside filter, current name: ',person.name);
return person.name === 'Tom' }));
console.log(arr.find((person) =&amp;gt; { 
console.log('inside find, current name: ',person.name);
return person.name === 'Tom' }));
  &lt;/code&gt;
&lt;/div&gt;


</description>
      <category>javascript</category>
      <category>uiweekly</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
