<?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: niketan wadaskar</title>
    <description>The latest articles on DEV Community by niketan wadaskar (@niketanwadaskar).</description>
    <link>https://dev.to/niketanwadaskar</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%2F1118121%2Ff9e96244-146d-426c-9fb8-fab2d6022a49.png</url>
      <title>DEV Community: niketan wadaskar</title>
      <link>https://dev.to/niketanwadaskar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niketanwadaskar"/>
    <language>en</language>
    <item>
      <title>Why Can’t We Use async with useEffect but Can with componentDidMount?</title>
      <dc:creator>niketan wadaskar</dc:creator>
      <pubDate>Mon, 10 Jun 2024 13:26:01 +0000</pubDate>
      <link>https://dev.to/niketanwadaskar/why-cant-we-use-async-with-useeffect-but-can-with-componentdidmount-45be</link>
      <guid>https://dev.to/niketanwadaskar/why-cant-we-use-async-with-useeffect-but-can-with-componentdidmount-45be</guid>
      <description>&lt;p&gt;React is full of neat tricks and some puzzling limitations. One such quirk is the inability to directly use &lt;code&gt;async&lt;/code&gt; functions in the &lt;code&gt;useEffect&lt;/code&gt; hook, unlike in the &lt;code&gt;componentDidMount&lt;/code&gt; lifecycle method of class components. Let’s dive into why this is the case and how you can work around it without pulling your hair out!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Basics: Why &lt;code&gt;useEffect&lt;/code&gt; Doesn't Like async Functions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Function Signature:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What &lt;code&gt;useEffect&lt;/code&gt; Wants: It expects its callback to return either nothing (undefined) or a cleanup function.&lt;/li&gt;
&lt;li&gt;What &lt;code&gt;async&lt;/code&gt; Gives: An &lt;code&gt;async&lt;/code&gt; function always returns a Promise, which doesn’t fit the return expectations of &lt;code&gt;useEffect&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cleanup Function:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;useEffect&lt;/code&gt;: If you need to perform cleanup (like clearing timers, cancelling subscriptions, etc.), you return a cleanup function.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;async&lt;/code&gt;: An &lt;code&gt;async&lt;/code&gt; function cannot directly return this cleanup function because it returns a promise, leaving React unsure of what to do. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Expected Return Value:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt; Expects: Either &lt;code&gt;undefined&lt;/code&gt; or a cleanup function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; Provides: A Promise, which doesn't align with this requirement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's a quick summary: &lt;code&gt;useEffect&lt;/code&gt; wants either nothing or a cleanup function, but an &lt;code&gt;async&lt;/code&gt; function always returns a Promise. Imagine ordering a coffee and getting a promise to deliver it someday—useEffect is just not cool with that.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Difference with componentDidMount&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Lifecycle Methods:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;componentDidMount&lt;/code&gt;: Called after the component has rendered and the DOM is ready. You can use &lt;code&gt;async&lt;/code&gt; functions within &lt;code&gt;componentDidMount&lt;/code&gt; because it doesn’t have the same requirement for a cleanup function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt;: Designed to handle side effects (like data fetching, subscriptions, or DOM manipulations) in functional components. Since &lt;code&gt;useEffect&lt;/code&gt; expects a cleanup function, using an async function directly would lead to unexpected behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Synchronous Nature:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;componentDidMount&lt;/code&gt;: Inherently synchronous. You can call an async function within it to perform side effects without needing to return a cleanup function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt;: Expects a cleanup function, so using async directly leads to unexpected behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Recommended Approaches: The Workarounds&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Since useEffect and async functions don’t mix well, here are some ways to handle async operations without causing React to throw a tantrum.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Immediately Invoked Function Expression (IIFE)&lt;/strong&gt;&lt;br&gt;
  Wrap your async logic inside an IIFE to keep your useEffect callback synchronous.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    (async () =&amp;gt; {
        const data = await fetchSomeData();
        console.log(data);
    })();
}, []);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Separate Function Declaration&lt;/strong&gt;&lt;br&gt;
  Declare the async function inside the effect and then call it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
        const data = await fetchSomeData();
        console.log(data);
    };

    fetchData();
}, []);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Cleanup Example&lt;/strong&gt;&lt;br&gt;
Handle cleanup within the async function and ensure it runs synchronously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    let isMounted = true;

    const fetchData = async () =&amp;gt; {
        const data = await fetchSomeData();
        if (isMounted) {
            console.log(data);
        }
    };

    fetchData();

    return () =&amp;gt; {
        isMounted = false;
    };
}, []);

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Understanding why &lt;code&gt;useEffect&lt;/code&gt; doesn't mesh well with &lt;code&gt;async&lt;/code&gt; functions helps you write cleaner, more efficient React code. Remember, the trick is to keep the &lt;code&gt;useEffect&lt;/code&gt; callback synchronous and handle async operations inside it. By wrapping your async logic properly, you can sidestep this quirky limitation and keep your side effects under control.&lt;/p&gt;

