<?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: jlerocher</title>
    <description>The latest articles on DEV Community by jlerocher (@jlerocher).</description>
    <link>https://dev.to/jlerocher</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%2F1273105%2F0ff58142-85a1-484a-ba51-cabc44b7ffec.png</url>
      <title>DEV Community: jlerocher</title>
      <link>https://dev.to/jlerocher</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jlerocher"/>
    <language>en</language>
    <item>
      <title>[Boost]</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Thu, 19 Dec 2024 04:47:23 +0000</pubDate>
      <link>https://dev.to/jlerocher/-4lob</link>
      <guid>https://dev.to/jlerocher/-4lob</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/geekvergil" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F1197837%2F92642af0-0973-474a-ba3c-2df793a06cf2.jpeg" alt="geekvergil"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/geekvergil/10-game-changing-frontend-tools-you-cant-afford-to-miss-in-2025-2jl6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;10 Game-Changing Frontend Tools You Can't Afford to Miss in 2025🔥&lt;/h2&gt;
      &lt;h3&gt;Vergil ・ Dec 17 '24&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#frontend&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>frontend</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Optimize Your Containerized App with SlimToolkit</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Tue, 07 May 2024 13:38:28 +0000</pubDate>
      <link>https://dev.to/jlerocher/optimize-your-containerized-app-with-slimtoolkit-lon</link>
      <guid>https://dev.to/jlerocher/optimize-your-containerized-app-with-slimtoolkit-lon</guid>
      <description>&lt;p&gt;SlimToolkit is an &lt;strong&gt;open-source project&lt;/strong&gt; that aims to enhance your experience with containers. Whether you're building, customizing, or using containers, SlimToolkit provides a suite of commands to simplify your workflow. Let's explore what makes SlimToolkit so powerful.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can easily try it with one docker command&lt;/p&gt;


&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; /var/run/docker.sock:/var/run/docker.sock dslim/slim
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This commands will run a new container in an interactive mode from the dslim/slim image. The -v option mounts the Docker socket from the host into the container, allowing it to interact with the Docker daemon on the host.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What Is SlimToolkit?&lt;/strong&gt;
&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%2Fqc8tjrbbeoeo1uhefhsp.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%2Fqc8tjrbbeoeo1uhefhsp.png" alt="some slim commands" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SlimToolkit&lt;/strong&gt; or just &lt;strong&gt;Slim&lt;/strong&gt; for friends😉(formerly known as &lt;strong&gt;DockerSlim&lt;/strong&gt;) allows developers to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inspect&lt;/strong&gt;: Understand the contents of your containers using commands like &lt;code&gt;xray&lt;/code&gt; and &lt;code&gt;lint&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimize&lt;/strong&gt;: Minify your container images by up to 30x without changing anything in your original image with &lt;code&gt;build&lt;/code&gt; command.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Debug&lt;/strong&gt;: Troubleshoot issues with the &lt;code&gt;debug&lt;/code&gt; command.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run&lt;/strong&gt;: Execute containers efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Merge&lt;/strong&gt;: Combine multiple images into one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Secure&lt;/strong&gt;: Automatically create Seccomp and AppArmor security profiles to enhance container security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Analyze Vulnerabilities&lt;/strong&gt;: Detect vulnerabilities in your containers using the &lt;code&gt;vulnerability&lt;/code&gt; command.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Use SlimToolkit?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Need to Change Your Workflow&lt;/strong&gt;: SlimToolkit doesn't require you to alter your existing tools or processes. Use the base image and package manager you prefer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Smaller, More Secure Containers&lt;/strong&gt;: SlimToolkit optimizes your containers by analyzing your application's behavior and removing unnecessary components. This reduces the attack surface and enhances security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Advanced Visibility&lt;/strong&gt;: Gain insights into your container images, both original and minified.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Growing Ecosystem Support&lt;/strong&gt;: SlimToolkit now supports additional container tools and runtimes in the cloud-native ecosystem.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Examples&lt;/strong&gt;
&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%2Fr8w6ps8wpps28x5d4c5e.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%2Fr8w6ps8wpps28x5d4c5e.png" alt="Some examples of results of Slim toolkit with NodeJS, Python or Ruby apps" width="800" height="749"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Get full examples on&lt;/strong&gt; &lt;a href="https://github.com/slimtoolkit/examples" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;SlimToolkit empowers developers to create better, smaller, and more secure containers without sacrificing their existing workflows. Explore the project on &lt;a href="https://github.com/slimtoolkit/slim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; or visit the &lt;a href="https://slimtoolkit.org/" rel="noopener noreferrer"&gt;official website&lt;/a&gt; to learn more.&lt;/p&gt;

&lt;p&gt;🐳 Happy containerizing ! 🐳&lt;/p&gt;

