<?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: Muhammad Haseeb</title>
    <description>The latest articles on DEV Community by Muhammad Haseeb (@haseeb1009).</description>
    <link>https://dev.to/haseeb1009</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%2F542856%2F78a6b0ef-92c5-4434-a3bc-f13943666cfb.jpg</url>
      <title>DEV Community: Muhammad Haseeb</title>
      <link>https://dev.to/haseeb1009</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/haseeb1009"/>
    <language>en</language>
    <item>
      <title>API vs MCP: Understanding the Shift From Developer Tools to AI Agents</title>
      <dc:creator>Muhammad Haseeb</dc:creator>
      <pubDate>Mon, 19 Jan 2026 10:58:06 +0000</pubDate>
      <link>https://dev.to/haseeb1009/api-vs-mcp-understanding-the-shift-from-developer-tools-to-ai-agents-5dcn</link>
      <guid>https://dev.to/haseeb1009/api-vs-mcp-understanding-the-shift-from-developer-tools-to-ai-agents-5dcn</guid>
      <description>&lt;p&gt;If you have been building software for a while, APIs probably feel second nature. They power almost everything we do, from fetching data to connecting services and automating workflows.&lt;/p&gt;

&lt;p&gt;That model starts to feel limiting once AI agents enter the picture. APIs were built for humans writing deterministic code, while MCP is designed for AI systems that need to understand tools, reason about options, and decide what to do next.&lt;/p&gt;

&lt;p&gt;This post explores that shift in a practical way.&lt;/p&gt;

&lt;h2&gt;
  
  
  What APIs Are Optimized For
&lt;/h2&gt;

&lt;p&gt;An API is a predefined contract. A developer reads documentation, understands the endpoints, and writes code that calls them.&lt;/p&gt;

&lt;p&gt;A typical API driven flow looks like this:&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%2F6ml3w5121kq8zj7mzgx7.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%2F6ml3w5121kq8zj7mzgx7.png" alt=" " width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Everything is explicit. The developer already knows what to call and when to call it. The intelligence lives in the application logic, not in the API itself.&lt;/p&gt;

&lt;p&gt;APIs assume the caller already understands the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why APIs Struggle With AI Agents
&lt;/h2&gt;

&lt;p&gt;Now imagine the caller is not a developer, but an AI agent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The agent does not naturally know:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which endpoints exist&lt;/li&gt;
&lt;li&gt;Which parameters are required&lt;/li&gt;
&lt;li&gt;Which system should be called first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can hardcode this knowledge into prompts or glue code, but that approach becomes fragile very quickly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every new integration needs custom logic&lt;/li&gt;
&lt;li&gt;Every API change risks breaking the agent&lt;/li&gt;
&lt;li&gt;Every workflow must be defined upfront&lt;/li&gt;
&lt;/ul&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%2F6k59ez0bftxnskccoaip.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%2F6k59ez0bftxnskccoaip.png" alt=" " width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the gap MCP tries to fill.&lt;/p&gt;

&lt;h2&gt;
  
  
  What MCP Brings
&lt;/h2&gt;

&lt;p&gt;MCP stands for Model Context Protocol.&lt;/p&gt;

&lt;p&gt;Instead of exposing raw endpoints, MCP exposes capabilities in a structured and machine readable way. An AI model can ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What tools are available&lt;/li&gt;
&lt;li&gt;What inputs do they expect&lt;/li&gt;
&lt;li&gt;What output will I get&lt;/li&gt;
&lt;/ul&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%2F2xxfjop6tgkf1o7mdu5q.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%2F2xxfjop6tgkf1o7mdu5q.png" alt=" " width="800" height="638"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The key difference is simple.&lt;/p&gt;

&lt;p&gt;APIs tell you how to call something.&lt;br&gt;
MCP tells the model what is possible.&lt;/p&gt;

&lt;p&gt;This allows AI systems to reason about tools instead of following rigid instructions.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Practical Example
&lt;/h2&gt;

&lt;p&gt;Imagine you are building an AI travel assistant.&lt;br&gt;
The user says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I want to travel next week but avoid bad weather.&lt;/p&gt;
&lt;/blockquote&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%2F65tdsqocq41hedenqaj5.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%2F65tdsqocq41hedenqaj5.png" alt=" " width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  With APIs
&lt;/h2&gt;

