<?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: Bibi</title>
    <description>The latest articles on DEV Community by Bibi (@bibschan).</description>
    <link>https://dev.to/bibschan</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%2F384126%2Ff157f58a-6195-4acc-94db-414cc141efa6.png</url>
      <title>DEV Community: Bibi</title>
      <link>https://dev.to/bibschan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bibschan"/>
    <language>en</language>
    <item>
      <title>Best UI Libraries for Developers (trust me, I'm a dev)</title>
      <dc:creator>Bibi</dc:creator>
      <pubDate>Sat, 11 Jan 2025 21:13:53 +0000</pubDate>
      <link>https://dev.to/bibschan/best-ui-libraries-for-developers-trust-me-im-a-dev-2f2i</link>
      <guid>https://dev.to/bibschan/best-ui-libraries-for-developers-trust-me-im-a-dev-2f2i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you're building modern web apps, UI libraries are your secret weapon. They save you time, keep your design consistent, and supercharge the user experience. These libraries provide reusable components and systems to get you up and running fast. But, not all UI libraries are made equal. They each have their own design philosophies, feature sets, and best use cases. Here's your ultimate guide to some of the best UI libraries out there, complete with overviews, pros, cons, and my personal recommendations.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. shadcn/ui
&lt;/h2&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%2Fco50429sjh3elfifd915.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%2Fco50429sjh3elfifd915.png" alt="Image description" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
Meet shadcn/ui—an open-source UI library that's sleek, customizable, and perfectly at home with Tailwind CSS. Its minimalistic design lets you craft lightweight, flexible web apps with ease, all while keeping performance and accessibility in check. Best part? It gives you full control over your styling with utility-first CSS, meaning no more feeling locked into a design pattern you don't love.&lt;/p&gt;

&lt;p&gt;The real magic of shadcn/ui? It's modular and headless (yep, unstyled!), so you can add your own flair without being told what to do. It meshes beautifully with Tailwind, giving you those utility classes you crave for the perfect dev workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom Dashboards: Flexibility to build dashboards that are totally your own.&lt;/li&gt;
&lt;li&gt;Lightweight Applications: Great for projects that don't want to be bogged down with too many dependencies.&lt;/li&gt;
&lt;li&gt;Tailwind Users: A match made in heaven for Tailwind CSS users who love utility-first CSS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Complex Enterprise Apps: If you're building something massive with rich data grids and tons of components, this might not have everything you need out of the box.&lt;/li&gt;
&lt;li&gt;Beginners to Tailwind: If you're still getting comfy with utility-first CSS, prepare for a bit of a learning curve.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Material-UI (MUI)
&lt;/h2&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%2F86b5awz06b5gbkpols26.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F86b5awz06b5gbkpols26.jpg" alt="Image description" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
Material-UI (now called MUI) is one of the most widely used React component libraries, built according to Google's Material Design system. It's renowned for its highly polished, consistent, and comprehensive set of components, ranging from simple buttons and icons to complex grids, charts, and form controls. MUI is perfect for developers who need a robust, production-ready solution that includes rich UI components.&lt;/p&gt;

&lt;p&gt;MUI provides a highly customizable theming system, allowing you to control aspects like color palettes, typography, and spacing. It also comes with a set of built-in utility functions for managing accessibility, layouts, and responsiveness. With extensive documentation and a large community, MUI is a top choice for many developers building web applications that prioritize both aesthetics and functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enterprise Applications: MUI's rich component library is perfect for complex, data-intensive applications.&lt;/li&gt;
&lt;li&gt;Cross-Platform Projects: It allows for a consistent design language between web and mobile (via frameworks like React Native).&lt;/li&gt;
&lt;li&gt;Projects Needing Custom Themes: If you want to implement your own unique branding while adhering to Material Design standards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight Applications: MUI can be heavy, and if you don't need the full set of features, it may increase the app's bundle size unnecessarily.&lt;/li&gt;
&lt;li&gt;Non-Material Design Projects: If your project doesn't align with Material Design principles, using MUI could feel forced or inconsistent with the desired user experience.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Radix UI
&lt;/h2&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%2Fn8c1qkcxo1xjxnmuzpqf.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%2Fn8c1qkcxo1xjxnmuzpqf.png" alt="Image description" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
Radix UI is a low-level UI component library focused on providing developers with unstyled, accessible components. Unlike other UI libraries, Radix UI does not impose a design system, giving developers the flexibility to apply their own design. This approach is ideal for developers who want complete control over the look and feel of their components while still ensuring accessibility and functionality.&lt;/p&gt;

&lt;p&gt;Radix UI offers a set of composable, unstyled components that come with built-in accessibility features and behavior. It's built with a focus on the UI logic, leaving the styling completely up to you. This gives you the power to create truly custom user interfaces without being tied to any predefined design system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom Design Projects: Radix UI is perfect when you need to build a highly customized UI and don't want to work within a predefined design system.&lt;/li&gt;
&lt;li&gt;Design Systems and Branding: If you are building your own design system or working on a project that requires a unique visual identity, Radix UI offers flexibility while ensuring accessibility and functionality.&lt;/li&gt;
&lt;li&gt;Accessibility-First Apps: Radix UI's components come with built-in accessibility features, making it ideal for applications that need to meet WCAG standards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pre-Styled, Opinionated UI Needs: If you need a fully styled, ready-to-use set of components, Radix UI might not be the best choice since it's unstyled by default.&lt;/li&gt;
&lt;li&gt;Time-Sensitive Projects: Radix UI requires you to do your own styling, so if you need to ship a project quickly, this might add extra time to the development process.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Ant Design
&lt;/h2&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%2Fnrh4gyk9iegldw0hkb3s.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%2Fnrh4gyk9iegldw0hkb3s.png" alt="Image description" width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
Ant Design is the heavy hitter in the UI library game, created by Ant Financial. It's got everything you could ever need for a polished, user-friendly UI—form controls, navigation, data visualization tools, you name it. If you're looking for a library that makes your app look like it's been designed by a pro, this is your go-to.&lt;/p&gt;

&lt;p&gt;Ant Design is all about consistency. With a solid design language and a theming system that lets you customize everything from colors to typography, you'll be able to tailor the UI to your heart's content. It's perfect for big projects that require a unified design system and top-notch UI components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enterprise Applications: If you're building something large-scale and data-heavy, Ant Design has your back with a full set of tools.&lt;/li&gt;
&lt;li&gt;Admin Dashboards: Want a ready-made solution to build those complex admin interfaces? This library's got you covered.&lt;/li&gt;
&lt;li&gt;Data-Heavy Applications: Ant Design's got strong support for tables, forms, and charts—perfect for any app that's juggling tons of data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Non-Enterprise Projects: If you're building something small or lightweight, Ant Design might feel like overkill.&lt;/li&gt;
&lt;li&gt;Projects with Custom Branding: Ant Design's design language is pretty specific, so if you're aiming for a highly unique look, this might not be your best bet.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Chakra UI
&lt;/h2&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%2F68ptgdq4idvyenu60n3d.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%2F68ptgdq4idvyenu60n3d.png" alt="Image description" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
Chakra UI is a simple, modular, and accessible component library for React. It provides a set of building blocks for creating modern user interfaces with a focus on simplicity and accessibility. Chakra UI offers a flexible design system that makes it easy to create custom UI components by using pre-designed building blocks, which are highly customizable via style props.&lt;/p&gt;

&lt;p&gt;Chakra UI's design philosophy emphasizes ease of use, accessibility, and consistency. It comes with a wide range of components, such as buttons, modals, and tooltips, that are designed to be customizable without having to deal with complex stylesheets. The library also integrates well with modern CSS-in-JS libraries like Emotion.&lt;/p&gt;

&lt;p&gt;One of Chakra UI's most notable features is its accessibility-first approach. All components come with built-in support for keyboard navigation, screen readers, and other assistive technologies, making it a great choice for projects that prioritize inclusive design.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customizable UI Components: Ideal for projects where you need a set of flexible, accessible components that can be customized via props.&lt;/li&gt;
&lt;li&gt;Accessibility-Focused Projects: Works great for projects that prioritize accessible and inclusive user experiences.&lt;/li&gt;
&lt;li&gt;Developer Productivity: For developers who want a streamlined process to build React UIs with a component library that's easy to integrate and use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Highly Complex UIs: If you're working on a project that requires highly intricate or custom designs, Chakra UI's pre-built components might not be flexible enough for the level of customization needed.&lt;/li&gt;
&lt;li&gt;Non-React Projects: Chakra UI is tailored for React, so it's not suitable for projects built with other JavaScript frameworks.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Tailwind UI
&lt;/h2&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%2Frepository-images.githubusercontent.com%2F592709381%2F385dcc25-ba70-4d2e-a50f-5a4f3c02c56b" 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%2Frepository-images.githubusercontent.com%2F592709381%2F385dcc25-ba70-4d2e-a50f-5a4f3c02c56b" alt="Image description" width="800" height="693"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
Tailwind UI is a commercial component library built by the creators of Tailwind CSS. It provides a collection of professionally designed, fully responsive UI components, optimized for use with Tailwind CSS. Since it's built on top of the utility-first design system of Tailwind, it offers a unique approach to building UI elements that are highly customizable and adaptable to any project.&lt;/p&gt;

&lt;p&gt;Unlike other component libraries that provide pre-styled components with a fixed design, Tailwind UI components allow for seamless integration with Tailwind's utility classes, making it a perfect fit for developers who are already using Tailwind CSS in their projects.&lt;/p&gt;

&lt;p&gt;One of the key advantages of Tailwind UI is its ability to provide highly customizable components out of the box. Developers can adjust the design using Tailwind's utility classes, resulting in a tailored UI that matches their branding or project requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tailwind CSS Users: Tailwind UI is a natural choice for developers who are already using Tailwind CSS and want pre-built components that fit seamlessly into their design system.&lt;/li&gt;
&lt;li&gt;Customizable UI Components: Ideal for projects that require high levels of customization, where the utility-first approach is preferred.&lt;/li&gt;
&lt;li&gt;Responsive Layouts: Perfect for building responsive UIs that adapt across a wide variety of devices and screen sizes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If You Don't Use Tailwind CSS: If you're not using Tailwind CSS in your project, the components in Tailwind UI may not be as useful or easy to integrate.&lt;/li&gt;
&lt;li&gt;If You Need a Complete Design System: Tailwind UI offers components but doesn't provide a complete design system, so it may not be the best fit for projects that require a full design toolkit with extensive guidelines.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Bootstrap
&lt;/h2&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%2Fs15fy7fd4rkdkwbqtj1n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs15fy7fd4rkdkwbqtj1n.jpg" alt="Image description" width="516" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
Bootstrap is one of the most popular open-source front-end frameworks that provides a collection of CSS and JavaScript components. Developed by Twitter, Bootstrap offers a grid system, pre-designed components, and JavaScript plugins to help developers build responsive and mobile-first websites with minimal effort. The framework follows a "mobile-first" design philosophy, meaning it prioritizes responsiveness for mobile devices, and then adapts to larger screens.&lt;/p&gt;

&lt;p&gt;Bootstrap includes a robust set of default styles for buttons, forms, typography, navigation bars, and other UI elements, allowing developers to quickly prototype or build fully responsive and feature-rich UIs without having to worry about CSS from scratch.&lt;/p&gt;

&lt;p&gt;The most significant advantage of Bootstrap is its widespread adoption and huge community support. There are extensive resources, tutorials, and third-party libraries that extend Bootstrap, making it a go-to solution for building websites and web apps rapidly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Use Cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quick Prototyping: Bootstrap is perfect for creating quick prototypes or MVPs (Minimum Viable Products) due to its ready-to-use components and grid system.&lt;/li&gt;
&lt;li&gt;Responsive Websites: Its mobile-first approach and built-in media queries make it an excellent choice for developing websites that need to work across multiple screen sizes.&lt;/li&gt;
&lt;li&gt;Minimal Customization Needs: For projects where minimal custom styles are required, Bootstrap's pre-built components offer a fast, consistent solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Not to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If You Need a Highly Custom Design: Bootstrap's default look is widely recognized, so if your project requires a completely unique design, it may not be the best choice, as the out-of-the-box components might be too generic.&lt;/li&gt;
&lt;li&gt;If You Need Lightweight Styles: Bootstrap is a large framework that may come with more features than you need, leading to potentially unnecessary bloat in your project.&lt;/li&gt;
&lt;li&gt;If You Need a More Modern Approach: Although Bootstrap is continuously updated, its approach to styles (particularly using pre-defined classes for layout) may feel outdated compared to newer utility-first frameworks like Tailwind CSS.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Closing Thoughts&lt;/strong&gt;&lt;br&gt;
To wrap it up, each of these UI libraries—Shadcn/UI, Material-UI (MUI), and Radix UI—brings something different to the table.&lt;/p&gt;

&lt;p&gt;Shadcn/UI is all about flexibility and freedom, giving you the power to craft lightweight, highly customizable web apps without sacrificing performance or accessibility. It's a solid pick for Tailwind lovers and anyone who wants complete control over their design.&lt;/p&gt;

&lt;p&gt;Material-UI (MUI), on the other hand, is the go-to for enterprise-level apps that need a polished, comprehensive set of components and a robust theming system. It's ideal if you're building something complex or need cross-platform consistency, though it might be a bit overkill for lighter projects.&lt;/p&gt;

&lt;p&gt;Radix UI plays in its own league, offering unstyled, accessible components that let you create fully custom user interfaces, making it perfect for those working on design systems or accessibility-first apps. Ultimately, the right choice depends on your project's scope and what kind of flexibility or structure you're looking for.&lt;/p&gt;

&lt;p&gt;And that's it for now! But don't worry, there's more on the way. If you've been enjoying this dive into UI libraries and all things web dev, there's plenty more to come. So, keep an eye out for my next posts 👀&lt;/p&gt;




</description>
      <category>ui</category>
      <category>design</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A Quick Guide to React's useState and useRef</title>
      <dc:creator>Bibi</dc:creator>
      <pubDate>Mon, 19 Aug 2024 14:14:20 +0000</pubDate>
      <link>https://dev.to/bibschan/react-series-usestate-vs-useref-3hm1</link>
      <guid>https://dev.to/bibschan/react-series-usestate-vs-useref-3hm1</guid>
      <description>&lt;p&gt;When you're starting out, useState and all of its quirks can be  overwhelming enough. Now throw useRef into the mix and you get a loud brain explosion.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExd2k4cGNpaTdiODBwc2JxNzlhZ3BtYTJ4OGVobWNxY2hpZ21zYjB3MSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/jzQXsabuFUUX6/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExd2k4cGNpaTdiODBwc2JxNzlhZ3BtYTJ4OGVobWNxY2hpZ21zYjB3MSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/jzQXsabuFUUX6/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;I thought it would be helpful to dive a bit deeper into the technicalities of useRef vs useState, as engineers usually have a hard time understanding the differences between them and when to choose one over the other. &lt;/p&gt;

&lt;p&gt;Here are my definitions for each, in broad strokes and obviously oversimplified:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;useState is a hook that lets you &lt;u&gt;access and update a value&lt;/u&gt;, triggering a re-render.&lt;/p&gt;

&lt;p&gt;useRef is a hook that lets you &lt;u&gt;reference a value&lt;/u&gt; that’s not needed for rendering.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;You might be asking yourself, &lt;strong&gt;why&lt;/strong&gt; is it important to understand what each of these bring to the table? Well, you might be tempted to slap useState into everything, simply because it does work.&lt;/p&gt;

&lt;p&gt;Yes, &lt;strong&gt;HOWEVER&lt;/strong&gt;, the more complex your app becomes with components passing state props 5 levels down, triggering a bunch of unnecessary updates along the way, you're probably going to start running into performance issues sooner than you imagine. &lt;/p&gt;

&lt;p&gt;Another misconception a lot of devs have is thinking that useRef can only manipulate and access DOM elements, which is sad because y'all are missing out on the other 99 things¹ useRef can do.&lt;/p&gt;

&lt;p&gt;Let's start with useState! Keep your ears open now, my dear dev ʕ◉ᴥ◉ʔ&lt;/p&gt;




&lt;h2&gt;
  
  
  ✨ &lt;strong&gt;useState&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;useState is a powerful but easy way to update the view once a variable's value changes, it keeps the latest value synchronized with the screen without manually performing any operations ourselves. The declaration syntax goes like this &lt;code&gt;const [memesILiked, setMemesILiked] = useState(9000)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's talk about what happens under the hood when you perform an operation with useState. &lt;/p&gt;

&lt;p&gt;Updating a state value triggers a re-render, and as you might imagine, re-rendering the view is a &lt;strong&gt;VERY&lt;/strong&gt; expensive operation for the browser to handle. Here's how React and the browser operate in conjunction to make sure your app is updated:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Event Trigger&lt;/strong&gt;: An event triggers a state update. A click, timer or anything really. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State Update&lt;/strong&gt;: setState is called and schedules an update for the component, the component is marked as "dirty" (needing re-render).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reconciliation Phase&lt;/strong&gt;: React starts the reconciliation between the new virtual DOM and the old virtual DOM. It recursively re-renders the component and all of its children.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Checking the differences&lt;/strong&gt;: React compares the new virtual DOM tree with the previous one. The changes are stored in a list of updates to be applied to the real DOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Render Phase&lt;/strong&gt;: The render method or function component is called with the new state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commit Phase&lt;/strong&gt;: React applies the changes from the diffing process to the real DOM. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update the DOM&lt;/strong&gt;: The real DOM is updated to reflect the new state. The browser re-paints the DOM, visually updating the UI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Post-render Effects&lt;/strong&gt;: Any effects that were scheduled to run after the component was re-rendered are called. This includes useEffect hooks that were registered with dependencies that changed during the render.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Phew&lt;/strong&gt;, that's a lot of stuff... While the above process ensures that your UI stays in sync with your application state, it also highlights why excessive or unnecessary re-renders can lead to performance issues. Luckily, React provides several strategies and tools to help optimize this process, like &lt;strong&gt;useMemo&lt;/strong&gt; and &lt;strong&gt;useCallback&lt;/strong&gt;, but that's beyond the scope of this article!&lt;/p&gt;

&lt;p&gt;In summary, useState is a pretty handy hook, and when used right, it can provide the user a great experience. Take theme toggling, for example. With useState, you can easily switch between light and dark modes, giving your users that instant gratification of seeing the app transform based on their preferences. &lt;/p&gt;




&lt;h2&gt;
  
  
  ✨ &lt;strong&gt;useRef&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now, let’s talk useRef. While useState is all about triggering re-renders when state changes, useRef is like the quiet observer that never wants to draw attention to itself. It's perfect for storing mutable values that don't require a re-render when they change. The syntax looks like &lt;code&gt;const memeRef = useRef(null)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;useRef is most often used for accessing DOM elements directly. For instance, if you need to focus an input field programmatically, useRef can hold a reference to that element. But useRef's capabilities go beyond just DOM access. It can also store &lt;strong&gt;any&lt;/strong&gt; mutable value! And this, my friends, is where the magic happens 🪄🪄🪄 (in my opinion anyway).&lt;/p&gt;

&lt;p&gt;Think of it like this: useRef is a way to &lt;strong&gt;persist values&lt;/strong&gt; across renders without triggering a re-render. This makes it perfect for storing data like timers, counters, or even the previous state of a component. Unlike useState, updating a ref doesn't notify React to re-render your component. It just quietly updates the value and carries on with its business.&lt;/p&gt;

&lt;p&gt;Here's a practical example: let's say you want to implement a simple counter, but you don’t want the UI to update every time you increment the counter. You could use useRef to store the counter value. The counter would increment as expected, but because the component doesn’t care about this ref value for rendering purposes, no re-rendering would happen.&lt;/p&gt;

&lt;p&gt;useRef is also great in preserving the latest value of a state without causing additional renders. For example, if you're using an &lt;strong&gt;interval&lt;/strong&gt; to update a value, but you don't want that value to trigger a re-render every millisecond, useRef is your go-to tool. It allows the value to change in the background, keeping your UI responsive and avoiding unnecessary re-renders.&lt;/p&gt;

&lt;p&gt;In summary, useRef is best used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Accessing DOM elements&lt;/strong&gt;: Classic use case, like focusing an input field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storing mutable values&lt;/strong&gt;: That don't require re-rendering, such as timers or previous values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintaining values across renders&lt;/strong&gt;: Without causing a re-render, keeping your UI smooth and efficient.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Now that you &lt;strong&gt;HOPEFULLY&lt;/strong&gt; understand the difference (if I've done my duty correctly²), let's dive into a few not-so-common use cases. I will focus on useRef a bit more as I feel like it's the unsung hero here. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tracking Component Mounting Status&lt;/strong&gt;: useRef can be used to track whether a component is mounted or unmounted, this can be useful for avoiding state updates after unmounting.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Holding Static Values&lt;/strong&gt;: For storing static values that don't change between renders, like a constant or a cached value, useRef is more efficient than useState.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Preventing Re-Initialization&lt;/strong&gt;: If you want to prevent a piece of code from re-running every render (for example, initializing a WebSocket connection).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storing Previous Callbacks&lt;/strong&gt;: If you need to keep a reference to a previous callback function, useRef can store the previous function reference without affecting the component's rendering cycle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Referencing Timer IDs&lt;/strong&gt;: When working with timers (like setTimeout or setInterval), store the timer ID in a useRef to avoid triggering a re-render every time the timer is set or cleared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Triggering Animations&lt;/strong&gt;: For triggering animations imperatively (like a CSS transition or scroll animation), useRef can be used to directly interact with DOM elements without causing re-renders.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;While useState is crucial for managing and reacting to state changes that should trigger re-renders, useRef is the silent partner that helps you manage state without disrupting the UI. &lt;/p&gt;