&lt;p&gt;Happy coding, and may your React components be ever snappy and bug-free! And remember, just like React, sometimes life gives you a Promise—you just need to handle it right!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding Error Boundaries in React: A Beginner’s Guide</title>
      <dc:creator>niketan wadaskar</dc:creator>
      <pubDate>Mon, 20 May 2024 06:37:19 +0000</pubDate>
      <link>https://dev.to/niketanwadaskar/understanding-error-boundaries-in-react-a-beginners-guide-4pg8</link>
      <guid>https://dev.to/niketanwadaskar/understanding-error-boundaries-in-react-a-beginners-guide-4pg8</guid>
      <description>&lt;p&gt;Hey there, coding friend! Today, we’re going to talk about error boundaries in React. If you’ve ever worked on a React app and had everything crash because of a JavaScript error, error boundaries are here to help. Let’s break it down in simple terms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Error Boundaries?&lt;/strong&gt;&lt;br&gt;
Think of your React app as a big Lego castle. If one piece breaks, you don’t want the whole castle to fall apart, right? Error boundaries are like safety nets that catch errors in specific parts of your app so the whole thing doesn’t crash. They catch errors in their child components, log those errors, and show a backup UI instead of letting your app explode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When Do Error Boundaries Catch Errors?&lt;/strong&gt;&lt;br&gt;
Error boundaries catch errors during:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rendering: When your component is figuring out what to display.&lt;/li&gt;
&lt;li&gt;Lifecycle Methods: During the different stages of a component’s life, like when it starts or updates.&lt;/li&gt;
&lt;li&gt;Constructors: When a component is first created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But, they won’t catch errors in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event Handlers: Things like button click events.&lt;/li&gt;
&lt;li&gt;Asynchronous Code: Promises, setTimeout, or async/await.&lt;/li&gt;
&lt;li&gt;Server-Side Errors: Anything happening on your server.&lt;/li&gt;
&lt;li&gt;Errors in the Error Boundary Itself: Because even the safety net can have holes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How Do Error Boundaries Work?&lt;/strong&gt;&lt;br&gt;
Error boundaries work like the catch block in JavaScript. If an error happens, the error boundary catches it. When JavaScript finds a problem during rendering or in lifecycle methods, it looks for the nearest error boundary to handle it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Making an Error Boundary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To create an error boundary, you need a class component that has one or both of these methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;getDerivedStateFromError&lt;/code&gt; : This method updates the state so you can show a fallback UI when an error is thrown.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;componentDidCatch&lt;/code&gt; : This method logs the error information so you can see what went wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s a simple 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%2Fcpjso28j8ainepdaw3vj.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%2Fcpjso28j8ainepdaw3vj.png" alt="Image description" width="588" height="635"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Placement Matters&lt;/strong&gt;&lt;br&gt;
Where you put your error boundaries is important. If you wrap your entire app in an error boundary, any error will show the fallback UI. If you wrap only specific components, only those components will be affected. It’s like deciding whether to have a fire extinguisher in every room or just one big one for the whole house.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Graceful Error Handling&lt;/strong&gt;&lt;br&gt;
Error boundaries let you handle errors gracefully. Instead of your users seeing a blank screen, they get a friendly “Oops! Something went wrong” message, and you get a logged error to fix later.&lt;/p&gt;

&lt;p&gt;And that’s it! A simple introduction to React error boundaries. They help keep your app running smoothly by catching errors and showing a fallback UI. Now, go and use error boundaries to make your app more reliable!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Exploring Controlled and Uncontrolled Components in React</title>
      <dc:creator>niketan wadaskar</dc:creator>
      <pubDate>Thu, 16 May 2024 12:36:28 +0000</pubDate>
      <link>https://dev.to/niketanwadaskar/exploring-controlled-and-uncontrolled-components-in-react-24gl</link>
      <guid>https://dev.to/niketanwadaskar/exploring-controlled-and-uncontrolled-components-in-react-24gl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Let's Dive In:&lt;/strong&gt;&lt;br&gt;