&lt;p&gt;The workflow is predefined by a developer.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;User Request&lt;br&gt;
↓&lt;br&gt;
Hardcoded Logic&lt;br&gt;
↓&lt;br&gt;
Weather API&lt;br&gt;
↓&lt;br&gt;
Flight API&lt;br&gt;
↓&lt;br&gt;
Hotel API&lt;br&gt;
↓&lt;br&gt;
Response&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The AI is following a script. Adding a new service later means rewriting the flow.&lt;/p&gt;

&lt;h2&gt;
  
  
  With MCP
&lt;/h2&gt;

&lt;p&gt;The MCP server exposes tools like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search flights&lt;/li&gt;
&lt;li&gt;Book hotels&lt;/li&gt;
&lt;li&gt;Check weather&lt;/li&gt;
&lt;li&gt;Create calendar events&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The AI decides the order dynamically.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;User Request&lt;br&gt;
↓&lt;br&gt;
AI Reasoning&lt;br&gt;
↓&lt;br&gt;
Tool Discovery&lt;br&gt;
↓&lt;br&gt;
Check Weather&lt;br&gt;
↓&lt;br&gt;
Search Flights&lt;br&gt;
↓&lt;br&gt;
Suggest Hotels&lt;br&gt;
↓&lt;br&gt;
Response&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Nothing is hardcoded. The AI chooses the path based on intent and context.&lt;/p&gt;

&lt;p&gt;Why MCP Matters&lt;/p&gt;

&lt;p&gt;APIs assume certainty.&lt;br&gt;
AI works best with exploration and decision making.&lt;/p&gt;

&lt;p&gt;MCP acts as a bridge between AI reasoning and real world systems. It does not replace APIs. It makes them usable by AI.&lt;/p&gt;

&lt;p&gt;Think of APIs as building blocks.&lt;br&gt;
Think of MCP as instructions written for machines.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use What
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use APIs when&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance and predictability matter&lt;/li&gt;
&lt;li&gt;Workflows are fixed&lt;/li&gt;
&lt;li&gt;Humans control the business logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use MCP when&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are building AI agents&lt;/li&gt;
&lt;li&gt;Workflows need to adapt dynamically&lt;/li&gt;
&lt;li&gt;Tools change frequently&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This is not API versus MCP.&lt;br&gt;
This is API and MCP working together.&lt;/p&gt;

&lt;p&gt;APIs remain the foundation. MCP adds a layer that allows AI to use that foundation intelligently.&lt;/p&gt;

&lt;p&gt;As AI agents become more common, understanding this distinction will matter not because MCP is trendy, but because it matches how AI actually works.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>mcp</category>
      <category>api</category>
    </item>
    <item>
      <title>How Frontend Engineers Can Make AI Accessible: Building Intuitive Interfaces for AI-Powered Apps</title>
      <dc:creator>Muhammad Haseeb</dc:creator>
      <pubDate>Mon, 24 Mar 2025 03:27:38 +0000</pubDate>
      <link>https://dev.to/haseeb1009/how-frontend-engineers-can-make-ai-accessible-building-intuitive-interfaces-for-ai-powered-apps-1dj7</link>
      <guid>https://dev.to/haseeb1009/how-frontend-engineers-can-make-ai-accessible-building-intuitive-interfaces-for-ai-powered-apps-1dj7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As a &lt;strong&gt;Frontend Engineer&lt;/strong&gt;, I’ve always been fascinated by how we can make complex technologies simple and accessible for users. But here’s the thing: AI is no longer just a backend marvel—it’s becoming a &lt;strong&gt;frontend responsibility&lt;/strong&gt;. Think about it: tools like &lt;strong&gt;ChatGPT&lt;/strong&gt;, &lt;strong&gt;AI-driven dashboards&lt;/strong&gt;, and &lt;strong&gt;personalized recommendation systems&lt;/strong&gt; are only as good as their interfaces. In this post, I’ll share actionable tips on how we, as frontend developers, can bridge the gap between AI and end-users through &lt;strong&gt;intuitive and engaging interfaces&lt;/strong&gt;. Let’s dive in!&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Frontend Matters in AI 🚀
&lt;/h2&gt;

&lt;p&gt;AI is powerful, but its true potential is unlocked when users can interact with it seamlessly. Think about tools like &lt;strong&gt;ChatGPT&lt;/strong&gt;, &lt;strong&gt;AI-driven dashboards&lt;/strong&gt;, or &lt;strong&gt;personalized recommendation systems&lt;/strong&gt;. The frontend is where users experience the magic of AI. As frontend engineers, our job is to make this experience smooth, intuitive, and delightful.&lt;/p&gt;

