<?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: TOOSRIET</title>
    <description>The latest articles on DEV Community by TOOSRIET (@toosriet).</description>
    <link>https://dev.to/toosriet</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%2F2803282%2Fc770eba6-1af8-4136-9a51-4626945996fc.png</url>
      <title>DEV Community: TOOSRIET</title>
      <link>https://dev.to/toosriet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toosriet"/>
    <language>en</language>
    <item>
      <title>The React Folder Structure Exposé: From Newbie to Code Mastery Style</title>
      <dc:creator>TOOSRIET</dc:creator>
      <pubDate>Mon, 17 Feb 2025 13:05:05 +0000</pubDate>
      <link>https://dev.to/toosriet/the-react-folder-structure-expose-from-newbie-to-code-mastery-style-3mlb</link>
      <guid>https://dev.to/toosriet/the-react-folder-structure-expose-from-newbie-to-code-mastery-style-3mlb</guid>
      <description>&lt;p&gt;When building React applications, the way you organize your files can impact both development speed and long-term maintainability. In this article, we will start with a basic structure suitable for beginners, move to an intermediate approach, and finally detail a complex, feature-based organization. Each level comes with its benefits and potential challenges.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Beginner Folder Structure
&lt;/h2&gt;

&lt;p&gt;For newcomers or simple projects, a flat folder structure works well. Most tools (like Create React App) generate a basic project without a lot of sub-folders.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example Structure&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my-react-app/
├── public/
│   ├── index.html
│   └── favicon.ico
└── src/
    ├── index.js
    ├── App.js
    ├── App.css
    └── index.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Pros&lt;/strong&gt; ✅
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity:&lt;/strong&gt;
New developers can quickly see where everything is. There isn’t much boilerplate to understand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quick Setup:&lt;/strong&gt;
With a few files to manage, you can rapidly start building your UI and logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ideal for Small Projects:&lt;/strong&gt;
If you are building a prototype or a tiny application, this structure minimizes overhead.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Cons&lt;/strong&gt; ❌
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Limited Scalability:&lt;/strong&gt;
As you add more components, pages, and logic, the file structure can become cluttered and challenging to maintain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poor Separation of Concerns:&lt;/strong&gt;
UI, business logic, and styling are all mixed together. It may become unclear where to locate or update specific functionality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Difficult Maintenance:&lt;/strong&gt;
In a growing codebase or when working within a team, quickly finding the right file for a specific feature becomes a challenge.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Intermediate Folder Structure
&lt;/h2&gt;

&lt;p&gt;As your project grows, you might want to separate files by their roles or responsibilities. An intermediate structure achieves this by creating folders like &lt;code&gt;components&lt;/code&gt;, &lt;code&gt;pages&lt;/code&gt;, and &lt;code&gt;hooks&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example Structure&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my-react-app/
├── public/
│   ├── index.html
│   └── favicon.ico
└── src/
    ├── index.js
    ├── App.js
    ├── assets/
    │   ├── images/
    │   └── styles/     &lt;span class="c"&gt;# Global styles and variables&lt;/span&gt;
    ├── components/
    │   ├── common/     &lt;span class="c"&gt;# Reusable UI components (buttons, inputs, etc.)&lt;/span&gt;
    │   └── specific/   &lt;span class="c"&gt;# Components dedicated to a specific part of the app&lt;/span&gt;
    ├── pages/          &lt;span class="c"&gt;# Container components for routes (e.g., Home, About)&lt;/span&gt;
    ├── hooks/          &lt;span class="c"&gt;# Custom hooks&lt;/span&gt;
    ├── context/        &lt;span class="c"&gt;# React Context providers if needed&lt;/span&gt;
    ├── services/       &lt;span class="c"&gt;# API calls and external service integrations&lt;/span&gt;
    └── utils/          &lt;span class="c"&gt;# Helper functions and utilities&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Pros&lt;/strong&gt; ✅
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clearer Organization:&lt;/strong&gt;
Grouping files by type makes it easier to locate assets, components, pages, and shared code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enhanced Maintainability:&lt;/strong&gt;
Reusable logic (like custom hooks or utility functions) is centralized, making changes easier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better Collaboration:&lt;/strong&gt;
With clearly defined responsibilities, team members can work on different layers (UI, API, state management) independently.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Cons&lt;/strong&gt; ❌
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scattering of Feature-Specific Logic:&lt;/strong&gt;
Consider a feature like a blog post. The UI may reside in &lt;code&gt;components/&lt;/code&gt;, the API calls in &lt;code&gt;services/&lt;/code&gt;, and custom logic in &lt;code&gt;hooks/&lt;/code&gt;. This separation can make it harder to see how pieces interrelate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Potential Overkill for Small Projects:&lt;/strong&gt;
For a simple or starting application, creating many folders might seem like over-engineering.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple Navigation Points:&lt;/strong&gt;
Developers might have to jump between several directories to amend a single feature, potentially slowing down development if not carefully documented.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Advanced / Complex (Feature-Based) Folder Structure
&lt;/h2&gt;