Imagine you're building a form in your React application. You've got input fields, checkboxes, and maybe even a file upload option. But how do you handle user input? That's where controlled and uncontrolled components come into play.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Meet the Uncontrolled Components:&lt;/strong&gt;&lt;br&gt;
Uncontrolled components are like free spirits—they operate independently of React's state management. They're perfect for scenarios where you need to interact with the DOM directly, like when dealing with file inputs or integrating with third-party libraries. They're flexible, adaptable, and have a mind of their own. But beware—without React's oversight, they can sometimes lead to unexpected surprises.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's Explore with an Example:&lt;/strong&gt;&lt;br&gt;
Imagine you're building a file upload component. With an uncontrolled approach, you let the browser handle the input, and you use refs to access the selected file. It's hands-off, easy to implement, and integrates seamlessly with the browser's behavior.&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%2F0ha3iemuw5xkc8gixpdv.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%2F0ha3iemuw5xkc8gixpdv.png" alt="Image description" width="612" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, Let's Meet the Controlled Components:&lt;/strong&gt;&lt;br&gt;
Controlled components, on the other hand, are the guardians of your form data. They embrace React's state management, giving you precise control over every input. They're predictable, reliable, and offer a structured approach to handling user input. When data integrity is paramount, controlled components are your best friends.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's Explore with an Example:&lt;/strong&gt;&lt;br&gt;
Let's say you're building a controlled input field for a username. With a controlled approach, every keystroke updates the component's state, ensuring real-time synchronization between the input and React's state.&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%2F79p15fm8rmf4asvv8tkm.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%2F79p15fm8rmf4asvv8tkm.png" alt="Image description" width="603" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Choosing the Right Tool for the Job:&lt;/strong&gt;&lt;br&gt;
So, when should you use controlled versus uncontrolled components? It all depends on your use case. Need flexibility and integration with external systems? Go with uncontrolled components. Want predictability and data integrity? Opt for controlled components. And don't be afraid to mix and match based on your application's needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
Controlled and uncontrolled components offer different approaches to handling user input in React. By understanding their strengths and weaknesses, you can choose the right tool for the job and build robust, interactive forms in your applications.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Mastering Higher-Order Components in React: A Guide to DRY Code</title>
      <dc:creator>niketan wadaskar</dc:creator>
      <pubDate>Wed, 15 May 2024 12:29:32 +0000</pubDate>
      <link>https://dev.to/niketanwadaskar/mastering-higher-order-components-in-react-a-guide-to-dry-code-4bil</link>
      <guid>https://dev.to/niketanwadaskar/mastering-higher-order-components-in-react-a-guide-to-dry-code-4bil</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When developing React applications, you may encounter scenarios where components are similar but have slight differences. In smaller projects, this might not pose a problem, but as your application grows, maintaining DRY (Don’t Repeat Yourself) code becomes crucial. Higher-order components (HOCs) provide a powerful abstraction to help you achieve this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Higher-Order Component (HOC)?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Simply put, an HOC is a function that takes a component and returns a new component. This new component wraps up the original one, adding shared functionality while leaving the original component’s behavior unchanged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Do We Need HOCs?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In large React applications, you’ll often find yourself writing similar components with minor variations. Repeating this code can lead to maintenance headaches and bugs. HOCs help by allowing you to define common logic in one place and share it across multiple components. This not only keeps your code DRY but also makes it easier to manage and understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do HOCs work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you have several components that need to fetch data from an API. Instead of writing the data fetching logic in each component, you can create an HOC to handle this. Here’s a simplified example:&lt;/p&gt;

&lt;p&gt;`const withDataFetching = (WrappedComponent, url) =&amp;gt; {&lt;br&gt;
  return class extends React.Component {&lt;br&gt;
    state = {&lt;br&gt;
      data: null,&lt;br&gt;
      loading: true,&lt;br&gt;
      error: null,&lt;br&gt;
    };&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {
  fetch(url)
    .then(response =&amp;gt; response.json())
    .then(data =&amp;gt; this.setState({ data, loading: false }))
    .catch(error =&amp;gt; this.setState({ error, loading: false }));
}

render() {
  return (
    &amp;lt;WrappedComponent
      {...this.props}
      data={this.state.data}
      loading={this.state.loading}
      error={this.state.error}
    /&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;br&gt;
}; `&lt;/p&gt;

&lt;p&gt;In this example, withDataFetching is an HOC that takes a component and a URL. It fetches data from the URL and passes the data, loading state, and any error as props to the wrapped component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of using HOCs:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code Reusability: Share logic across multiple components without repeating code.&lt;/li&gt;
&lt;li&gt;Separation of Concerns: Keep your data fetching logic separate from your UI logic.&lt;/li&gt;
&lt;li&gt;Improved Readability: By abstracting shared functionality, your components become simpler and easier to understand.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Higher-order components are a powerful tool in the React developer’s toolkit. They allow you to keep your code DRY, share functionality across multiple components, and improve the maintainability of your application. Next time you find yourself writing similar components, consider using a HOC to streamline your code. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