&lt;h2&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%2F8ak84o24a0lugjwcumrw.png" alt="chatbots, live recommendations" width="800" height="457"&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Key Challenges in AI-Powered Frontends
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Complexity vs. Simplicity&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AI models often produce complex outputs. Our challenge is to present these outputs in a way that’s easy to understand.&lt;/li&gt;
&lt;li&gt;Example: Instead of showing raw data, use visualizations like charts, graphs, or natural language summaries.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Real-Time Interactions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Many AI applications require real-time updates (e.g., chatbots, live recommendations). This means optimizing for performance and ensuring smooth user interactions.&lt;/li&gt;
&lt;li&gt;Tools like &lt;strong&gt;WebSockets&lt;/strong&gt; or &lt;strong&gt;Server-Sent Events (SSE)&lt;/strong&gt; can help here.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Handling Uncertainty&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AI isn’t perfect. Sometimes it produces unexpected or incorrect results. As frontend developers, we need to design interfaces that handle these edge cases gracefully.&lt;/li&gt;
&lt;li&gt;Example: Provide fallback options or clear error messages when the AI fails.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Tips for Building AI-Powered Frontends
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Leverage Modern Frameworks&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use frameworks like &lt;strong&gt;Vue.js&lt;/strong&gt;, &lt;strong&gt;React&lt;/strong&gt;, or &lt;strong&gt;Svelte&lt;/strong&gt; to build dynamic, responsive interfaces that can handle real-time AI interactions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Focus on UX&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AI can feel intimidating to users. Use &lt;strong&gt;progressive disclosure&lt;/strong&gt;—show only what’s necessary and reveal more details as the user interacts.&lt;/li&gt;
&lt;li&gt;Example: Start with a simple input field and gradually introduce advanced options.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Visualize AI Outputs&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Turn raw AI data into something meaningful. Use libraries like &lt;strong&gt;D3.js&lt;/strong&gt; or &lt;strong&gt;Chart.js&lt;/strong&gt; to create interactive visualizations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Test, Test, Test&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AI behavior can be unpredictable. Test your frontend with different AI outputs to ensure it handles all scenarios gracefully.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&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%2Fzle8aqm15nuuz1ce76fe.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%2Fzle8aqm15nuuz1ce76fe.png" alt="An image showing raw data on one side and a clean, visually appealing chart on the other, symbolizing the transformation of complex AI outputs into simple visuals." width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;




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

&lt;p&gt;AI is transforming the way we build applications, and as frontend engineers, we have a unique opportunity to shape how users interact with this technology. By focusing on &lt;strong&gt;simplicity&lt;/strong&gt;, &lt;strong&gt;performance&lt;/strong&gt;, and &lt;strong&gt;user experience&lt;/strong&gt;, we can make AI accessible to everyone.&lt;/p&gt;

&lt;p&gt;What’s your take on AI-powered frontends? Have you worked on any projects that combine AI and frontend development? Share your experiences or questions in the comments—I’d love to hear from you! 👇&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>aiapplications</category>
    </item>
    <item>
      <title>Best Time to Refactor: Tips for Refactoring a Legacy Code Base</title>
      <dc:creator>Muhammad Haseeb</dc:creator>
      <pubDate>Sun, 16 Jun 2024 11:31:05 +0000</pubDate>
      <link>https://dev.to/haseeb1009/best-time-to-refactor-tips-for-refactoring-a-legacy-code-base-1b51</link>
      <guid>https://dev.to/haseeb1009/best-time-to-refactor-tips-for-refactoring-a-legacy-code-base-1b51</guid>
      <description>&lt;p&gt;As software engineers, we all know that refactoring is important for maintaining and improving code quality. However, its very common being lazy.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Imagine a scenario where, during a code review, someone identifies a code smell and suggests some changes. You might respond by saying there's no time for that now and that we'll add it to our technical debt backlog. Or perhaps you leave a few &lt;strong&gt;TODO&lt;/strong&gt; comments instead of taking the time to write clean code. It’ll get done eventually, right?&lt;br&gt;
No! In reality, these tasks rarely get completed once they’re consigned to the limbo of “to be done later.” There's always something more urgent than refactoring.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this post, we'll explore some tips for deciding the best time to refactor and how to do it in a legacy codebase.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Time to Refactor
&lt;/h3&gt;

