<?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: HitanshiMehta</title>
    <description>The latest articles on DEV Community by HitanshiMehta (@hitanshimehta).</description>
    <link>https://dev.to/hitanshimehta</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%2F793486%2F1aedeb4c-444f-41cc-a5b9-d507c65d1b5f.png</url>
      <title>DEV Community: HitanshiMehta</title>
      <link>https://dev.to/hitanshimehta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hitanshimehta"/>
    <language>en</language>
    <item>
      <title>Feeling Overworked? Meet Your Secret Santa</title>
      <dc:creator>HitanshiMehta</dc:creator>
      <pubDate>Thu, 01 Aug 2024 09:49:50 +0000</pubDate>
      <link>https://dev.to/hitanshimehta/feeling-overworked-meet-your-secret-santa-2l89</link>
      <guid>https://dev.to/hitanshimehta/feeling-overworked-meet-your-secret-santa-2l89</guid>
      <description>&lt;p&gt;As developers, we often juggle various tasks—some demanding our expertise and others being repetitive and time-consuming. This can easily lead to getting bogged down in routine work, encountering errors, and spending excessive time on basic tasks.&lt;/p&gt;

&lt;p&gt;The solution? ChatGPT. This post explores how ChatGPT can be used effectively to streamline your workflow.&lt;/p&gt;

&lt;p&gt;By letting ChatGPT handle common tasks, you can focus on more critical aspects of your projects. For instance, if you need to write automation code for reading files from your local system, you can ask ChatGPT to generate the code, optimize it, or make specific adjustments.&lt;/p&gt;

&lt;p&gt;If you're working with multiple versions of your code in VS Code and experimenting with different approaches, ChatGPT can help you evaluate which approach is more effective. Think of ChatGPT as your 24/7 senior developer, ready to guide you through any problem and provide error-free solutions without hesitation.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Helps While Learning New Tech&lt;/strong&gt;
&lt;/h2&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%2Ftfsn7k7pt984axd87pio.jpg" 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%2Ftfsn7k7pt984axd87pio.jpg" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine you’re learning Ruby on Rails (RoR) and following a tutorial to build a small website. After getting stuck midway, you can use ChatGPT to assist you step-by-step. For example, if you’re developing an e-commerce site and have successfully created CRUD (Create, Read, Update, Delete) operations for categories and products, but struggle with linking products to categories, ChatGPT can help:&lt;/p&gt;

&lt;p&gt;Generate the CRUD operations for categories to ensure your setup is correct.&lt;br&gt;
Provide CRUD code for products to verify best practices.&lt;br&gt;
Show you how to create foreign key relationships between products and categories.&lt;br&gt;
By comparing your code with ChatGPT’s suggestions, you can identify and fix mistakes, progressing on your RoR project even without additional external help.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Help to Showcase Your Hard Work&lt;/strong&gt;
&lt;/h2&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%2Fpyi6gn6tf7ywrcbhgw14.jpg" 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%2Fpyi6gn6tf7ywrcbhgw14.jpg" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes, despite your hard work, it may not be evident to your manager. ChatGPT can help you present your efforts more effectively. Before posting updates on Jira, ask ChatGPT to refine and enhance your comments, making them more descriptive and comprehensive. If needed, you can request adjustments until the format meets your needs.&lt;/p&gt;

&lt;p&gt;Using ChatGPT ensures your hard work is well-documented and visible, showcasing the full extent of your contributions effectively.&lt;/p&gt;

&lt;p&gt;Attribute :&lt;br&gt;
Cover image by upklyak on Freepik&lt;br&gt;
learn Image by pikisuperstar on Freepik&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Best coding practices in React.js</title>
      <dc:creator>HitanshiMehta</dc:creator>
      <pubDate>Thu, 13 Jan 2022 20:34:31 +0000</pubDate>
      <link>https://dev.to/hitanshimehta/best-coding-practices-in-reactjs-4gmp</link>
      <guid>https://dev.to/hitanshimehta/best-coding-practices-in-reactjs-4gmp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we will discuss the best coding practices that you can follow in your next project.&lt;/p&gt;

&lt;p&gt;These practices will make your code, &lt;/p&gt;