&lt;p&gt;Advanced projects may benefit from a feature-based or domain-driven structure. This strategy encapsulates all related files—UI components, state management, hooks, API calls—for a given feature in one place.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example Structure&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my-react-app/
├── public/
│   ├── index.html
│   └── favicon.ico
└── src/
    ├── index.js
    ├── App.js
    ├── assets/
    │   ├── images/
    │   └── styles/       &lt;span class="c"&gt;# Global styles and themes&lt;/span&gt;
    ├── components/       &lt;span class="c"&gt;# Generic, reusable components across features&lt;/span&gt;
    ├── features/
    │   ├── blog/
    │   │   ├── components/  &lt;span class="c"&gt;# Blog-specific UI components&lt;/span&gt;
    │   │   ├── hooks/       &lt;span class="c"&gt;# Custom hooks for blog functionality&lt;/span&gt;
    │   │   ├── services/    &lt;span class="c"&gt;# API calls and business logic for blog&lt;/span&gt;
    │   │   └── blogSlice.js &lt;span class="c"&gt;# Redux slice or local state management for blog&lt;/span&gt;
    │   └── user/
    │       ├── components/
    │       ├── hooks/
    │       ├── services/
    │       └── userSlice.js
    ├── store/            &lt;span class="c"&gt;# Global state management (e.g., Redux store)&lt;/span&gt;
    │   ├── index.js
    │   └── configuration.js
    ├── hooks/            &lt;span class="c"&gt;# Global hooks (e.g., useAuth, useMediaQuery)&lt;/span&gt;
    ├── context/          &lt;span class="c"&gt;# Global context providers, if using Context API&lt;/span&gt;
    ├── utils/            &lt;span class="c"&gt;# Shared helper functions&lt;/span&gt;
    └── tests/            &lt;span class="c"&gt;# Unit and integration tests&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Pros&lt;/strong&gt; ✅
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Feature Encapsulation:&lt;/strong&gt;
All files related to a specific feature (UI, state, API, etc.) are grouped together. This makes the codebase more maintainable and easier to understand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Scalability:&lt;/strong&gt;
New features can be added as separate modules under the &lt;code&gt;features/&lt;/code&gt; folder without disrupting the overall structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduced Merge Conflicts:&lt;/strong&gt;
Different teams can work on separate features independently since each feature is self-contained.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier Refactor &amp;amp; Reuse:&lt;/strong&gt;
If a feature needs to be reused in another project or a significant refactor is necessary, the self-contained nature of the feature makes the process simpler.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Cons&lt;/strong&gt; ❌
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initial Overhead for Small Projects:&lt;/strong&gt;
This structure might be too robust and complicated for small-scale applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Risk of Duplication:&lt;/strong&gt;
Without proper management, utility functions or shared components may get duplicated across feature folders. It is important to extract truly shared logic into a common directory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learning Curve:&lt;/strong&gt;
Team members new to the project might need some time to understand the feature-based layout before they can navigate effectively.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How the Advanced Structure Solves Issues from Lower Levels
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Improving Scalability:&lt;/strong&gt;
The beginner folder structure quickly becomes cluttered as the project expands. Moving to an intermediate setup offers file type separation, but it may still scatter feature-specific logic across multiple directories. The advanced structure consolidates everything related to one feature into one area, making it easier to manage a growing codebase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enhancing Maintainability:&lt;/strong&gt;
In the intermediate model, developers might struggle to locate and update all parts of a single feature. The feature-based approach encapsulates all the necessary components, state management, and service calls, simplifying maintenance and debugging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Facilitating Team Collaboration:&lt;/strong&gt;
With the advanced structure, teams can work on separate features autonomously. This minimizes conflicts and streamlines code reviews since resources and logic for each feature are isolated from one another.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Choosing the right folder structure is essential for maintaining code quality and team productivity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;Beginner Structure&lt;/strong&gt; keeps things simple and is perfect for small apps or prototypes but quickly becomes unmanageable with growth.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Intermediate Structure&lt;/strong&gt; introduces clear separations by file type, making things easier to navigate but often scatters feature logic.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Advanced (Feature-Based) Structure&lt;/strong&gt; provides a scalable, maintainable, and team-friendly architecture by grouping all parts of a feature together—even though it may add some initial complexity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By starting with a simpler structure and evolving to a feature-based organization as your project grows, you can balance simplicity with maintainability and scalability, ensuring that your React application remains robust as its complexity increases.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>18+ Questions to Challenge and Expand Your Babel Knowledge</title>
      <dc:creator>TOOSRIET</dc:creator>
      <pubDate>Sat, 15 Feb 2025 08:56:12 +0000</pubDate>
      <link>https://dev.to/toosriet/18-questions-to-challenge-and-expand-your-babel-knowledge-4bj8</link>
      <guid>https://dev.to/toosriet/18-questions-to-challenge-and-expand-your-babel-knowledge-4bj8</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Table of Contents&lt;/li&gt;
&lt;li&gt;0. Intro&lt;/li&gt;
&lt;li&gt;
1. Babel Fundamentals and Syntax Transformations

&lt;ul&gt;
&lt;li&gt;What is Babel and why we need it?&lt;/li&gt;
&lt;li&gt;How does Babel work?&lt;/li&gt;
&lt;li&gt;Describe how Babel transforms modern JavaScript syntax such as arrow functions, async/await, and generators under the hood.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

2. Babel Configuration and Setup

&lt;ul&gt;
&lt;li&gt;How can Babel be configured in a project?&lt;/li&gt;
&lt;li&gt;What is the difference between Babel “presets” and “plugins”?&lt;/li&gt;
&lt;li&gt;How would you set up Babel for a React project and what additional presets or plugins might you use?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

3. Compatibility and Debugging Tools

&lt;ul&gt;
&lt;li&gt;What is @babel/polyfill and why we need it?&lt;/li&gt;
&lt;li&gt;What are source maps and why would you configure Babel to generate them?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

4. Performance Optimizations and Plugin Management

&lt;ul&gt;
&lt;li&gt;How does Babel utilize caching to improve performance, especially during development?&lt;/li&gt;
&lt;li&gt;How do you implement custom Babel plugins? Provide an outline of creating a plugin that logs every function entry.&lt;/li&gt;
&lt;li&gt;Is Babel plugin order important?&lt;/li&gt;
&lt;li&gt;Example Scenario&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  0. Intro
&lt;/h2&gt;