&lt;p&gt;Refactoring is a continuous process that should be done throughout the development cycle. So, if I had to answer that in one word that would be &lt;strong&gt;NOW&lt;/strong&gt;. Pretty explicit from the cover!&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Its always the best time to refactor"!&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&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%2Fqzfhe8hssvdfx0j7t0m2.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%2Fqzfhe8hssvdfx0j7t0m2.png" alt="Image description" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;However, there are certain times when you will find the opportunity to do refactoring.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. When Adding New Features&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Adding new features to an existing code base can be a good opportunity to refactor. Refactoring before adding new features can help to reduce the technical debt and improve the overall quality of the codebase. It's easier to refactor before adding new features than after.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. When Fixing Bugs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fixing bugs can also be a good time to refactor. If a bug is caused by poorly written code, refactoring can help to prevent similar bugs from happening in the future. Refactoring while fixing bugs can also make the code easier to understand and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. When Working on Code That's Already Being Touched&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're already working on a specific section of the codebase and you feel a room for improvement, it can be a good opportunity to refactor that section as well. This can help to improve the quality of the codebase and reduce technical debt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactoring in a Legacy Code Base
&lt;/h2&gt;

&lt;p&gt;Refactoring in a legacy code base can be challenging. Legacy codebases are often large, complex, and poorly documented, making it difficult to understand the code's behavior and dependencies. Here are some tips for refactoring a legacy code base:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Understand the Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before you start refactoring, it's essential to understand the codebase's structure and behavior. This includes understanding how different components interact with each other, identifying areas of the codebase that are prone to errors, and determining the code's overall complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Identify Code Smells&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Identifying code smells is crucial for effective refactoring in a legacy codebase. Common code smells include duplicated code, long methods, and excessive nesting. Identifying these code smells can help you prioritize which sections of the codebase need to be refactored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Start Small&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Refactoring a legacy code base can be overwhelming, so it's essential to start small. Identify small sections of the codebase that can be refactored without impacting the rest of the codebase's behavior. Starting small can help you build momentum and reduce the risk of breaking the codebase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Write Tests&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Writing tests can have a significant impact on refactoring and it help you to ensure that your refactoring efforts don't introduce new bugs or break existing functionality. Tests can also serve as documentation for the codebase, making it easier to understand and maintain in the future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Refactor Incrementally&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Refactoring a legacy codebase can be a time-consuming process, so it's essential to do it incrementally. Refactor small sections of the codebase at a time, and make sure that the code still works as expected after each change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Example: Refactoring with Ag-Grid and Formik
&lt;/h2&gt;

&lt;p&gt;In one of my projects, we used Ag-Grid for an enterprise application to display large amounts of data, hierarchies, and computations, making it highly customizable and extensible. We had numerous computations on the grids and were using Formik for managing the state of the grid as a form.&lt;/p&gt;

&lt;p&gt;Working with a legacy codebase, we faced the need to incorporate enterprise features from newer versions of both Ag-Grid and Formik which had a lot of performance improvements. However, directly updating these libraries was not feasible due to significant breaking changes.&lt;/p&gt;

&lt;p&gt;To address this challenge, we adopted an incremental approach:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Initial Setup:&lt;/strong&gt; We installed the latest versions of Ag-Grid and Formik alongside the existing versions. This allowed us to experiment without disrupting the current functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Starting Small:&lt;/strong&gt; We began by updating a small, manageable section of the application. This section was chosen for its simplicity and minimal dependencies, reducing the risk of introducing errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Creating a Reference Implementation:&lt;/strong&gt; Successfully updating this small section provided us with a reference implementation. This served as a guide for how to handle similar updates throughout the codebase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Gradual Rollout:&lt;/strong&gt; Using the reference implementation, we incrementally updated other sections of the application. This step-by-step approach minimized risks and allowed for continuous testing and verification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Comprehensive Testing:&lt;/strong&gt; Throughout the process, we ensured thorough testing at each stage. Writing and running tests helped us catch and fix issues early, maintaining the stability of the application.&lt;/p&gt;