</description>
      <category>docker</category>
      <category>opensource</category>
      <category>cicd</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding useReducer Hook in React – An introduction and a Comprehensive Guide for Web Developers</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Sun, 14 Apr 2024 07:00:09 +0000</pubDate>
      <link>https://dev.to/jlerocher/understanding-usereducer-hook-in-react-an-introduction-and-a-comprehensive-guide-for-web-developers-1j2g</link>
      <guid>https://dev.to/jlerocher/understanding-usereducer-hook-in-react-an-introduction-and-a-comprehensive-guide-for-web-developers-1j2g</guid>
      <description>&lt;p&gt;In ReactJS, managing state is a crucial concept that can determine the success or failure of your application. One of the hooks provided by React for managing state is &lt;code&gt;useReducer&lt;/code&gt;. This hook is particularly useful when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. It's also handy when you're dealing with a state that transitions through multiple stages in a predictable manner—think of it as a more robust alternative to &lt;code&gt;useState&lt;/code&gt;. This article serves as both an introduction and a comprehensive guide, meticulously crafted to demystify the &lt;code&gt;useReducer&lt;/code&gt; hook for web developers of all skill levels.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's the problem that &lt;code&gt;useReducer&lt;/code&gt; tries to resolve ?
&lt;/h2&gt;

&lt;p&gt;Imagine you're building a large and intricate React application. You're using useState to manage state of your components, but as your app grows, you start to notice some limitations. Managing complex state transitions becomes cumbersome, and your code starts to feel less efficient. This is where &lt;code&gt;useReducer&lt;/code&gt; comes to the rescue, offering a &lt;strong&gt;&lt;em&gt;more robust solution for handling state in complex scenarios&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's &lt;code&gt;useReducer&lt;/code&gt; ?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useReducer&lt;/code&gt; hook in React is a state management utility that follows the Redux pattern. It is similar to the &lt;code&gt;useState&lt;/code&gt; hook but provides greater flexibility and control over complex state logic. &lt;code&gt;useReducer&lt;/code&gt; operates by dispatching actions in response to specific triggers, which then dictate how the state should be updated. This approach is akin to the way Redux orchestrates state management in React applications. However, &lt;code&gt;useReducer&lt;/code&gt; has the advantage of being integrated within React itself, offering a straightforward and efficient method for handling state transitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Syntax
&lt;/h2&gt;

&lt;p&gt;Let's dive into some code to understand the basic syntax of &lt;code&gt;useReducer&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Import it from react and create an initial state
&lt;/li&gt;
&lt;/ul&gt;

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

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a reducer &lt;strong&gt;outside&lt;/strong&gt; of your component
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reducer&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;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&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;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;decrement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&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;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Unhandled action type: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;blockquote&gt;
&lt;p&gt;A reducer function is a fundamental concept in state management, commonly used in libraries like Redux and with the &lt;code&gt;useReducer&lt;/code&gt; hook in React. It's essentially a pure function that takes in the current state and an action, and returns a new state based on that action. I'm excited to offer an article on the amazing features of JavaScript like reducers and their practical applications. Let me know in the comments if you're interested!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;useReducer&lt;/code&gt; in your functionnal component
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&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;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;Count&lt;/span&gt;&lt;span class="p"&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;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;+&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;decrement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;-&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;useReducer&lt;/code&gt; hook takes two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;reducer&lt;/strong&gt;: the reducer defined outside of the functionnal component&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;initial state&lt;/strong&gt;: Define initial state of your component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;useReducer&lt;/code&gt; hook returns two values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;state&lt;/strong&gt;: the current state value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;dispatch&lt;/strong&gt;: a function used to dispatch actions to update the state&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this example, an initial state and a reducer function are defined. The &lt;code&gt;useReducer&lt;/code&gt; hook is then utilized to manage the state and dispatch actions for its updates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best practices
&lt;/h3&gt;

&lt;p&gt;To use useReducer effectively, it's important to follow some best practices like organize your state logic, separate concerns, optimize performance, and handle side effects properly.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Reducer Function
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Purity is Key&lt;/strong&gt;: The reducer function should be pure. &lt;strong&gt;&lt;em&gt;It solely relies on the current state and dispatched action, avoiding side effects&lt;/em&gt;&lt;/strong&gt; (mutations, external api calls).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Immutability Matters&lt;/strong&gt;: When updating state, create a new state object with the necessary changes. Utilize techniques like the spread operator (...) to maintain immutability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clarity with Switch&lt;/strong&gt;: Employ a &lt;code&gt;switch&lt;/code&gt; statement within the reducer to handle distinct action types. This enhances readability and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default Case&lt;/strong&gt;: In your switch statement, incorporate a default case to &lt;em&gt;handle unexpected actions&lt;/em&gt;, which may involve returning the existing state.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. State Management
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Complex State&lt;/strong&gt;: Use useReducer for &lt;strong&gt;&lt;em&gt;complex state objects with multiple properties requiring coordinated updates&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Separation is Key&lt;/strong&gt;: Leverage useReducer to separate state update logic from the component's rendering logic, improving organization and testability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom Hook Power&lt;/strong&gt;: Consider creating &lt;strong&gt;&lt;em&gt;custom hooks to encapsulate complex reducer logic and dispatch functions&lt;/em&gt;&lt;/strong&gt;, promoting reusability across components.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Action Objects
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Descriptive Types&lt;/strong&gt;: Use descriptive action type names (e.g., INCREMENT_COUNTER, TOGGLE_MODAL) to effectively communicate the intended state update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Payload Power&lt;/strong&gt;: Include a payload property in your action object when additional data is needed for the update (e.g., the amount to increment, new modal content).&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Testing
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Unit Tests&lt;/strong&gt;: Write unit tests for your reducer function to ensure it produces the expected new state for different actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component Tests&lt;/strong&gt;: Test your component's behavior with different state updates dispatched through the reducer.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Additional Tips
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Complex Logic? Split It Up&lt;/strong&gt;: If your reducer logic becomes intricate, explore splitting it into smaller reducer functions for better organization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Context API as an Option&lt;/strong&gt;: Consider using React's Context API for managing global state across deeply nested components, especially with complex application state.&lt;/p&gt;