&lt;p&gt;In today's fast-paced world, where advanced AI technologies are emerging rapidly, staying competitive by quickly acquiring cutting-edge knowledge is essential. This motivation inspired me to create this concise document.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For beginners, I hope it provides a clear introduction to the key concepts of Babel, allowing you to dive right in.&lt;/li&gt;
&lt;li&gt;For experienced users, I hope you'll find it a valuable resource to further challenge and expand your Babel expertise.&lt;/li&gt;
&lt;li&gt;Please feel free to share any feedback or suggestions for improvement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Babel Fundamentals and Syntax Transformations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Babel and why we need it?
&lt;/h3&gt;

&lt;p&gt;Babel is a JavaScript compiler. It is used to convert modern JavaScript code into older versions (or "transpile") that older browsers can understand.&lt;/p&gt;

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

&lt;p&gt;Modern code:&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Transpiled code:&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;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;Browser Compatibility:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Browser&lt;/th&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Cannot Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Internet Explorer&lt;/td&gt;
&lt;td&gt;IE 11&lt;/td&gt;
&lt;td&gt;- Arrow functions (=&amp;gt;) &lt;br&gt; - let/const declarations &lt;br&gt; - Classes &lt;br&gt; - Template literals (``)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Safari&lt;/td&gt;
&lt;td&gt;&amp;lt; 10&lt;/td&gt;
&lt;td&gt;- async/await &lt;br&gt; - Array.includes() &lt;br&gt; - Object spread operators {...obj}&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chrome&lt;/td&gt;
&lt;td&gt;&amp;lt; 49&lt;/td&gt;
&lt;td&gt;- Default parameters &lt;br&gt; - Destructuring &lt;br&gt; - Generator functions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Firefox&lt;/td&gt;
&lt;td&gt;&amp;lt; 52&lt;/td&gt;
&lt;td&gt;- async/await &lt;br&gt; - Rest parameters &lt;br&gt; - ES modules (import/export)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is why we need Babel—it allows us to write modern JavaScript while ensuring our code works in older browsers through transpilation.&lt;/p&gt;




&lt;h3&gt;
  
  
  How does Babel work?
&lt;/h3&gt;

&lt;p&gt;Babel transforms code through a three-step process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Parsing:&lt;/strong&gt;
The source code is read and converted into an Abstract Syntax Tree (AST), which represents the code structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transformation:&lt;/strong&gt;
Babel traverses the AST and applies plugins that modify it—replacing modern syntax with equivalent, older JavaScript constructs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Generation:&lt;/strong&gt;
Finally, Babel generates new JavaScript code from the transformed AST.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For more information on ASTs, you can read this article on the &lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noopener noreferrer"&gt;Abstract Syntax Tree&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Describe how Babel transforms modern JavaScript syntax such as arrow functions, async/await, and generators under the hood.
&lt;/h3&gt;

&lt;p&gt;Babel first parses your code to create an Abstract Syntax Tree (AST). It then traverses the AST using a plugin system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Arrow Functions:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Babel converts them into traditional function expressions, while ensuring the correct binding of the lexical &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Async/Await:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Babel rewrites them into a generator-based workflow (often using a helper like &lt;code&gt;regeneratorRuntime&lt;/code&gt;) or into promise-based chains, simulating asynchronous behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Generators:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Babel transforms generators into state machine-like constructs that mimic the iterator behavior in environments without native support.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This systematic transformation driven by AST manipulation enables Babel to support many modern language features.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Babel Configuration and Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How can Babel be configured in a project?
&lt;/h3&gt;

&lt;p&gt;Babel can be configured in several ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;.babelrc File:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A JSON file at the project root that specifies presets and plugins for Babel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;babel.config.js:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A JavaScript configuration file that offers dynamic configuration options and is especially useful in monorepos or projects with multiple packages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;package.json:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Babel settings may also be included in the &lt;code&gt;"babel"&lt;/code&gt; field within your package.json.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The choice depends on your project’s complexity. Simple projects often use a &lt;code&gt;.babelrc&lt;/code&gt;, while more complex or multi-package projects benefit from the flexibility of &lt;code&gt;babel.config.js&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  What is the difference between Babel “presets” and “plugins”?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Plugins:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
These are individual modules that handle specific code transformations. For example, a plugin might transform arrow functions, object spread, or other particular syntax.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Presets:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
These are collections of plugins bundled together to provide a broader transformation capability. For example, &lt;code&gt;@babel/preset-env&lt;/code&gt; includes many plugins that together enable support for modern JavaScript based on your target environments.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using presets simplifies configuration because you can include a full set of related transformations with a single entry.&lt;/p&gt;




&lt;h3&gt;
  
  
  How would you set up Babel for a React project and what additional presets or plugins might you use?
&lt;/h3&gt;