&lt;p&gt;Knowing when to use each can save you from potential performance issues and make your React applications more efficient and maintainable! &lt;/p&gt;




&lt;p&gt;Thanks for reading, if you made it here, PAWS UP AND HIGH FIVE! ⊹⋛⋋( ՞ਊ ՞)⋌⋚⊹&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Footnotes:&lt;br&gt;
¹ Obviously an exaggeration.&lt;br&gt;
² I'm a bit dramatic, in case you couldn't tell.&lt;/em&gt;&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%2Fds1ooof0f44uffh1id8h.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%2Fds1ooof0f44uffh1id8h.png" alt="cat paw waving goodbye" width="240" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Vercel's latest product is the best thing since sliced bread</title>
      <dc:creator>Bibi</dc:creator>
      <pubDate>Sat, 06 Jul 2024 21:49:40 +0000</pubDate>
      <link>https://dev.to/bibschan/vercels-latest-product-is-the-best-thing-since-sliced-bread-29c4</link>
      <guid>https://dev.to/bibschan/vercels-latest-product-is-the-best-thing-since-sliced-bread-29c4</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;all opinions are my own and don't reflect my employers in any way (even though it should). I write with a humorous flair and if you get butt hurt that's your problem ٩(＾◡＾)۶&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You should know by now that I'm a NextJS evangelist, so it should come as no surprise that I'm knocking door to door spreading the Vercel gospel yet again -- BUT, hold on to your seats my dear devs... because today, I bring a fresh-out-of-the-oven tool for y'all to delight yourselves with. &lt;strong&gt;Enter our lord and savior, v0&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExcjZpMTB4aWJxMmU2cmxvMzB3OXIzbnR2cnlmNGU4b2lqZWJwaW9iNCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/dX3dMAyBWYyAV4zhOm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExcjZpMTB4aWJxMmU2cmxvMzB3OXIzbnR2cnlmNGU4b2lqZWJwaW9iNCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/dX3dMAyBWYyAV4zhOm/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;v0 is Vercel's newest addition to their product catalog, it is a &lt;strong&gt;Generative UI&lt;/strong&gt; tool that uses simple text prompting and images to generate pages for you. Forget boilerplate code and templates, v0 aims to take you from a blank screen to a complete page faster than ever before!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how does it work?&lt;/strong&gt; After you submit your prompt, the platform gives you three choices of AI-generated user interfaces. You can pick one, copy the entire source code generated, or refine it further until you get what you want!&lt;/p&gt;