&lt;p&gt;This methodical and incremental strategy enabled us to modernize the codebase effectively. By breaking the task into smaller, manageable pieces, we were able to mitigate risks, ensure compatibility, and gradually leverage the new features offered by the updated libraries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;_&lt;br&gt;
I would love to hear from you guys on the following:_&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What's the most challenging refactoring you've tackled, and how did you approach it?&lt;/li&gt;
&lt;li&gt;How do you prioritize refactoring in your development workflow?&lt;/li&gt;
&lt;li&gt;What are your favorite refactoring techniques or tools, and why? &lt;/li&gt;
&lt;li&gt;Have you ever faced resistance to refactoring within your team or organization? How did you overcome it? &lt;/li&gt;
&lt;li&gt;How do you balance the trade-off between refactoring for maintainability and delivering new features quickly?&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>The UseEvent Hook</title>
      <dc:creator>Muhammad Haseeb</dc:creator>
      <pubDate>Mon, 05 Sep 2022 08:58:18 +0000</pubDate>
      <link>https://dev.to/haseeb1009/the-useevent-hook-1c8l</link>
      <guid>https://dev.to/haseeb1009/the-useevent-hook-1c8l</guid>
      <description>&lt;p&gt;This article needs prior understanding of Hooks. So, if you are not already familiar please checkout the &lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;official docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I came across this hook while reviewing a PR of one of my colleagues &lt;a href="https://dev.to/cipriananin"&gt;ciprian&lt;/a&gt;. One of my colleagues introduced a hook in his PR and we had a long healthy technical discussion around that as it was solving &lt;a href="https://github.com/facebook/react/issues/14099" rel="noopener noreferrer"&gt;this issue&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Motivation for introducing useEvent
&lt;/h3&gt;

&lt;p&gt;The React core team published a Request for Comment (RFC) for a new React hook: &lt;code&gt;useEvent&lt;/code&gt;. This post attempts to capture what this hook is and how it works.&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%2F7wnjxtbfjtwq2flmgqqs.jpg" 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%2F7wnjxtbfjtwq2flmgqqs.jpg" alt="Image description" width="600" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also here is the proposal for &lt;a href="https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md" rel="noopener noreferrer"&gt;useEvent hook&lt;/a&gt; and It hasn't been released yet (by the time I am writing this post) and its behavior could change.&lt;/p&gt;

&lt;h3&gt;
  
  
  The real problem
&lt;/h3&gt;

&lt;p&gt;let’s wrap our heads around the problem before we jump into what &lt;code&gt;useEvent&lt;/code&gt; is. React’s execution model is largely powered by comparing the current and previous values of &lt;code&gt;state&lt;/code&gt;/&lt;code&gt;props&lt;/code&gt;. This happens in components and in hooks like &lt;code&gt;useEffect&lt;/code&gt;, &lt;code&gt;useMemo&lt;/code&gt;, and &lt;code&gt;useCallback&lt;/code&gt;.&lt;br&gt;
Lets see this example to make better understanding of the problem.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;This &lt;code&gt;onClick&lt;/code&gt; event handler needs to read the currently typed text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Chat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 🟡 Always a different function&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SendButton&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say you want to optimize &lt;code&gt;SendButton&lt;/code&gt; by wrapping it in &lt;code&gt;React.memo&lt;/code&gt;. For this to work, the props need to be shallowly equal between re-renders. The &lt;code&gt;onClick&lt;/code&gt; function will have a different function identity on every re-render, so it will break memoization.&lt;br&gt;
The usual way to approach a problem like this is to wrap the function into &lt;code&gt;useCallback&lt;/code&gt; hook to preserve the function identity. However, it wouldn't help in this case because onClick needs to read the latest text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Chat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 🟡 A different function whenever `text` changes&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SendButton&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The text changes with every keystroke, so &lt;code&gt;onClick&lt;/code&gt; will still be a different function on every keystroke. (We can't remove text from the &lt;code&gt;useCallback&lt;/code&gt; dependencies because otherwise the &lt;code&gt;onClick&lt;/code&gt; handler would always "see" the initial text.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's the solution
&lt;/h3&gt;

&lt;p&gt;A new hook &lt;code&gt;useEvent&lt;/code&gt; is being proposed to ensure we have a stable reference to a function without having to create a new function based on its dependents.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is &lt;code&gt;useEvent&lt;/code&gt; hook?
&lt;/h4&gt;

&lt;p&gt;A Hook to define an event handler with an &lt;strong&gt;always-stable&lt;/strong&gt; function identity.&lt;/p&gt;

&lt;p&gt;By comparison, &lt;code&gt;useEvent&lt;/code&gt; does not take a dependency array and always returns the same stable function, even if the text changes. Nevertheless, text inside &lt;code&gt;useEvent&lt;/code&gt; will reflect its latest value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Chat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// ✅ Always the same function (even if `text` changes)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useEvent&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SendButton&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a result, memoizing &lt;code&gt;SendButton&lt;/code&gt; will now work because its &lt;code&gt;onClick&lt;/code&gt; prop will always receive the same function.&lt;/p&gt;