&lt;p&gt;To set up Babel for a React project, you would typically use the following presets and plugins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;@babel/preset-react&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Designed for React code, it includes plugins for features such as JSX.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;@babel/preset-env&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Used to transform modern JavaScript into backward-compatible versions for your target environments.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A typical &lt;code&gt;.babelrc&lt;/code&gt; might look like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`json&lt;br&gt;
{&lt;br&gt;
  "presets": ["@babel/preset-env", "@babel/preset-react"]&lt;br&gt;
}&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Compatibility and Debugging Tools
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/polyfill and why we need it?
&lt;/h3&gt;

&lt;p&gt;Babel’s transformation works in two phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Syntax Transformation:&lt;/strong&gt;
Converts modern JavaScript syntax into older JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Polyfilling:&lt;/strong&gt;
Backfills new implementations introduced in modern JavaScript for older environments.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Historically, &lt;code&gt;@babel/polyfill&lt;/code&gt; was used to handle necessary polyfills. However, since Babel 7.4.0, &lt;code&gt;@babel/polyfill&lt;/code&gt; has been deprecated. The recommended approach now is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Install &lt;code&gt;core-js&lt;/code&gt; and &lt;code&gt;regenerator-runtime&lt;/code&gt; separately.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use the &lt;code&gt;useBuiltIns&lt;/code&gt; option in &lt;code&gt;@babel/preset-env&lt;/code&gt;:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"entry":&lt;/strong&gt; Requires an explicit import of &lt;code&gt;core-js/stable&lt;/code&gt; and &lt;code&gt;regenerator-runtime/runtime&lt;/code&gt; at the top of your entry file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"usage":&lt;/strong&gt; Instructs Babel to include polyfills automatically wherever they're needed in your code.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This approach provides better control and potentially smaller bundle sizes, as you only include what is needed.&lt;/p&gt;




&lt;h3&gt;
  
  
  What are source maps and why would you configure Babel to generate them?
&lt;/h3&gt;

&lt;p&gt;Source maps help map the transformed code back to the original source code. This is especially useful for debugging; if an error occurs in the transpiled code, developers can refer to the source map to see the corresponding location in the original code.&lt;/p&gt;

&lt;p&gt;To configure Babel to generate source maps, you can add the following configuration:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`json&lt;br&gt;
{&lt;br&gt;
  "sourceMaps": true&lt;br&gt;
}&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Performance Optimizations and Plugin Management
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How does Babel utilize caching to improve performance, especially during development?
&lt;/h3&gt;

&lt;p&gt;When used with tools like &lt;code&gt;babel-loader&lt;/code&gt;, Babel caches the output of file transformations based on a file hash. On rebuilds, unchanged files are retrieved from this cache (commonly stored in a directory like &lt;code&gt;node_modules/.cache&lt;/code&gt;), significantly accelerating the build process during development.&lt;/p&gt;




&lt;h3&gt;
  
  
  How do you implement custom Babel plugins? Provide an outline of creating a plugin that logs every function entry.
&lt;/h3&gt;