&lt;p&gt;Sounds like any other AI tool out there, right? Yes and no. Yes, it's AI. But no, it's better than anything else out there. These are just some of its initial key features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Follow up prompting:&lt;/strong&gt; v0 allows you to endlessly refine the UI to your own taste using prompts, zero code involved! You can select individual parts of the UI to fine tune your creation. 
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.discordapp.com%2Fattachments%2F1258446643888656404%2F1258459529289662555%2F2.gif%3Fex%3D66881f44%26is%3D6686cdc4%26hm%3D7b69b4b3130e4ce4accfad6240e2d03c3ef10f460239e04ac55f014313e16c48%26" width="800" height="400"&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Switch between canvas and code:&lt;/strong&gt; seamlessly make code and UI adjustments on the fly, your changes are reflected instantly on the generated UI. Think of Webflow meets VS Code!
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.discordapp.com%2Fattachments%2F1258446643888656404%2F1258459528320520192%2F1.gif%3Fex%3D66881f44%26is%3D6686cdc4%26hm%3D2a209815bfd13d0adbb1d61b04a03984000ea1707e9755abe39898b31db1010e%26" width="800" height="400"&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Public/private mode and community templates:&lt;/strong&gt; Browse through what others have published to the community, giving you an even better starting point reference. Alternatively, you can keep your work in stealth and no one will know what you're cooking!
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.discordapp.com%2Fattachments%2F1258446643888656404%2F1258459528802992228%2F3.gif%3Fex%3D66881f44%26is%3D6686cdc4%26hm%3D2235d560d2cdefac85f4a7d19f1faeaeedb9524aedb8e44e5b9e6663efd9329f%26" width="800" height="400"&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Leverage ShadCN/UI and Tailwind:&lt;/strong&gt; You get pre-built, open-source, fully responsive UI components with &lt;a href="https://ui.shadcn.com/" rel="noopener noreferrer"&gt;ShadCN&lt;/a&gt; and industry-standard Tailwind. Because nobody wants to pay for &lt;a href="https://mui.com/material-ui/" rel="noopener noreferrer"&gt;MaterialUI&lt;/a&gt; 👀
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.discordapp.com%2Fattachments%2F1258446643888656404%2F1258459527527927808%2F4.gif%3Fex%3D66881f44%26is%3D6686cdc4%26hm%3Da0d870af863560b47088c4acb9de7b52bd59e0ee347a7357a561978b72a4d62b%26" width="800" height="400"&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;A note on accessibility&lt;/strong&gt; -- While v0 does generate ARIA attributes to incorporate accessibility standards, it still leaves room for improvement in terms of inclusive design. I can assure you though, it's more than what your company is doing for accessibility anyway 👀&lt;/p&gt;