&lt;p&gt;Fantastic work! I think that at this stage, you have a good understanding of the &lt;code&gt;useReducer&lt;/code&gt; hook in React, a powerful tool for state management in your components. This understanding will streamline your React projects, making them more efficient and robust. Dive into experimenting with useReducer in your applications and seek out additional resources to expand your expertise. Enjoy your coding journey!&lt;/p&gt;

&lt;p&gt;If you found this guide helpful, please consider liking, bookmarking, and leaving a comment below to share your thoughts and experiences with the &lt;code&gt;useReducer&lt;/code&gt; hook. Your feedback helps me improve and create more valuable content for developers like you. Thank you for reading!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Mastering Docker Image Optimization: 6 Strategies for building Lighter, Faster, and Safer images</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Thu, 04 Apr 2024 23:34:48 +0000</pubDate>
      <link>https://dev.to/jlerocher/mastering-docker-image-optimization-6-key-strategies-for-building-lighter-faster-and-safer-images-5bm9</link>
      <guid>https://dev.to/jlerocher/mastering-docker-image-optimization-6-key-strategies-for-building-lighter-faster-and-safer-images-5bm9</guid>
      <description>&lt;p&gt;In containerization's universe, Docker has been the &lt;strong&gt;&lt;em&gt;standard&lt;/em&gt;&lt;/strong&gt; for building images for many years. However, as developed applications become more complex, their size also increases, leading to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extended build times,&lt;/li&gt;
&lt;li&gt;Higher storage costs,&lt;/li&gt;
&lt;li&gt;Slower deployments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To fix these issues, developers or deployment engineers must &lt;strong&gt;&lt;em&gt;master the art of optimizing Docker images&lt;/em&gt;&lt;/strong&gt;. In this article, we'll explore 6 strategies for creating lighter and more efficient Docker images. &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Choose a Minimal Base Image
&lt;/h2&gt;

&lt;p&gt;The fundamental principle of Docker image optimization lies in &lt;u&gt;selecting a lightweight base image&lt;/u&gt;. By starting with a minimal base, such as &lt;a href="https://hub.docker.com/_/alpine" rel="noopener noreferrer"&gt;Alpine Linux&lt;/a&gt;, you can significantly reduce the size of your Docker images. These lightweight base images contain only the essential components necessary to run applications, thereby eliminating any unnecessary overhead.&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%2Ffye6os9bg81wyc81vb1w.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%2Ffye6os9bg81wyc81vb1w.png" alt="Image depicting the lightweight nature of Alpine Linux. It weighs 5 mb" width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Can you believe it? This image is smaller than 6 MB. 😲&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. Reduce Layers
&lt;/h2&gt;

&lt;p&gt;Docker images are built using a layered file system, with each instruction in the Dockerfile creating a new layer. To optimize the size of the image, you should reduce the number of layers by combining related commands using the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator. Additionally, cleaning up unnecessary files and dependencies within the same &lt;code&gt;RUN&lt;/code&gt; instruction will help streamline the image build process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get update &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; curl &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get clean &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In this Dockerfile exemple, we use a single &lt;code&gt;RUN&lt;/code&gt; command to update packages, install Curl, clean the Apt cache, and remove downloaded package lists.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/wagoodman/dive" rel="noopener noreferrer"&gt;Dive&lt;/a&gt; is an open-source tool that allows you to explore the various layers of a Docker image. It shows you the content of each layer and helps you identify voluminous or unnecessary parts.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Use Multi-Stage Builds
&lt;/h2&gt;