&lt;p&gt;To create a custom Babel plugin, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a Plugin File:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
For instance, create a new JavaScript file (e.g., &lt;code&gt;my-plugin.js&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Export a Function:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The function should adhere to the Babel plugin API, receiving an argument (commonly named &lt;code&gt;babel&lt;/code&gt; which includes type helpers).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define Visitor Methods:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Implement visitors for function nodes (both function declarations and expressions).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insert Logging Code:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Use Babel's type helpers (&lt;code&gt;t&lt;/code&gt;) to construct a &lt;code&gt;console.log&lt;/code&gt; node and inject it at the beginning of each function.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;javascript&lt;br&gt;
// my-plugin.js&lt;br&gt;
module.exports = function ({ types: t }) {&lt;br&gt;
  return {&lt;br&gt;
    visitor: {&lt;br&gt;
      Function(path) {&lt;br&gt;
        // Determine function name, falling back to 'anonymous'&lt;br&gt;
        const functionName =&lt;br&gt;
          path.node.id &amp;amp;&amp;amp; path.node.id.name ? path.node.id.name : "anonymous";&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Create a console.log statement
    const logStatement = t.expressionStatement(
      t.callExpression(
        t.memberExpression(t.identifier("console"), t.identifier("log")),
        [t.stringLiteral(`Entering function ${functionName}`)]
      )
    );

    // If the function body is a block statement, insert the log statement at the start
    if (t.isBlockStatement(path.node.body)) {
      path.node.body.body.unshift(logStatement);
    }
  },
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;p&gt;This plugin will traverse every function node and prepend a logging statement at the start of its body.&lt;/p&gt;




&lt;h3&gt;
  
  
  Is Babel plugin order important?
&lt;/h3&gt;

&lt;p&gt;Yes, the order of Babel plugins is important. Babel processes plugins in the sequence they appear in your configuration, which means that when multiple plugins target the same node types, the order can affect the final transformation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Scenario
&lt;/h4&gt;

&lt;p&gt;Consider two plugins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Plugin A:&lt;/strong&gt; Transforms every identifier named &lt;code&gt;"foo"&lt;/code&gt; into &lt;code&gt;"bar"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plugin B:&lt;/strong&gt; Transforms every identifier named &lt;code&gt;"bar"&lt;/code&gt; into &lt;code&gt;"baz"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Scenario 1: Plugin A first, then Plugin B&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`javascript&lt;br&gt;
// babel.config.js&lt;br&gt;
module.exports = {&lt;br&gt;
  plugins: [&lt;br&gt;
    "./plugin-foo-to-bar.js", // Plugin A&lt;br&gt;
    "./plugin-bar-to-baz.js", // Plugin B&lt;br&gt;
  ],&lt;br&gt;
};&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source Code:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`javascript&lt;br&gt;
const foo = 42;&lt;br&gt;
console.log(foo);&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transformation Process:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Plugin A:&lt;/strong&gt;
&lt;code&gt;"foo"&lt;/code&gt; becomes &lt;code&gt;"bar"&lt;/code&gt;, resulting in:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;`javascript&lt;br&gt;
   const bar = 42;&lt;br&gt;
   console.log(bar);&lt;br&gt;
   `&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Plugin B:&lt;/strong&gt;
&lt;code&gt;"bar"&lt;/code&gt; becomes &lt;code&gt;"baz"&lt;/code&gt;, resulting in:
&lt;code&gt;`javascript
const baz = 42;
console.log(baz);
`&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2: Plugin B first, then Plugin A&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`javascript&lt;br&gt;
// babel.config.js&lt;br&gt;
module.exports = {&lt;br&gt;
  plugins: [&lt;br&gt;
    "./plugin-bar-to-baz.js", // Plugin B&lt;br&gt;
    "./plugin-foo-to-bar.js", // Plugin A&lt;br&gt;
  ],&lt;br&gt;
};&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transformation Process:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Plugin B:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Checks for &lt;code&gt;"bar"&lt;/code&gt; but finds &lt;code&gt;"foo"&lt;/code&gt; so nothing changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Plugin A:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Converts &lt;code&gt;"foo"&lt;/code&gt; to &lt;code&gt;"bar"&lt;/code&gt;, resulting in:&lt;br&gt;
&lt;code&gt;`javascript&lt;br&gt;
const bar = 42;&lt;br&gt;
console.log(bar);&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This example demonstrates that plugin order can directly impact the output of your code transformations.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>18+ Questions to Challenge and Expand Your Webpack Knowledge</title>
      <dc:creator>TOOSRIET</dc:creator>
      <pubDate>Sat, 08 Feb 2025 16:39:17 +0000</pubDate>
      <link>https://dev.to/toosriet/18-questions-to-challenge-and-expand-your-webpack-knowledge-20lf</link>
      <guid>https://dev.to/toosriet/18-questions-to-challenge-and-expand-your-webpack-knowledge-20lf</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Table of Contents&lt;/li&gt;
&lt;li&gt;0. Intro&lt;/li&gt;
&lt;li&gt;
1. Introduction &amp;amp; Basic Concepts

&lt;ul&gt;
&lt;li&gt;What is Webpack?&lt;/li&gt;
&lt;li&gt;What is the most common webpack version?&lt;/li&gt;
&lt;li&gt;What is a bundle?&lt;/li&gt;
&lt;li&gt;Why do we need bundles?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

2. How Webpack Works &amp;amp; How to Configure It

&lt;ul&gt;
&lt;li&gt;How does Webpack work?&lt;/li&gt;
&lt;li&gt;How do we configure Webpack?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

3. Loaders &amp;amp; Plugins

&lt;ul&gt;
&lt;li&gt;What is the role of Loaders in Webpack?&lt;/li&gt;
&lt;li&gt;What are Plugins in Webpack and how do they extend its functionality?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

4. Code Optimization Techniques

&lt;ul&gt;
&lt;li&gt;What is Tree Shaking in Webpack?&lt;/li&gt;
&lt;li&gt;What is Code Splitting and why is it beneficial?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

5. Development Tools &amp;amp; Modes

&lt;ul&gt;
&lt;li&gt;What is the role of the Webpack Dev Server?&lt;/li&gt;
&lt;li&gt;What are Source Maps and how are they used in Webpack?&lt;/li&gt;
&lt;li&gt;What is the difference between development and production mode in Webpack?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

6. Bundle Analysis &amp;amp; Caching Strategies

&lt;ul&gt;
&lt;li&gt;How can you analyze the bundle size?&lt;/li&gt;
&lt;li&gt;How does Webpack make sure the client gets the latest version of the bundle?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

7. Build Performance Optimization Strategies

&lt;ul&gt;
&lt;li&gt;What are some strategies to optimize Webpack build performance?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

8. Environment Variables &amp;amp; TypeScript Integration

&lt;ul&gt;
&lt;li&gt;What are Environment Variables and how can they be integrated in Webpack?&lt;/li&gt;
&lt;li&gt;How do you use Webpack with TypeScript?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

9. Troubleshooting &amp;amp; Common Pitfalls

&lt;ul&gt;
&lt;li&gt;What are some common pitfalls when configuring Webpack?&lt;/li&gt;
&lt;li&gt;How do you debug problems in the Webpack build process?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  0. Intro
&lt;/h2&gt;

&lt;p&gt;In today's fast-paced world, where advanced AI technologies are emerging rapidly, staying competitive by quickly acquiring cutting-edge knowledge is essential. This motivation inspired me to create this concise document. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For beginners, I hope it provides a clear introduction to the key concepts of Webpack, allowing you to dive right in. &lt;/li&gt;
&lt;li&gt;For experienced users, I hope you'll find it a valuable resource to further challenge and expand your Webpack expertise. Please feel free to share any feedback or suggestions for improvement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Introduction &amp;amp; Basic Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Webpack?
&lt;/h3&gt;

&lt;p&gt;Webpack is a tool that bundles your code into a single file. It is a popular tool for building modern JavaScript applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the most common webpack version?
&lt;/h3&gt;

&lt;p&gt;The most common version of Webpack is 5.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a bundle?
&lt;/h3&gt;

&lt;p&gt;A bundle is a single file that contains all of the code for your application. It is the output of the Webpack build process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do we need bundles?
&lt;/h3&gt;

&lt;p&gt;Bundles are necessary because modern JavaScript applications are made up of many small files for better organization and performance.&lt;br&gt;&lt;br&gt;
These files need to be bundled together so that they can be served to the browser.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. How Webpack Works &amp;amp; How to Configure It
&lt;/h2&gt;
&lt;h3&gt;
  
  
  How does Webpack work?
&lt;/h3&gt;

&lt;p&gt;Webpack works by analyzing your code and creating a dependency graph. This graph is used to determine which files need to be bundled together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;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="c1"&gt;// index.js&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;add&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;./math.js&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&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="c1"&gt;// math.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;In this example, the &lt;code&gt;math.js&lt;/code&gt; file is a dependency of the &lt;code&gt;index.js&lt;/code&gt; file. Webpack will create a dependency graph that includes the &lt;code&gt;index.js&lt;/code&gt; file and the &lt;code&gt;math.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do we configure Webpack?
&lt;/h3&gt;

&lt;p&gt;Webpack is configured using a &lt;code&gt;webpack.config.js&lt;/code&gt; (or similar) file that typically specifies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entry:&lt;/strong&gt; The starting point(s) of your application. This is the file that will be used to create the dependency graph.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; Where and how bundles are output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loaders:&lt;/strong&gt; Modules that transform files (e.g., transpile JavaScript, compile Sass).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plugins:&lt;/strong&gt; Extensions that add functionality (e.g., optimizing bundles, injecting environment variables).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;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="c1"&gt;// webpack.config.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&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;HtmlWebpackPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bundle.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist&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="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;rules&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="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;js$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel-loader&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="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HtmlWebpackPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./index.html&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="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Loaders &amp;amp; Plugins
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the role of Loaders in Webpack?
&lt;/h3&gt;

&lt;p&gt;Loaders allow you to preprocess files as they are added to your dependency graph. For instance, you can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;babel-loader:&lt;/strong&gt; To transpile ES6+ JavaScript to older versions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;css-loader &amp;amp; style-loader:&lt;/strong&gt; To handle CSS imports.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;file-loader / url-loader:&lt;/strong&gt; To process images, fonts, and other assets.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are Plugins in Webpack and how do they extend its functionality?
&lt;/h3&gt;

&lt;p&gt;Plugins are powerful tools that tap into Webpack's build process to perform a wide range of tasks such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimizing and minifying bundles (e.g., using TerserPlugin).&lt;/li&gt;
&lt;li&gt;Generating HTML files that include your bundles (e.g., using HtmlWebpackPlugin).&lt;/li&gt;
&lt;li&gt;Extracting CSS into separate files (e.g., using MiniCssExtractPlugin).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Code Optimization Techniques
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Tree Shaking in Webpack?
&lt;/h3&gt;

&lt;p&gt;Tree shaking is a form of dead-code elimination that removes unused exports from your code. When running in production mode, Webpack analyzes module exports to exclude unnecessary code, resulting in smaller bundle sizes.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Code Splitting and why is it beneficial?
&lt;/h3&gt;

&lt;p&gt;Code splitting is a technique that breaks your bundle into smaller chunks that can be loaded on demand. This improves the initial load time of your application. It can be achieved in Webpack by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entry Points:&lt;/strong&gt; Specifying multiple configuration entries. One entry point is equivalent to one bundle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Imports:&lt;/strong&gt; Using the &lt;code&gt;import()&lt;/code&gt; syntax to load modules asynchronously.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SplitChunks Plugin:&lt;/strong&gt; Automatically splitting vendor code and common dependencies.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Development Tools &amp;amp; Modes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the role of the Webpack Dev Server?
&lt;/h3&gt;

&lt;p&gt;The Webpack Dev Server is a tool that provides a local server for your application. It is used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Serve your application to the browser.&lt;/li&gt;
&lt;li&gt;Enable hot module replacement (meaning that when you make a change to your code, the browser will automatically reload the page, similar to a live reload, but more efficiently).&lt;/li&gt;
&lt;li&gt;Handle error reporting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are Source Maps and how are they used in Webpack?
&lt;/h3&gt;

&lt;p&gt;Source maps are files that map the transformed bundled code back to the original source code. They are critical for debugging, as they let you see the original code in the browser's developer tools.&lt;/p&gt;

&lt;p&gt;To enable source maps, you need to add the &lt;code&gt;devtool&lt;/code&gt; property to your Webpack configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Configuration:&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="c1"&gt;// webpack.config.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...other configuration...&lt;/span&gt;
    &lt;span class="na"&gt;devtool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;source-map&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example Source Map File Content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"file"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"bundle.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"sources"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"webpack:///./src/index.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"sourcesContent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"function greet() {&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;  console.log('Hello, world!');&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;greet();"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"names"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"greet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"console"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"log"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mappings"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"AAAA,SAASA,KAAT,EAAgB;AACfC,IAAAA,OAAO,CAACC,GAAR,CAAY,aAAZ;AACA;;AAEDF,KAAK"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is the difference between development and production mode in Webpack?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Development Mode:&lt;/strong&gt; Offers features like detailed error messages, source maps, and improved build speed. It doesn’t perform aggressive optimizations, making debugging easier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production Mode:&lt;/strong&gt; Enables optimizations such as minification, tree shaking, and other performance improvements to create optimized bundles for deployable code.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Bundle Analysis &amp;amp; Caching Strategies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How can you analyze the bundle size?
&lt;/h3&gt;

&lt;p&gt;Tools such as &lt;code&gt;webpack-bundle-analyzer&lt;/code&gt; can visualize the size of the output files, helping you identify large dependencies or poorly optimized code. You can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; webpack-bundle-analyzer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;webpack &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nt"&gt;--json&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; stats.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use a visualizer to get insights about bundle performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does Webpack make sure the client gets the latest version of the bundle?
&lt;/h3&gt;

&lt;p&gt;Webpack uses a content hash to generate a unique identifier for each bundle. This hash is included in the filename of the bundle. When the bundle is updated, the hash changes, ensuring that the browser downloads the new version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Webpack ContentHash Example&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- The script tag is updated to include the latest content-hashed bundle --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"app.f6g7h8i9j0.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  7. Build Performance Optimization Strategies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are some strategies to optimize Webpack build performance?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;code&gt;cache&lt;/code&gt; option to cache the results of the build.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;parallel&lt;/code&gt; option to build multiple bundles in parallel.&lt;/li&gt;
&lt;li&gt;Use loaders and plugins that are optimized for performance.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;splitChunks&lt;/code&gt; plugin to split the bundle into smaller chunks.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;UglifyJsPlugin&lt;/code&gt; to minify the bundle.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;ExtractTextPlugin&lt;/code&gt; to extract CSS into a separate file — the smaller the file, the better the reduction in initial load time.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;ProvidePlugin&lt;/code&gt; to automatically load modules instead of having to import them everywhere.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  8. Environment Variables &amp;amp; TypeScript Integration
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are Environment Variables and how can they be integrated in Webpack?
&lt;/h3&gt;

&lt;p&gt;Webpack allows you to define environment variables through the &lt;code&gt;DefinePlugin&lt;/code&gt;. This lets you specify variables for different build environments (development, testing, production) during compile time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;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;webpack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webpack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DefinePlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;process.env.NODE_ENV&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;production&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;process.env.API_URL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com&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="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;
  
  
  How do you use Webpack with TypeScript?
&lt;/h3&gt;

&lt;p&gt;To use Webpack with TypeScript, you need to install the following packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TypeScript:&lt;/strong&gt; The TypeScript compiler.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ts-loader:&lt;/strong&gt; A Webpack loader to handle TypeScript files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Webpack &amp;amp; Webpack CLI:&lt;/strong&gt; The core bundler and its command line interface.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  9. Troubleshooting &amp;amp; Common Pitfalls
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are some common pitfalls when configuring Webpack?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Overcomplicating the configuration with too many plugins.&lt;/li&gt;
&lt;li&gt;Not optimizing the production build (e.g., missing tree shaking or minification).&lt;/li&gt;
&lt;li&gt;Incorrect loader configurations that lead to misprocessing files.&lt;/li&gt;
&lt;li&gt;Failing to properly handle caching, which can lead to duplicate code or outdated assets.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How do you debug problems in the Webpack build process?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;code&gt;stats&lt;/code&gt; option to show detailed information about the build.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;progress&lt;/code&gt; option to display a progress bar during the build.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;errors-only&lt;/code&gt; option to display only errors during the build.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>webpack</category>
      <category>babel</category>
    </item>
    <item>
      <title>Breakdown useEffect in React: Why do many people hate useEffect</title>
      <dc:creator>TOOSRIET</dc:creator>
      <pubDate>Sun, 02 Feb 2025 15:38:11 +0000</pubDate>
      <link>https://dev.to/toosriet/breakdown-useeffect-in-react-why-do-many-people-hate-useeffect-2la4</link>
      <guid>https://dev.to/toosriet/breakdown-useeffect-in-react-why-do-many-people-hate-useeffect-2la4</guid>
      <description>&lt;h2&gt;
  
  
  Why useEffect ?
&lt;/h2&gt;

&lt;p&gt;It's clear that useEffect is one of the most commonly used hooks in React. If you go through 10 React course, all of them will likely mention to &lt;code&gt;useEffect&lt;/code&gt;. React itself also emphasizes the important of &lt;code&gt;useEffect&lt;/code&gt; - its official documentation includes five dedicated sections just discussing about how to handle side effect in React. Despite its popularity, &lt;code&gt;useEffect&lt;/code&gt; seem like not friendly with React community. In fact, there’s been a growing discussion among experts who are trying to convince others to "Say Goodbye to &lt;code&gt;useEffect&lt;/code&gt;." So, what’s going on with &lt;code&gt;useEffect&lt;/code&gt;? Let’s break it down together.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/bGzanfKVFeU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Is useEffect unintuitive ?
&lt;/h2&gt;

&lt;p&gt;When discussing why a function might feel unintuitive, we might think that its document isn't clear enough in explaining its purpose. However, it’s not a case for &lt;code&gt;useEffect&lt;/code&gt;. Let’s have a look in React document about the definition of &lt;code&gt;useEffect&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;useEffect is a React Hook that lets you &lt;a href="https://react.dev/learn/synchronizing-with-effects" rel="noopener noreferrer"&gt;synchronize a component with an external system.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What's the heck is &lt;code&gt;external system&lt;/code&gt; ? &lt;br&gt;
If you scroll down a bit then you will find the definition for this term&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some components need to stay connected to the network, some browser API, or a third-party library, while they are displayed on the page. These systems aren’t controlled by React, so they are called _external.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So by theory, in React &lt;code&gt;useEffect&lt;/code&gt; is created when you want to synchronize internal state (state is created by our code - useState, useContext) with external state (local storage, network, etc..). &lt;/p&gt;

&lt;p&gt;However, in reality, &lt;code&gt;useEffect&lt;/code&gt; is considered as a powerful tools for hooking into the initialization of component, or watching changes &lt;strong&gt;&lt;u&gt;not only in internal data but also in external data&lt;/u&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s take a look on this example. Let's say we want to create a custom hook usePrevious to get the previous data when state change to new value.&lt;/p&gt;

&lt;p&gt;With FE mindset, it’s very intuitive to come up with a solution listening on the changes of state by using &lt;code&gt;useEffect&lt;/code&gt;, much like the &lt;a href="https://github.com/streamich/react-use/blob/master/src/usePrevious.ts" rel="noopener noreferrer"&gt;approach taken by libraries such as &lt;code&gt;react-use&lt;/code&gt;&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;usePrevious&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&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;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&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;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="nx"&gt;T&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;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="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;state&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;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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s make sense but it’s the thing React doesn’t want us to do. By definition, we should only &lt;code&gt;useEffect&lt;/code&gt; when we need to sync with &lt;strong&gt;&lt;u&gt;external state&lt;/u&gt;&lt;/strong&gt;, however, in this scenario no external involved.&lt;/p&gt;

&lt;p&gt;Instead, we need to solve the problem as same as the approach of &lt;a href="https://github.com/uidotdev/usehooks/blob/90fbbb4cc085e74e50c36a62a5759a40c62bb98e/index.js#L1017" rel="noopener noreferrer"&gt;useHook library&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;usePrevious&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;current&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCurrent&lt;/span&gt;&lt;span class="p"&gt;]&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;previous&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPrevious&lt;/span&gt;&lt;span class="p"&gt;]&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setPrevious&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;setCurrent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;previous&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;In this code, we call two &lt;code&gt;setState&lt;/code&gt; functions. However, React is very efficient at batching state updates—it will calculate the changes and renders only once.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;useEffect&lt;/code&gt; is very power, easily to overuse in wrong way and addicted that make some developer in React community don't like it. Beside, if you overuse useEffect in a wrong way you may encounters some problems&lt;/p&gt;

&lt;h2&gt;
  
  
  What if we continue useEffect in a wrong way ?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; is clearly a good tool for handling side effect, however, if we don't use it properly we may encounter some issues&lt;/p&gt;

&lt;h3&gt;
  
  
  Your app may got performance issue
&lt;/h3&gt;

&lt;p&gt;Keep using &lt;code&gt;useEffect&lt;/code&gt; as a tracking tool for all purpose may let your brain lazy in coming up better solution to solve the need.&lt;/p&gt;

&lt;p&gt;Let’s have a look on this example&lt;br&gt;
You're received a task building the search filter. If you overuse &lt;code&gt;useEffect&lt;/code&gt;, you may come up with the solution like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearchTerm&lt;/span&gt;&lt;span class="p"&gt;]&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;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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;filteredItems&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFilteredItems&lt;/span&gt;&lt;span class="p"&gt;]&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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;setFilteredItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nx"&gt;MOCK_DATA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&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;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&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;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&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;handleSearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;setSearchTerm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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;Unfortunately, it’s not a good approach as your application have to render twice when keyword changes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First render for setSearchTerm call in handleSearch function&lt;/li&gt;
&lt;li&gt;After searchTerm is updated to expected value, &lt;code&gt;useEffect&lt;/code&gt; will be called and invoke setFilteredItems and trigger second render&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead, we can batch the state updates into one, as React is very efficient at rendering. Even if we call two &lt;code&gt;setState&lt;/code&gt; functions simultaneously, they only trigger a single render with this approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearchTerm&lt;/span&gt;&lt;span class="p"&gt;]&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;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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;filteredItems&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFilteredItems&lt;/span&gt;&lt;span class="p"&gt;]&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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;handleSearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;const&lt;/span&gt; &lt;span class="nx"&gt;newvalue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;

    &lt;span class="nf"&gt;setSearchTerm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newvalue&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&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;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newvalue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setFilteredItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;
  
  
  Inconsistence in coding style