&lt;h2&gt;
  
  
  My thoughts on v0 ໒(⊙ᴗ⊙)७✎▤
&lt;/h2&gt;

&lt;p&gt;While the tool is currently focused on frontend code generation and can't perform data fetching yet, I firmly believe eventually this feature will be around with the official alpha release. While it does generate generic code, eventually, I know for a fact it will integrate with your existing codebase and Figma design system, allowing it to contextualize the output and create that seamless experience that we developers love -- a good copy and paste that just works!&lt;/p&gt;

&lt;p&gt;Now, I want to wrap this article with a prediction. Two years down the road, vanilla React will be gone and forgotten. NextJS will take over as the most popular framework and Vercel will release a CLI version of v0 with some neat VS Code plugins. Frontend engineering will be more about prompting these tools for the best output, rather than coding useless media-queries.&lt;/p&gt;

&lt;p&gt;We'll have to wait to find out, and in the meantime, I shall continue spreading the word door to door... &lt;/p&gt;




&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExeTFwcXowd2RoNXowa2I4NGV1cTR5bHVpYm5mNHNkZGMxbG4zeXg1diZlcD12MV9naWZzX3NlYXJjaCZjdD1n/zOwzTlnOnx1T9JZgql/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExeTFwcXowd2RoNXowa2I4NGV1cTR5bHVpYm5mNHNkZGMxbG4zeXg1diZlcD12MV9naWZzX3NlYXJjaCZjdD1n/zOwzTlnOnx1T9JZgql/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Do you have five minutes to hear the word of our lord and saviour today?&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Play with v0 and see the magic for yourself: &lt;a href="https://v0.dev/" rel="noopener noreferrer"&gt;https://v0.dev/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading my silly article ๑(◕‿◕)๑~~~&lt;/p&gt;