&lt;h3&gt;
  
  
  When to use useCallBack?
&lt;/h3&gt;

&lt;p&gt;You must be curious about what would be the use-cases where &lt;code&gt;useCallBack&lt;/code&gt; will be useful after we have the &lt;code&gt;useEvent&lt;/code&gt; hook? I had the same question back then.&lt;/p&gt;

&lt;p&gt;Well, &lt;code&gt;useCallaback&lt;/code&gt; will be used for memoizing render functions. &lt;strong&gt;For example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;renderItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;itemName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;itemName&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why&lt;/strong&gt;? because useEvent cannot be used for this scenario as it would keep the same reference for render function, and this is not desired, because we need a changing ref so that we know when to rerender.&lt;/p&gt;

&lt;p&gt;Event handlers wrapped in &lt;code&gt;useEvent&lt;/code&gt; will throw if called during render. (Calling it from an effect or at any other time is fine.) So it is enforced that during rendering these functions are treated as opaque and never called. This makes it safe to preserve their identity despite the changing &lt;code&gt;props&lt;/code&gt;/&lt;code&gt;state&lt;/code&gt; inside.&lt;/p&gt;

&lt;p&gt;You can see many more evidences on that &lt;a href="https://github.com/facebook/react/issues/14099" rel="noopener noreferrer"&gt;issue&lt;/a&gt; and &lt;a href="https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md" rel="noopener noreferrer"&gt;RFC&lt;/a&gt; itself, I am not listing those here to keep this post concise. Overall, I think this is going to be a great addition to the React ecosystem.&lt;/p&gt;




&lt;p&gt;Here is what my colleague did to mock the same behavior that &lt;code&gt;useEvent&lt;/code&gt; would probably be doing soon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useEventCallback&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Args&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;Return&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Return&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ref is not initialized, in order to ensure that we can't call this in the render phase&lt;/span&gt;
  &lt;span class="c1"&gt;// ref.current will be undefined if we call the `fn` during render phase (as a render function)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useLayoutEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;// make sure that the value of `this` provided for the call to fn is not `ref`&lt;/span&gt;
      &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;






&lt;center&gt; Hit the ❤️ if it was useful.
Please share your feedback so I can make my blog posts more helpful. 😊
&lt;/center&gt;




</description>
    </item>
    <item>
      <title>Avoid ignoring react-hooks/exhaustive-deps linting warnings</title>
      <dc:creator>Muhammad Haseeb</dc:creator>
      <pubDate>Tue, 09 Aug 2022 19:08:00 +0000</pubDate>
      <link>https://dev.to/haseeb1009/avoid-ignoring-react-hooksexhaustive-deps-linting-warnings-244f</link>
      <guid>https://dev.to/haseeb1009/avoid-ignoring-react-hooksexhaustive-deps-linting-warnings-244f</guid>
      <description>&lt;p&gt;This post is inspired by the conversation on this &lt;a href="https://github.com/facebook/react/issues/14920#issuecomment-471070149" rel="noopener noreferrer"&gt;github issue&lt;/a&gt; which was shared by a colleague in one of our technical meeting. There are so many other cases discussed here so its worth reading.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rationale
&lt;/h3&gt;

&lt;p&gt;While working with a react hook, you must have been in a situation when you get this linting warning &lt;code&gt;react-hooks/exhaustive-deps&lt;/code&gt;. You try to add the dependency to the array and may ended up in an infinite loop or any unexpected behavior;&lt;/p&gt;

&lt;h4&gt;
  
  
  For example:
&lt;/h4&gt;

&lt;p&gt;You have a state count in your component and you want to increment it only once. Most likely you will write in this way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give you a warning that &lt;code&gt;React Hook useEffect has a missing dependency: 'count'. Either include it or remove the dependency array&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You have an easy exit: that is to ignore the error by disabling the rule;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// eslint-disable-next-line&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// eslint-disable-next-line&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;center&gt;**It worked - Great job!!!  👏** But wait;&lt;/center&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%2Fx1do8qte3no6ikprbcnw.jpeg" 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%2Fx1do8qte3no6ikprbcnw.jpeg" alt="Image" width="586" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The design of &lt;code&gt;useEffect&lt;/code&gt; forces you to confront it. You can of course work around it, but the default is to nudge you to handle these cases. Instead of disabling these warnings, I've found it worth finding a way to accommodate the dependency array requirements.&lt;/p&gt;