&lt;/h3&gt;

&lt;p&gt;Imagine you’re working in a team where they're very good in React and you use &lt;code&gt;useEffect&lt;/code&gt; like a tracking tools for all purpose. As a result, it will make your college are not happy and it’s might impact to your promotion&lt;/p&gt;

&lt;h3&gt;
  
  
  useEffect remains unintuitive
&lt;/h3&gt;

&lt;p&gt;Yes, if we don't use &lt;code&gt;useEffect&lt;/code&gt; correctly, you might encounter bugs due to its unintuitive behavior, as shown in the image below.&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%2F0g2w0qkdfuas480eugdh.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%2F0g2w0qkdfuas480eugdh.png" alt="Image description" width="606" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason for above bug come from overusing of &lt;code&gt;useEffect&lt;/code&gt; then the author doesn't know when &lt;code&gt;useEffect&lt;/code&gt; is invoked in React render lifecycle (TLDR: React will only render when state change → After render, React will start invoking &lt;code&gt;useEffect&lt;/code&gt; function to handle side-effect)&lt;br&gt;
If you understand React render lifecycle you may realize the pathname inside &lt;code&gt;useEffect&lt;/code&gt; code will always be the latest value (not the previous one)&lt;/p&gt;
&lt;h2&gt;
  
  
  What is the good example for correct usage of &lt;code&gt;useEffect&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;As we mention earlier &lt;code&gt;useEffect&lt;/code&gt; is created for handling side-effect, so we expect code inside &lt;code&gt;useEffect&lt;/code&gt; should be related to side-effect (updating DOM, managing localStorage, calling API, etc..)&lt;br&gt;
 In below example, we're trying to build a component that can help us update the tab title by clicking different button&lt;br&gt;
 We need &lt;code&gt;useEffect&lt;/code&gt; in this scenario to help us synchronize the data between internal state (React state) and external state (window.document.title)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;DocumentTitleChanger&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTitle&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="s1"&gt;Default Title&lt;/span&gt;&lt;span class="dl"&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="c1"&gt;// This will run every time "title" changes.&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;title&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;title&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Change Document Title&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Title One&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Title One&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Title Two&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Title Two&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Title Three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Title Three&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;That it’s for this post. I hope this post can help you understand more about &lt;code&gt;useEffect&lt;/code&gt; and help you use &lt;code&gt;useEffect&lt;/code&gt; in a more correct ways. Mastering side-effect is not an easy task in React, please let's me know your attention by follow / react on this post. If you find it useful, I will work on writing more post in the future for mastering side-effect&lt;/p&gt;

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