&lt;p&gt;All resources on this page were sourced from the v0 launch keynote.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>ai</category>
      <category>news</category>
    </item>
    <item>
      <title>Closures in JS 🔒</title>
      <dc:creator>Bibi</dc:creator>
      <pubDate>Wed, 26 Jun 2024 03:38:46 +0000</pubDate>
      <link>https://dev.to/bibschan/closures-in-js-1gik</link>
      <guid>https://dev.to/bibschan/closures-in-js-1gik</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;TL/DR:&lt;/strong&gt;&lt;br&gt;
Closures are a synthetic inner delimiter that has access to its parent's scope, even after the parent function has finished executing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Not clear? I didn't get it either. Read along partner!&lt;/p&gt;




&lt;h2&gt;
  
  
  🔒 Closures 🔒
&lt;/h2&gt;

&lt;p&gt;Ah, closures. Sounds pretty straight forward... whatever's inside the curly braces right?! Well, yes and no. This topic is quite simple but there's a lot to explore in it, you'll wonder whether you accidentally stumbled into a parallel universe after reading, I guarantee. But fear not, my fellow wizards, for today we shall unravel the enigma that is closures! &lt;/p&gt;

&lt;p&gt;First, let's start with the textbook definition because I love a good MDN reference: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;a closure is the combination of a function bundled together with references to its surrounding state (the lexical environment), even after the outer function has returned. Kinda like having the key to a treasure chest of variables, even when you've left the pirate ship!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Okay that wasn't MDN verbatim, but you get the idea. So, how does this sorcery work?? Well, when you create a function within another, the inner one has access to the variables and parameters of the outer function. This is because the inner function forms a closure, maintaining access to the environment in which it was created. It's like the inner function remembers its surroundings!&lt;/p&gt;