&lt;p&gt;In most situations its practically possible and I will talk about some very common use-cases here.&lt;/p&gt;

&lt;h4&gt;
  
  
  For state dependencies
&lt;/h4&gt;

&lt;p&gt;In the preceding example, you can get rid of that warning using a callback in your &lt;code&gt;setCount&lt;/code&gt; method because count will no longer be a dependency 😊&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F47u3kg4t2qcv2ipvamcv.jpeg" 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%2F47u3kg4t2qcv2ipvamcv.jpeg" alt="image" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Another drawback off adding &lt;code&gt;// eslint-disable-next-line&lt;/code&gt; could be that it will not warn you about future dependencies. &lt;strong&gt;i.e&lt;/strong&gt; If you made some changes in that hook which has a dependency but you forgot to add it in the dependency array, you will not get a warning now as you already have disabled that. This could lead to unexpected behavior and even break your app.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  For function dependencies
&lt;/h4&gt;

&lt;p&gt;When you use any function in the &lt;code&gt;useEffect&lt;/code&gt; hook.&lt;br&gt;
Let’s consider you have a function that uses &lt;code&gt;name&lt;/code&gt; as argument and does something. You will get an &lt;code&gt;es-lint&lt;/code&gt; warning for not adding the function in the dependency array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;functionName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// do something&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, even adding it directly to the dependency wont fix it and you will get another warning that &lt;code&gt;you have just caused an infinite loop because someFunction has a different reference on each render&lt;/code&gt;. So whats the fix?&lt;/p&gt;

&lt;p&gt;The solution here is likely to wrap that function in a &lt;code&gt;useCallback&lt;/code&gt; hook because &lt;code&gt;useCallBack&lt;/code&gt; will return a memoized callback. It makes certain that the function doesn’t change references between renders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;functionName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// do something with count&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;blockquote&gt;
&lt;p&gt;As per official &lt;a href="https://reactjs.org/docs/hooks-reference.html#usecallback" rel="noopener noreferrer"&gt;useCallBack&lt;/a&gt; docs make sure that every value referenced inside the callback should also appear in the dependencies array.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Here is another use-case I saw on the same github issue related to useCallBack, where a user will add an email and invite those email to the app. It potentially fix the deps into &lt;code&gt;[emails, props]&lt;/code&gt;. Whereas, actually it needs to update the function reference only when &lt;code&gt;onSubmit&lt;/code&gt; change and not any other &lt;code&gt;prop&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleSubmit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emails&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;emails&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This is because technically props.onSubmit() passes props itself as &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;onSubmit&lt;/code&gt; call. So &lt;code&gt;onSubmit&lt;/code&gt; might implicitly depend on props and the best practice is always de-structuring.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Valid-Case
&lt;/h3&gt;

&lt;p&gt;I also want to talk about one of the legitimate cases that I found on the &lt;a href="https://github.com/facebook/react/issues/14920#issuecomment-467896512" rel="noopener noreferrer"&gt;same thread&lt;/a&gt;, where you may have to disable extraneous dependency warning&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scrollTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;activeTab&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Its very common to disable/ignore &lt;code&gt;react-hooks/exhaustive-deps&lt;/code&gt; linting warnings in most cases. Probably you actually want to fire the hook when the dependency changes. It might not seem like it at first, but there are probably some edge cases for which your effect logic will fail if you don’t include the variable in the dependency array. So, you should try to fulfill react hook dependency requirements with given ways instead disabling. &lt;/p&gt;




&lt;center&gt; Hit the ❤️ if it was helpful and you learn something new &lt;/center&gt;




</description>
    </item>
    <item>
      <title>Do we even need if/else?</title>
      <dc:creator>Muhammad Haseeb</dc:creator>
      <pubDate>Tue, 07 Dec 2021 05:58:08 +0000</pubDate>
      <link>https://dev.to/haseeb1009/do-we-even-need-ifelse-4722</link>
      <guid>https://dev.to/haseeb1009/do-we-even-need-ifelse-4722</guid>
      <description>&lt;p&gt;Flow control is one of the first things we all learn as programmers.&lt;br&gt;