&lt;p&gt;This process involves dividing the construction of a Docker image into &lt;a href="https://docs.docker.com/build/building/multi-stage/" rel="noopener noreferrer"&gt;multiple distinct stages&lt;/a&gt;. Each stage creates an image layer, but &lt;u&gt;only the final layers are included in the final image&lt;/u&gt;. This allows for separating build dependencies (such as compilers, build tools, etc.) from runtime dependencies (such as libraries, binaries, etc.). By doing so, one can benefit from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reduced image size:&lt;/strong&gt; By removing unnecessary build artifacts, the final image is lighter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved performance:&lt;/strong&gt; Fewer layers mean faster startup, more efficient resource usage, and better responsiveness.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example: Node.js Web app with npm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;   &lt;span class="c"&gt;# Stage 1: Build&lt;/span&gt;
   FROM node:14 AS build
   WORKDIR /app
   COPY package*.json .
   RUN npm install
   COPY . .
   RUN npm run build

   &lt;span class="c"&gt;# Stage 2: Final Image&lt;/span&gt;
   FROM nginx:alpine
   COPY --from=build /app/dist /usr/share/nginx/html
   EXPOSE 80
   CMD ["nginx", "-g", "daemon off;"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In this example, we first build the Node.js application, then copy the built files into a lighter Nginx image.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This approach not only reduces the image size but also dramatically improves your CI/CD pipeline's performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Copy Only Necessary Files: .dockerignore
&lt;/h2&gt;

&lt;p&gt;When adding files to the Docker image, it's essential to &lt;u&gt;include only what is necessary for the application to function&lt;/u&gt;. This may seem trivial, but it's important to mention. Use the &lt;code&gt;.dockerignore&lt;/code&gt; file for this purpose.&lt;br&gt;
The &lt;code&gt;.dockerignore&lt;/code&gt; file excludes unnecessary files and directories from Docker image creation. By defining explicit rules for what to exclude, you can reduce the size of the build context and speed up the build process. This simple yet effective technique streamlines Docker image creation and management. Therefore, avoid copying unnecessary files, directories, or build artifacts that are not necessary for the app to run. &lt;strong&gt;By minimizing superfluous content, you save space&lt;/strong&gt;, and your application is less vulnerable because less code means fewer security vulnerabilities.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example, among the directories of a nodeJS project that do not belong in a docker image, we have:&lt;br&gt;
      &lt;code&gt;node_modules&lt;/code&gt; &lt;code&gt;.npm&lt;/code&gt; &lt;code&gt;npm-cache&lt;/code&gt; &lt;code&gt;logs&lt;/code&gt; &lt;code&gt;tmp&lt;/code&gt;&lt;br&gt;
The above list is not exhaustive, the only question to ask yourself at this stage is &lt;u&gt;&lt;strong&gt;Is this file (or folder) really essential for my application?&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  5. Minimize Cache Busting
&lt;/h2&gt;

&lt;p&gt;Cache busting can have a significant impact on Docker image build times and efficiency. To optimize caching, you should place instructions that frequently change towards the end of the Dockerfile, ensuring that cached layers are reused as much as possible. This approach reduces build times and improves overall productivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Inspect Image Size
&lt;/h2&gt;

&lt;p&gt;Regularly monitoring Docker image sizes is essential for identifying areas to optimize and tracking improvements over time. You should use Docker commands such as &lt;a href="https://docs.docker.com/reference/cli/docker/image/ls/" rel="noopener noreferrer"&gt;&lt;code&gt;docker image ls&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://docs.docker.com/reference/cli/docker/inspect/" rel="noopener noreferrer"&gt;&lt;code&gt;docker inspect&lt;/code&gt;&lt;/a&gt; to inspect image sizes and analyze their composition. By remaining vigilant and proactive, developers can continuously optimize Docker images for maximum efficiency.&lt;/p&gt;

&lt;p&gt;Optimizing Docker images is a crucial aspect of modern application development, allowing developers to create lightweight, performant, and secure containers. By following key strategies such as choosing minimal base images, reducing layers, and using multi-stage builds, developers can significantly improve build times, reduce storage costs, and streamline deployment processes. &lt;strong&gt;&lt;em&gt;It is important to remember that optimization is an ongoing process&lt;/em&gt;&lt;/strong&gt;; therefore, it's essential to regularly revisit your Docker images to identify any new optimization opportunities as your application evolves.&lt;/p&gt;

&lt;p&gt;If you find these recommendations helpful, please list in the comments both your initial Docker image size and the final, optimized image size. &lt;/p&gt;

</description>
      <category>devops</category>
      <category>docker</category>
      <category>performance</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Optimisation des images Docker: 6 Stratégies clés pour des images plus légeres et plus performantes</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Thu, 04 Apr 2024 23:02:29 +0000</pubDate>
      <link>https://dev.to/jlerocher/optimisation-des-images-docker-6-strategies-cles-pour-des-images-plus-legeres-et-plus-performantes-gfn</link>
      <guid>https://dev.to/jlerocher/optimisation-des-images-docker-6-strategies-cles-pour-des-images-plus-legeres-et-plus-performantes-gfn</guid>
      <description>&lt;p&gt;Dans l'univers de la conteneurisation, depuis de nombreuses années, Docker s'est imposé comme &lt;strong&gt;&lt;em&gt;le standard&lt;/em&gt;&lt;/strong&gt; pour la construction d'images destinées au déploiements automatisés. Cependant, à mesure que les applications développées deviennent plus complexes, leur taille augmente également, ce qui entraine &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;une durée de construction prolongée, &lt;/li&gt;
&lt;li&gt;des coûts de stockage plus élevés&lt;/li&gt;
&lt;li&gt;des déploiements plus lents. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Afin de remédier à ces problèmes, les développeurs ou les ingénieurs chargés du déploiement doivent &lt;em&gt;&lt;strong&gt;maîtriser l'art d'optimiser les images Docker&lt;/strong&gt;&lt;/em&gt;. Dans cet article, nous explorerons 6 stratégies clés pour créer des images Docker plus légères et plus performantes.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Choisissez une image de base minimale
&lt;/h2&gt;

&lt;p&gt;Le principe fondamental de l'optimisation des images Docker réside dans le fait de &lt;u&gt;sélectionner une image de base légère&lt;/u&gt;. En commençant par une base minimale, telle qu'&lt;a href="https://hub.docker.com/_/alpine" rel="noopener noreferrer"&gt;Alpine Linux&lt;/a&gt; par exemple, vous pouvez réduire considérablement la taille de vos images Docker. Ces images de base légères ne contiennent que les composants essentiels nécessaires pour exécuter des applications, purgeant ainsi toute surcharge inutile.&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%2Ffye6os9bg81wyc81vb1w.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%2Ffye6os9bg81wyc81vb1w.png" alt="Image décrivant la légèreté d'alpine linux. Elle pèse 5 mb " width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rendez vous compte, il s'agit d'une image de 5 mb 😲&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. Réduisez les couches
&lt;/h2&gt;

&lt;p&gt;Les images Docker sont construites à l'aide d'un système de fichiers en couches, chaque instruction dans le Dockerfile créant une nouvelle couche. Pour optimiser la taille de l'image, vous devriez réduire le nombre de couches en combinant les commandes connexes à l'aide de l'opérateur &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;. De plus, nettoyer les fichiers et dépendances inutiles dans la même instruction &lt;code&gt;RUN&lt;/code&gt; aidera à rationaliser le processus de construction de l'image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get update &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; curl &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get clean &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Dans cet extrait de Dockerfile, nous utilisons une seule commande &lt;code&gt;RUN&lt;/code&gt; pour mettre à jour les paquets, installer Curl, nettoyer le cache Apt, et supprimer les listes de paquets téléchargées.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/wagoodman/dive" rel="noopener noreferrer"&gt;Dive&lt;/a&gt; est un outil open source qui permet d’explorer les différentes couches d’une image Docker. Il vous montre le contenu de chaque couche et vous aide à identifier les parties volumineuses ou inutiles.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Utilisez des constructions multi-étapes
&lt;/h2&gt;

&lt;p&gt;Ce processus consiste à diviser la construction d'une image Docker en &lt;a href="https://docs.docker.com/build/building/multi-stage/" rel="noopener noreferrer"&gt;plusieurs étapes distinctes&lt;/a&gt;. Chaque étape crée une couche d'image, mais &lt;u&gt;seules les couches finales sont incluses dans l'image finale&lt;/u&gt;. Cela permet de séparer les dépendances de construction (comme les compilateurs, les outils de génération, etc.) des dépendances d'exécution (comme les bibliothèques, les fichiers binaires, etc.). En procédant de la sorte, on peut bénéficier d'une:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Réduction de la taille de l'image :&lt;/strong&gt; En supprimant les artefacts de construction inutiles, l'image finale est plus légère.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Amélioration des performances :&lt;/strong&gt; Moins de couches signifient un démarrage plus rapide, une utilisation plus efficace et responsable des ressources.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Exemple: Web app Node.js avec npm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;   &lt;span class="c"&gt;# Étape 1: Construction&lt;/span&gt;
   FROM node:14 AS build
   WORKDIR /app
   COPY package*.json .
   RUN npm install
   COPY . .
   RUN npm run build

   &lt;span class="c"&gt;# Étape 2: Image finale&lt;/span&gt;
   FROM nginx:alpine
   COPY --from=build /app/dist /usr/share/nginx/html
   EXPOSE 80
   CMD ["nginx", "-g", "daemon off;"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Dans cet exemple, nous construisons d'abord l'application Node.js, puis nous copions les fichiers construits dans une image Nginx plus légère.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cette approche permet non seulement de réduire la taille de l'image, mais aussi d'améliorer drastiquement les performances de votre pipeline CI/CD.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Copiez uniquement les fichiers indispensables: dockerignore
&lt;/h2&gt;

&lt;p&gt;Lors de l'ajout de fichiers à l'image Docker, il est essentiel d'&lt;u&gt;inclure uniquement ce qui est nécessaire pour que l'application fonctionne&lt;/u&gt;. Cela peut sembler bête, mais il est important de le dire. Pour cela utilisez le fichier &lt;code&gt;.dockerignore&lt;/code&gt;.&lt;br&gt;
Le &lt;code&gt;.dockerignore&lt;/code&gt; permet d'exclure des fichiers et répertoires inutiles de la création d'images Docker. En définissant des règles explicites pour ce qu'il faut exclure, on peut ainsi réduire la taille du contexte et accélérer le processus de construction. Cette technique simple mais efficace permet de rationaliser la création et la gestion des images Docker. Evitez donc de copier des fichiers, répertoires ou artefacts de construction inutiles qui ne sont pas nécessaires à l'exécution de l'app. &lt;strong&gt;En minimisant le contenu superflu, on économise de l'espace&lt;/strong&gt; et notre application est moins vulnérable car moins de code = moins de failles de sécurités.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Par exemple, parmi les dossiers d'un projet nodeJS qui n'ont pas leur place dans une image docker, on a:&lt;br&gt;
      &lt;code&gt;node_modules&lt;/code&gt; &lt;code&gt;.npm&lt;/code&gt; &lt;code&gt;npm-cache&lt;/code&gt; &lt;code&gt;logs&lt;/code&gt; &lt;code&gt;tmp&lt;/code&gt;&lt;br&gt;
La liste ci-dessus n'est pas exhaustive, la seule question qu'il faut se poser durant cette étape c'est &lt;u&gt;&lt;strong&gt;Ce fichier(ou ce dossier) est il réellement indispensable pour mon application ?&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  5. Minimisez la perturbation du cache
&lt;/h2&gt;

&lt;p&gt;La perturbation du cache peut avoir un impact significatif sur les temps de construction des images Docker et leur efficacité. Pour optimiser la mise en cache, les développeurs devraient placer les instructions qui changent fréquemment vers la fin du Dockerfile, en veillant à ce que les couches mises en cache soient réutilisées autant que possible. Cette approche réduit les temps de construction et améliore la productivité globale.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Inspectez la taille de l'image
&lt;/h2&gt;

&lt;p&gt;Surveiller régulièrement les tailles des images Docker est essentiel pour identifier les domaines à optimiser et suivre les améliorations au fil du temps. Les développeurs devraient utiliser des commandes Docker telles que &lt;a href="https://docs.docker.com/reference/cli/docker/image/ls/" rel="noopener noreferrer"&gt;&lt;code&gt;docker image ls&lt;/code&gt;&lt;/a&gt; et &lt;a href="https://docs.docker.com/reference/cli/docker/inspect/" rel="noopener noreferrer"&gt;&lt;code&gt;docker inspect&lt;/code&gt;&lt;/a&gt; pour inspecter les tailles des images et analyser leur composition. En restant vigilant et proactif, les développeurs peuvent continuellement optimiser les images Docker pour une efficacité maximale.&lt;/p&gt;

&lt;p&gt;L'optimisation des images Docker est un aspect crucial du développement d'applications modernes, permettant aux développeurs de créer des conteneurs légers, performants et sécurisés. En suivant des stratégies clés telles que le choix d'images de base minimales, la réduction des couches et l'utilisation de constructions multi-étapes, les développeurs peuvent améliorer considérablement les temps de construction, réduire les coûts de stockage et rationaliser les processus de déploiement.  &lt;strong&gt;&lt;em&gt;Il est important de se rappeler que l'optimisation est un processus en continu&lt;/em&gt;&lt;/strong&gt;; il convient donc de réexaminer régulièrement vos images Docker pour identifier toute nouvelle possibilité d'optimisation à mesure que votre application se développe.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>cloud</category>
      <category>performance</category>
    </item>
    <item>
      <title>Understanding useContext Hooks in React – An introduction and a Comprehensive Guide for Web Developers</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Thu, 29 Feb 2024 10:07:05 +0000</pubDate>
      <link>https://dev.to/jlerocher/understanding-usecontext-hooks-in-react-an-introduction-and-a-comprehensive-guide-for-web-developers-1fhp</link>
      <guid>https://dev.to/jlerocher/understanding-usecontext-hooks-in-react-an-introduction-and-a-comprehensive-guide-for-web-developers-1fhp</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;React is a popular JavaScript library used for building user interfaces, particularly single-page applications. One of its core features is the concept of context which allows you to share values between components without having to explicitly pass a prop through every level of the tree. In this article, we'll explore what &lt;code&gt;useContext&lt;/code&gt; is and how it can be useful in React applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is useContext?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useContext&lt;/code&gt; hook is a built-in feature provided by React that allows you to access context directly from your functional components without having to wrap them with the Context Consumer Component. It's used when you need to read the context of a particular component in your app, and it helps you avoid the props drilling problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Uses and Implementations&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s look at some practical examples:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;MyContext&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;./MyContext&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ChildComponent&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="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Now you can access the value of context here.&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, &lt;code&gt;useContext&lt;/code&gt; is used to get the current state of a Context object and it returns the current context value for this hook. If there's no provider above us in the tree (the closest one), then it throws an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Advanced cases&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Theme Switching&lt;/li&gt;
&lt;li&gt;Authentication Management&lt;/li&gt;
&lt;li&gt;Internationalization (i18n)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Theme Switching
&lt;/h3&gt;

&lt;p&gt;One of the most visually impactful features you can implement with useContext is theme switching. By encapsulating your theme state and toggling logic within a context provider, components throughout your application can effortlessly adapt their appearance based on the selected theme, all without the need for prop drilling.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ThemeContext.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ThemeContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ThemeProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTheme&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;light&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;toggleTheme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTheme&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevTheme&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;prevTheme&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&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;dark&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;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&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="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggleTheme&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;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useTheme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, any component within your application can effortlessly subscribe to the current theme and toggle functionality using the useTheme() hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication Management
&lt;/h3&gt;

&lt;p&gt;Securing access to your application's features often involves managing user authentication state. By centralizing this state within a context provider, components can seamlessly interact with authentication-related functions without the need for prop passing.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// AuthContext.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AuthContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AuthProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&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="kc"&gt;null&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;login&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userData&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;// Logic for authenticating user&lt;/span&gt;
    &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userData&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="nx"&gt;logout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Logic for logging out user&lt;/span&gt;
    &lt;span class="nf"&gt;setUser&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="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="nc"&gt;AuthContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;logout&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;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;AuthContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useAuth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AuthContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, components can easily access the user object and authentication functions using the useAuth() hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  Internationalization (i18n)
&lt;/h3&gt;

&lt;p&gt;For applications catering to a global audience, internationalization is paramount. By leveraging useContext, you can manage the current language state and provide translation functions throughout your component tree.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// I18nContext.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;I18nContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;I18nProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;language&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLanguage&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;en&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;translate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;// Logic to retrieve translated text based on language and key&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="nc"&gt;I18nContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&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="nx"&gt;language&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLanguage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;translate&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;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;I18nContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useI18n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;I18nContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Components can use the useI18n() hook to access the current language and translation function.&lt;/p&gt;

&lt;p&gt;While useContext excels at basic state management, its true power lies in its ability to facilitate global state management scenarios for small apps with ease. By harnessing its capabilities for theme switching, authentication management, internationalization, and beyond, you can craft React applications that are both robust and flexible, captivating users with their seamless user experience. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Best Practices&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here are some tips when using &lt;code&gt;useContext&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding Deep Component Tree&lt;/strong&gt;: Context should be used sparingly to minimize unnecessary re-renders and deep component trees. It is best used for global data that multiple components need access to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Selective Consumption&lt;/strong&gt;: If you have multiple context providers, it's possible that your child components will receive different versions of the same value. To control this behavior, wrap your components with a Consumer and provide only what they actually use. This can be done using &lt;code&gt;React.useContext&lt;/code&gt; in a function component to get the context from an ancestor provider.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding Context on Primitives&lt;/strong&gt;: It is a common misconception that React's Context API will work with primitives, but it doesn’t. Context only works with objects (and arrays). This is because React compares object references to determine when state has changed and should cause re-renders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding Unnecessary Re-Renders&lt;/strong&gt;: The useContext hook subscribes to context changes by default, this can lead to unnecessary renders if used inside a render prop or higher order component. To mitigate this, you can pass a function to the optional second argument of &lt;code&gt;useContext&lt;/code&gt; that returns a value for comparison when deciding whether to re-render your components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding Nested Contexts&lt;/strong&gt;: React’s context API is not designed to be used with deeply nested contexts and it may lead to performance issues due to frequent re-renders caused by the changes in context values at various levels of depth. &lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The &lt;code&gt;useContext&lt;/code&gt; hook provides a way to pass data through the component tree without having to pass props down manually at every level, making your code more concise and easier to understand. This makes it an essential tool for large React applications where context is used extensively throughout different parts of the app. Happy coding Reacters!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>hook</category>
    </item>
    <item>
      <title>Understanding useEffect Hooks in React – An introduction and Comprehensive Guide for Web Developers</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Thu, 29 Feb 2024 08:56:20 +0000</pubDate>
      <link>https://dev.to/jlerocher/understanding-useeffect-hooks-in-react-an-introduction-and-comprehensive-guide-for-web-developers-4b26</link>
      <guid>https://dev.to/jlerocher/understanding-useeffect-hooks-in-react-an-introduction-and-comprehensive-guide-for-web-developers-4b26</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;React is a powerful JavaScript library used for building user interfaces, particularly single-page applications. One of its core features is the concept of state and lifecycle methods which are now managed by using Hooks. Among these hooks, &lt;code&gt;useEffect&lt;/code&gt; has gained popularity due to its ability to perform side effects in function components. This article aims to demystify what &lt;code&gt;useEffect&lt;/code&gt; is, how to use it, and some best practices when working with &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is useEffect?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook is a built-in feature provided by React for performing side effects in functional components. Side effects could be data fetching, subscriptions or manually changing the DOM. It accepts two arguments:&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="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;didUpdate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&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="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;// your code here&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;didUpdate&lt;/code&gt; is a function that performs the side effect and optionally returns a cleanup function. The array of dependencies (second argument) specifies when the effect should be run. If you pass an empty array ([]), it means the effect will only run once on mount and unmount. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Uses and Implementations&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s look at some practical examples:&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="c1"&gt;// Fetching data from API&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;fetch&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/data&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="c1"&gt;// Empty dependency array means this effect will only run once on mount&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Subscribing to an event or API&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;eventHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Event occurred&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;eventHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Cleanup function: remove the event listener when the component unmounts&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;eventHandler&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="c1"&gt;// Empty dependency array means this effect will only run once on mount&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Best Practices&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here are some tips when using &lt;code&gt;useEffect&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding Memory Leaks&lt;/strong&gt;: If you're subscribing to an external service or setting up a timer, it's important that you clean up after yourself by removing the event listener or clearing the interval when your component unmounts, so as not to cause memory leaks in your app. This is what the return function of &lt;code&gt;useEffect&lt;/code&gt; does for us.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conditional Effect Execution&lt;/strong&gt;: If there are dependencies provided to &lt;code&gt;useEffect&lt;/code&gt;, it will only execute after the initial render and on any updates where those values change. It's a way to control when side effects occur based on props or state changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using Cleanup Function&lt;/strong&gt;: Every time your component renders, React runs your effect function (including useEffect). This is why you often see cleanup functions in &lt;code&gt;useEffect&lt;/code&gt; — they allow for resource management and prevent memory leaks by cleaning up after ourselves.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding Effects on Mount&lt;/strong&gt;: If you want to run an effect only on updates, not on initial mount, you can pass a dependency array with one or more values as the second argument of &lt;code&gt;useEffect&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nested useEffect and Cleanup&lt;/strong&gt;: You may have multiple effects in your component. Each of them will run sequentially with the cleanup from the previous effect running before it runs. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Splitting Effects&lt;/strong&gt;: If you have a large effect that does several things, or if these things can be split apart for performance gains, consider breaking them out into their own useEffect hooks.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; Hook is an essential part of the React ecosystem as it enables side effects in functional components. Understanding its usage and best practices will allow you to handle more complex scenarios effectively with React. Happy coding Reacters!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>hook</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding useState Hook in React – An introduction and a Comprehensive Guide for Web Developers</title>
      <dc:creator>jlerocher</dc:creator>
      <pubDate>Thu, 29 Feb 2024 08:34:56 +0000</pubDate>
      <link>https://dev.to/jlerocher/understanding-usestate-hook-in-react-an-introduction-and-a-comprehensive-guide-for-web-developers-57o7</link>
      <guid>https://dev.to/jlerocher/understanding-usestate-hook-in-react-an-introduction-and-a-comprehensive-guide-for-web-developers-57o7</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the world of web development, React is often cited as one of the most popular JavaScript libraries for building user interfaces. As a powerful library, it offers several ways to manage state and perform side effects but its core feature remains managing component state – that's where &lt;code&gt;useState&lt;/code&gt; hook comes in. This article aims to demystify what &lt;code&gt;useState&lt;/code&gt; is, how to use it, and some of the best practices when working with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is useState?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is a built-in Hook provided by React for adding local state to functional components. The basic syntax of &lt;code&gt;useState&lt;/code&gt; looks 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;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&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="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;useState&lt;/code&gt; returns two values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the current state and&lt;/li&gt;
&lt;li&gt;a function that updates it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why we’re using array destructuring to give names to these items. The initial value of the state can be either an object or a function returning an object. If you pass a function, it will only be executed on the first render after which it'll return the same value every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Uses and Implementations&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let's look at a practical example:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Declare a new state variable, which we'll call "count"&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="mi"&gt;0&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;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;You clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Click me
      &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;In this example, we've created a counter button. On each click of the button, &lt;code&gt;setCount&lt;/code&gt; updates the state to be one more than its previous value and causes the component to re-render with the new count.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Best Practices&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here are some tips when using &lt;code&gt;useState&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding direct mutation&lt;/strong&gt;: The main feature of useState hook is that it allows you to add local state without having to make your components class-based by avoiding direct mutations on the component's state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conditional Updates&lt;/strong&gt;: You can conditionally update &lt;code&gt;useState&lt;/code&gt; based on conditions in your event handlers or elsewhere in your code using logical &amp;amp;&amp;amp; operator. This makes it easier to manage what state gets updated when, and why.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preventing Unnecessary Rendering&lt;/strong&gt;: If you are passing a function (like setCount) to &lt;code&gt;useState&lt;/code&gt;, React will always return the same function reference. So for complex objects or functions, ensure they don't cause unnecessary re-renders by using &lt;code&gt;React.useMemo()&lt;/code&gt; or `React.useCallback().&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ordering State Updates&lt;/strong&gt;: If you need to perform multiple state updates based on the current state (known as "batch updates" in React), use a function with setState, which gives you the previous state value. For example:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;setCount(prevCount =&amp;gt; prevCount + 1);&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; Hook is fundamental to managing component state in functional components in React. By understanding its usage and best practices, developers can efficiently build complex user interfaces with React. Happy coding!&lt;/p&gt;

&lt;p&gt;Remember that the power of this hook lies not only in adding local state but also in enabling you to abstract out repetitive logic into custom Hooks for cleaner code organization. You're now equipped to handle more complex scenarios with &lt;code&gt;useState&lt;/code&gt; and beyond!&lt;/p&gt;

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