&lt;p&gt;Here's a classic example to illustrate closures:&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%2Fbnnt15izhwdv2tqauf1i.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%2Fbnnt15izhwdv2tqauf1i.png" alt="1" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, &lt;code&gt;outerFunction&lt;/code&gt; takes a parameter &lt;code&gt;x&lt;/code&gt; and has a local variable &lt;code&gt;y&lt;/code&gt;. It defines an &lt;code&gt;innerFunction&lt;/code&gt; that accesses both &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, and then returns the &lt;code&gt;innerFunction&lt;/code&gt;. When we assign the result of calling &lt;code&gt;outerFunction(5)&lt;/code&gt; to the variable closure, we are essentially capturing the &lt;code&gt;innerFunction&lt;/code&gt; along with its environment (where &lt;code&gt;x&lt;/code&gt; is 5 and &lt;code&gt;y&lt;/code&gt; is 10). Even though &lt;code&gt;outerFunction&lt;/code&gt; has finished executing, the closure still remembers the values of &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, allowing us to invoke it later and get the expected output of 15.&lt;/p&gt;

&lt;p&gt;This was just a simple example, but closures actually have various practical applications, such as data privacy, factories, and memoization. They allow you to create functions with private state and encapsulate behaviour, leading to more modular and reusable code. Let's do a deeper dive into these specific applications:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Private Variables and Encapsulation&lt;/strong&gt; 🔒&lt;/p&gt;