We are going to learn about some of the alternatives we have, that from my point of view are generally more cleaner and safer.&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%2Fjcy0oh31fiabvyp1lo8g.jpg" 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%2Fjcy0oh31fiabvyp1lo8g.jpg" alt="clean code" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lets take a simple example to start with;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  if/Ternary
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isWeekend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sunday&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a weekend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We have a function &lt;code&gt;isWeekend&lt;/code&gt; which takes a &lt;code&gt;day&lt;/code&gt;and returns if &lt;em&gt;its a weekend or not&lt;/em&gt;. Now this has an issue, but JavaScript itself doesn't give us any kind of error. We didn't return any message if its not &lt;em&gt;sunday&lt;/em&gt;. So we can do something like this or add an else block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isWeekend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sunday&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a weekend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, as As the title says, &lt;strong&gt;do we even need if&lt;/strong&gt; for this simple conditional block? No, we can use a ternary instead. &lt;br&gt;
So we can update our function isWeekend like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isWeekend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a weekend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// or&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isWeekend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Its a ${ day === "sunday" ? "weekend" : "working day :(" }&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages of ternaries over ifs:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It forces to cover both &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt; case.&lt;/li&gt;
&lt;li&gt;Less code footprints.&lt;/li&gt;
&lt;li&gt;More readable.&lt;/li&gt;
&lt;li&gt;Another big advantage is that we can initialize a constant based on condition i.e.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We cannot achieve this using &lt;code&gt;if&lt;/code&gt; &lt;code&gt;else&lt;/code&gt; and will have to use &lt;code&gt;let&lt;/code&gt; instead of &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  if/Switch
&lt;/h3&gt;

&lt;p&gt;But what if we have to cover multiple conditions. Instead of using multiple &lt;code&gt;ifs&lt;/code&gt; we should use the &lt;code&gt;switch&lt;/code&gt; statement. Let take the same example, this time we need to have a condition for all the possible days i.e.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// multiple case switch program&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;monday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tuesday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wednesday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;thursday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;friday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;saturday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a weekend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;thats not even a day&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Plain Objects and the ??
&lt;/h3&gt;

&lt;p&gt;We can even use plain objects and the &lt;code&gt;nullish&lt;/code&gt; operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;daysMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;monday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tueday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wednesday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;thursday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;friday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Its a working day :(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;saturday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;its a weekend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;its a weekend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;thats not even a day&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isWeekend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;daysMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those who are not familiar with the &lt;code&gt;??&lt;/code&gt; operator, it checks either it has a value or &lt;code&gt;nullish&lt;/code&gt; (&lt;strong&gt;null or undefined&lt;/strong&gt;). If day is &lt;code&gt;nullish&lt;/code&gt;, then we use defaultValue, it not, use the value itself.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion:
&lt;/h4&gt;

&lt;p&gt;There might be some cases where we have to use if/else. But, in my opinion, we can use the alternatives in most cases.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Any thought??&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#coding #softwareengineering #productivity #cleancode #codingtips #javascript #webdev #devlife #programming #computerscience&lt;/code&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>typescript</category>
      <category>codequality</category>
    </item>
    <item>
      <title>How to reuse component's props in TypeScript</title>
      <dc:creator>Muhammad Haseeb</dc:creator>
      <pubDate>Mon, 29 Nov 2021 13:37:26 +0000</pubDate>
      <link>https://dev.to/haseeb1009/how-to-reuse-components-props-in-typescript-51m4</link>
      <guid>https://dev.to/haseeb1009/how-to-reuse-components-props-in-typescript-51m4</guid>
      <description>&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%2F7of21h7z4nsqimhak00i.jpg" 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%2F7of21h7z4nsqimhak00i.jpg" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We often find ourself defining types for the props that are not actually part of that component and are used in some other component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Lets take a simple example:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Suppose we have a Parent and a Child component with following props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;⚠️ &lt;strong&gt;Problem Statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Repetitive Props&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Type for &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; are also defined in Parent because it needs to be passed down to the Child component. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Code Sync issues&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If one part changes we need to make sure the others stays in sync.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the Child component is used in a similar pattern elsewhere we need to make sure we update the props there too.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can use ComponentProps to extract the Props directly form the component's type and use rest and spread syntax to avoid repetition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ComponentProps&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ComponentProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;childProps&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;childProps&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Creating the parent's props by merging them with it's child's solves the code sync issues.&lt;/li&gt;
&lt;li&gt;Makes a unique source of truth.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; &lt;em&gt;If you only need some of the props in the Child component, you can use Partials.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#coding #softwareengineering #productivity #webdeveloper #cleancode #codingtips #javascript #webdev #devlife #programming #computerscience&lt;/code&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>typescript</category>
      <category>ux</category>
    </item>
  </channel>
</rss>