&lt;p&gt;reusable&lt;br&gt;
cleaner&lt;br&gt;
efficient and&lt;br&gt;
easily adaptable by another developer.&lt;br&gt;
List of coding practices that you should follow.&lt;/p&gt;

&lt;p&gt;Combine state&lt;br&gt;
In long component hierarchy use useContext&lt;br&gt;
Separate UI and logic&lt;br&gt;
Remove unnecessary props&lt;br&gt;
Write a function for a repetitive task&lt;br&gt;
Avoid named import/member import&lt;br&gt;
Use forEach instead of map&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Combine state&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In most of the components, you'll have a state. While defining a new state, take time and think if you can combine multiple states into a single state. Let's understand this with the help of an example.&lt;/p&gt;

&lt;p&gt;Let's say you are working on a chocolate website.&lt;/p&gt;

&lt;p&gt;You have two types of sizes.&lt;/p&gt;

&lt;p&gt;Default size ---&amp;gt; You will receive sizes from API&lt;br&gt;
Custom size ---&amp;gt; User can add custom sizes.&lt;br&gt;
Once user has added custom sizes, user can proceed for checkout by selecting desired sizes. &lt;/p&gt;

&lt;p&gt;In the wrong coding practice, you can have three different states.&lt;/p&gt;

&lt;p&gt;state for default sizes (received from backend)&lt;br&gt;
state for custom sizes&lt;br&gt;
state for selected sizes&lt;br&gt;
So you will define three different states.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [APISizes, setAPISizes] = useState([{
    id: ''
    height: '',
    width: ''
}]);
const [customSizes, setCustomSizes] = useState([{
    id: '',
    height: '',
    width: ''
}]);
const [selectedSizes, setSelectedSizes] = useState([1, 2]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have to keep an eye on three different states and you'll need to keep them in sync. Let's look at scenarios where it will create a problem.&lt;/p&gt;

&lt;p&gt;While displaying all sizes you have to loop through two states. (on APISizes and customSizes)&lt;br&gt;
In selectedSizes we are storing only ids. For size information we have to iterate over APISize and CustomSize.&lt;br&gt;
In good coding practice, you can define a single state as follow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [userSuggestions, setUserSuggestion] = useState([{
    id: 1,
    height: 100,
    width: 100,
    isCustom: false,
    isSelected: false
}]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, you have to think about only one state. If another developer works on your code it's easy for her/him too.&lt;/p&gt;

&lt;p&gt;In this coding practice, If you want to introduce a new key, you have to update only one state instead of 2-3 states.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [userSuggestions, setUserSuggestion] = useState([{
    id: 1,
    height: 100,
    width: 100,
    isCustom: false,
    isSelected: false,
    isByDefaultSelected: true,
}]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. In long component hierarchy use useContext&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a long component hierarchy, useContext will provide cleaner and reusable code. Look at the following example.&lt;/p&gt;

&lt;p&gt;In the application, you can select a project and folder. In the dashboard component, we want to show the total selected projects and folders. We need to define two states in the dashboard component&lt;/p&gt;

&lt;p&gt;Selected project&lt;br&gt;
Selected folder&lt;br&gt;
We will pass these states from&lt;/p&gt;

&lt;p&gt;Selected project: Dashboard -&amp;gt; Projects -&amp;gt; Project Display -&amp;gt; Project Option&lt;/p&gt;

&lt;p&gt;Selected folder: Dashboard -&amp;gt; Folders -&amp;gt; Folder display -&amp;gt; Folder Option&lt;/p&gt;

&lt;p&gt;Best coding practices in react&lt;/p&gt;

&lt;p&gt;This becomes nastier as the number of state and the number of components grows.&lt;/p&gt;

&lt;p&gt;The solution to this problem is creating context. It will allow you to use state in any component. You will call context from the topmost component and all children will be able to use state.&lt;/p&gt;

&lt;p&gt;If you don't know how context works, you can go through this article: &lt;a href="https://reactjs.org/docs/context.html"&gt;https://reactjs.org/docs/context.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Separate logic and UI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prefer to separate logic and UI. For example, you can write onClick function as an inline function or you can call a separate function. Create a separate function rather than writing an inline function. This will give a clear separation between UI and logic. It will make the code more understandable, clear, and reusable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Remove unnecessary props&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep a good eye on whether your props are dynamic or static. Other than static props, sometimes we pass redux state as props which don't help in reducing number of render at all. Passing redux state as props makes component difficult to reuse. We will understand this with help of an example.&lt;/p&gt;

&lt;p&gt;In our project we have profile component. Profile component is calling the Image component. Image component requires login user information and login user information is stored in redux state.&lt;/p&gt;

&lt;p&gt;Profile component is already calling a redux state of login information. In this situation, you can choose from two options.&lt;/p&gt;

&lt;p&gt;Pass redux state as props from parent component(Profile) to child component(Image)&lt;br&gt;
Call redux state in child component(Image) using useSelector&lt;br&gt;
You should always go for the second option as in the future Image component will be used from multiple components. All the parent component of Image component have to call login user information. (As login user information is compulsory props of Image component). This will lead to an unnecessary call of redux state every time the component is reused.&lt;/p&gt;

&lt;p&gt;In both cases,&lt;/p&gt;

&lt;p&gt;If you pass state as props from parent component (From profile to Image)&lt;br&gt;
use useSelector inside child component (Image component)&lt;br&gt;
react will re-render. A change in props causes re-render and change in the redux state also cause re-render.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Write a function for a repetitive task&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This seems to be a normal thing but keep a good eye on repetitive code. For example, you might be updating the same state in the same manner from 5 different components. In this case, create one function to update state and use that function in all the components. Slow down while you write code and if you encounter yourself writing the same code, again and again, write a common function instead. I'll highly recommend creating a common function for the repetitive task. As more code you'll write, you will appreciate the time you have spent writing common functions. In the future, if you have any code change, there will be only one place to change code rather than going through all the components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Avoid named import/member import if possible&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, let's understand how we can import the module. Let's say you are using material UI. In your component, you need Button and TextField from material UI. You can import them in two ways.&lt;/p&gt;

&lt;p&gt;a. Named import / Member import&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {TextField, Button} from "@mui/material";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b. Default import&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always prefer default import as in the default import only code of button and textfield is imported. In the named import/member import, all the modules of material UI are loaded. From all the material UI code you are using button and textfield in your component. Over time this will increase bundle size. Default import have one disadvantage. If you are using 10 different components of material UI, you'll have 10 different imports in a file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import CloseIcon from "@mui/icons-material/Close";
import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Container from "@mui/material/Container";
import IconButton from "@mui/material/IconButton";
import Paper from "@mui/material/Paper";
import Popover from "@mui/material/Popover";
import TextField from "@mui/material/TextField";
import Toolbar from "@mui/material/Toolbar";
import Typography from "@mui/material/Typography";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will increase the line of code but it will reduce bundle size&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Use forEach instead of map&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In code, we normally use a map to iterate over an object. Many developers follow this wrong practice. If you only want to iterate over an object you must use forEach. If you want to modify the current object then use a map.&lt;/p&gt;

&lt;p&gt;Let's understand this with an example. Let's say we have sizes object as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sizes = {
    category: '',
    height: '',
    width: '',
    isSelected: false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a. forEach&lt;/p&gt;

&lt;p&gt;We want to iterate over sizes object to get all type of size categories into one array.&lt;/p&gt;

&lt;p&gt;We are not modifying sizes object but we are iterating over sizes object to get new information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const allCategory = [];
sizes.forEach((sizeObj) =&amp;gt; {
    const {
        category
    } = sizeObj;
    if (!allCategory.includes(category)) allCategory.push(category);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b. map&lt;/p&gt;

&lt;p&gt;On button click, we want to select all 'custom' category sizes. In this case, we want to modify the sizes object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const updatedSizes = sizes.map((sizeObj) =&amp;gt; {
    const {
        category
    } = sizeObj;
    if (category === 'custom') {
        const newSizeObj = {
            ...sizeObj,
            isSelected: true,
        };
        return newSizeObj;
    }
    return sizeObj;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;map returns a new object so in updatedSizes, all 'personal' category sizes will be selected.&lt;/p&gt;

&lt;p&gt;Following are some of the variable-related practices that you can follow while coding.&lt;/p&gt;

&lt;p&gt;Use const instead of let (if possible)&lt;br&gt;
Write the meaningful and more understandable variable name&lt;/p&gt;

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