&lt;p&gt;Closures can be used to create private variables and achieve encapsulation. Consider the following example:&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%2F7qq12moir5p0n9brmhit.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7qq12moir5p0n9brmhit.jpg" alt="2" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the &lt;code&gt;createCounter&lt;/code&gt; function returns an object with an &lt;code&gt;increment&lt;/code&gt; method. The count variable is defined within the &lt;code&gt;createCounter&lt;/code&gt; function and is not accessible from the outside. The &lt;code&gt;increment&lt;/code&gt; method, being a closure, has access to the &lt;code&gt;count&lt;/code&gt; variable and can modify it. Each time &lt;code&gt;counter.increment()&lt;/code&gt; is called, the &lt;code&gt;count&lt;/code&gt; is incremented, but it remains private and cannot be accessed directly from the outside.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;2. Function Factories&lt;/strong&gt; 🏭 &lt;br&gt;
Closures can be used to create function factories, which are functions that generate other functions with customized behaviour. Here's an example:&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%2F8b433rryq6nxioupj6ki.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8b433rryq6nxioupj6ki.jpg" alt="3" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the &lt;code&gt;multiplyBy&lt;/code&gt; function takes a &lt;code&gt;factor&lt;/code&gt; parameter and returns a new function that multiplies a given &lt;code&gt;number&lt;/code&gt; by the &lt;code&gt;factor&lt;/code&gt;. We create two separate functions, &lt;code&gt;double&lt;/code&gt; and &lt;code&gt;triple&lt;/code&gt;, by calling &lt;code&gt;multiplyBy&lt;/code&gt; with different factors. Each returned function forms a closure, capturing its own &lt;code&gt;factor&lt;/code&gt; value, allowing us to multiply numbers by the respective factors.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;3. Memoization&lt;/strong&gt; ⏰ &lt;br&gt;
Closures can be used to implement memoization, which is a technique to cache the results of expensive function calls and return the cached result when the same inputs occur again. Here's an example of memoizing a factorial function:&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%2F6uptx379uq4dt3tmbgb4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6uptx379uq4dt3tmbgb4.jpg" alt="4" width="800" height="756"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the &lt;code&gt;memoizedFactorial&lt;/code&gt; function returns a closure that serves as the actual &lt;code&gt;factorial&lt;/code&gt; function. The closure maintains a &lt;code&gt;cache&lt;/code&gt; object to store previously computed results. Whenever the &lt;code&gt;factorial&lt;/code&gt; function is called with a number &lt;code&gt;n&lt;/code&gt;, it first checks if the result is already in the cache. If it is, it returns the cached result. Otherwise, it calculates the factorial recursively and stores the result in the cache before returning it. Subsequent calls with the same &lt;code&gt;n&lt;/code&gt; will retrieve the result from the cache, avoiding redundant calculations.&lt;/p&gt;




&lt;p&gt;These examples are obviously simple but the applications of closures in JavaScript can be much more complex. They can provide a powerful mechanism for data privacy, code organization, and optimization, when done right!&lt;/p&gt;

&lt;p&gt;However, closures can also lead to some gotchas if not used carefully. One common pitfall is creating closures in loops, where the closure captures the last value of the loop variable. But that's a story for another day! &lt;/p&gt;




&lt;h2&gt;
  
  
  Alright, I think that's it for me ┗(･ω･;)┛
&lt;/h2&gt;

&lt;p&gt;Here are some key points to take home:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A closure is a function that remembers the environment in which it was created. It has access to variables and parameters of the outer function.&lt;/li&gt;
&lt;li&gt;It allows a function to access variables from its outer (enclosing) scope, even after the outer function has finished executing.&lt;/li&gt;
&lt;li&gt;Closures can access and manipulate variables from the outer scope, even after the outer function has returned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope you learned something with me today! (´◡`)&lt;br&gt;
Bibi&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>career</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hoisting for dummies (aka me)</title>
      <dc:creator>Bibi</dc:creator>
      <pubDate>Wed, 05 Jun 2024 04:35:16 +0000</pubDate>
      <link>https://dev.to/bibschan/hoisting-for-dummies-aka-me-2i6k</link>
      <guid>https://dev.to/bibschan/hoisting-for-dummies-aka-me-2i6k</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;TL/DR:&lt;/strong&gt;&lt;br&gt;
Hoisting is a useful JavaScript feature that allows you to use variables and functions before they are declared. However, it's crucial to remember that only declarations are hoisted, not assignments. The keywords &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; have different hoisting behaviours, and strict mode can be used to avoid potential hoisting-related issues.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  A quick guide to hoisting 🏗️
&lt;/h2&gt;

&lt;p&gt;Hoisting is one of those buzzwords you hear about only two times in your dev life, one is during that pesky interview and the other is when you're in school. But guess what? I never learned it (or maybe I did and forgot, sorry teacher). So, let's find out what the mystery of ✨hoisting✨ is.&lt;/p&gt;

&lt;p&gt;The textbook definition of hoisting is &lt;strong&gt;&lt;em&gt;to lift something heavy, to move from a lower position to a higher position.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Interesting... But still doesn't click for me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; defines hoisting as: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the process whereby the interpreter appears to move the declaration of functions, variables, classes, or imports to the top of their scope, prior to execution of the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ah. Now I get it. &lt;/p&gt;

&lt;p&gt;This means that regardless of the their actual placement within the scope, you can use them as if they were declared at the beginning. If you're familiar with the black magic of &lt;code&gt;var&lt;/code&gt;, that is basically hoisting at it's finest. Just add functions, classes and imports into the mix. &lt;/p&gt;

&lt;p&gt;There is a few catches though. Only declarations are hoisted, not assignments. It also doesn't apply to arrow functions, as they are declared with the &lt;code&gt;const&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;I'm a visual person, so I made some ~cool~ graphics using Figma. Check this out:&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%2F9niyi3syl1qlzr8uaxv8.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%2F9niyi3syl1qlzr8uaxv8.png" alt="Function Hoisting" width="800" height="610"&gt;&lt;/a&gt;&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%2Fqs0apxi59s4y457izdkt.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%2Fqs0apxi59s4y457izdkt.png" alt="Initialization" width="800" height="610"&gt;&lt;/a&gt;&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%2Fk1v93ni854nncurt7ppy.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%2Fk1v93ni854nncurt7ppy.png" alt="Variable hoisting" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Alright, I think that's it for me ┗(･ω･;)┛
&lt;/h2&gt;

&lt;p&gt;Here are some key points to take home:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Variable and function declarations are hoisted:&lt;/strong&gt; Hoisting applies only to declarations, not assignments. For instance, the declaration ‘var headcount’ is hoisted, but the value assignment ‘var headcount = 10’ remains at its original location.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; behave differently:&lt;/strong&gt; Unlike &lt;code&gt;var&lt;/code&gt;, which is hoisted to the top of its scope, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables are declared in the place they are written. Attempting to access a let or const variable before its declaration will result in a reference error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hoisting in Strict Mode:&lt;/strong&gt; JavaScript offers a "strict mode" to prevent potential issues caused by hoisting. When running code in strict mode, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables are no longer hoisted, ensuring clear and error-free code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope you learned something with me today! (´◡`)&lt;/p&gt;

&lt;p&gt;Bibi&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>career</category>
    </item>
  </channel>
</rss>
