<?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: Bradley Dirheimer</title>
    <description>The latest articles on DEV Community by Bradley Dirheimer (@dirheimerb).</description>
    <link>https://dev.to/dirheimerb</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%2F536575%2Fe0f48499-f8ce-422f-8216-9529512afa70.png</url>
      <title>DEV Community: Bradley Dirheimer</title>
      <link>https://dev.to/dirheimerb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dirheimerb"/>
    <language>en</language>
    <item>
      <title>Artificial Intelligence and Machine Learning in Web Development</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Mon, 22 Apr 2024 03:56:51 +0000</pubDate>
      <link>https://dev.to/dirheimerb/artificial-intelligence-and-machine-learning-in-web-development-3n8i</link>
      <guid>https://dev.to/dirheimerb/artificial-intelligence-and-machine-learning-in-web-development-3n8i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Artificial Intelligence (AI) and Machine Learning (ML) are reshaping the landscape of web development, transforming the way users interact with web applications. These technologies enable sophisticated features such as personalized user experiences, automated customer support via chatbots, and advanced image recognition capabilities. This article explores the integration of AI and ML in web development, focusing on popular tools like TensorFlow.js and their practical applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Personalization through AI and ML
&lt;/h2&gt;

&lt;p&gt;One of the most impactful applications of AI and ML in web development is personalization. By leveraging user data and behavior patterns, ML algorithms can customize website content, recommendations, and advertisements to individual user preferences. TensorFlow.js, an open-source library that brings ML capabilities to the web, allows developers to train and deploy models directly in the browser. This enables real-time personalization, such as dynamically adjusting content or recommendations based on user interactions without the need for backend computations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automated Customer Support: Chatbots
&lt;/h2&gt;

&lt;p&gt;AI-powered chatbots have revolutionized customer service in web environments. These chatbots use natural language processing (NLP) and machine learning to understand and respond to customer inquiries effectively. TensorFlow.js supports the development of such models, enabling chatbots to learn from interactions and improve over time. This not only enhances user experience but also reduces the workload on human customer service representatives by handling routine inquiries and filtering out more complex issues that require human intervention.&lt;/p&gt;

&lt;h2&gt;
  
  
  Image Recognition Technologies
&lt;/h2&gt;

&lt;p&gt;Image recognition is another area where AI and ML have made significant inroads. Web applications can now use ML to analyze images for various purposes, such as facial recognition, object detection, and automated tagging. TensorFlow.js provides tools that enable developers to incorporate image recognition functionalities directly into web applications, facilitating features like photo-based search, user verification, and interactive media.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools and Frameworks
&lt;/h2&gt;

&lt;p&gt;In addition to TensorFlow.js, other tools and frameworks support AI and ML in web development. Libraries such as Brain.js provide simpler neural network implementations, suitable for smaller-scale applications or those with less complex data sets. Moreover, APIs like Microsoft Azure Cognitive Services or Google Cloud AI offer ready-to-use models that can be easily integrated into web applications, speeding up development and deployment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The integration of AI and ML into web development is not just a trend but a transformative shift that enhances the interactivity and intelligence of web applications. With tools like TensorFlow.js, developers are equipped to build more personalized, responsive, and intelligent web applications. As technology advances, the scope of AI and ML in web development will undoubtedly expand, paving the way for more innovative and user-focused solutions.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Exploring Bitwise Operators: From JavaScript to TypeScript Enhancements</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Mon, 22 Apr 2024 03:38:42 +0000</pubDate>
      <link>https://dev.to/dirheimerb/exploring-bitwise-operators-from-javascript-to-typescript-enhancements-5ab8</link>
      <guid>https://dev.to/dirheimerb/exploring-bitwise-operators-from-javascript-to-typescript-enhancements-5ab8</guid>
      <description>&lt;p&gt;Bitwise operators are a foundational aspect of programming, enabling direct manipulation of individual bits within an integer's binary representation. In web development, JavaScript provides this low-level access, which is crucial for performance-critical applications. However, when TypeScript is added to the mix, it enhances these operations with strong typing and advanced features that lead to safer and more maintainable code. This blog post will delve into the workings of bitwise operators in JavaScript, compare their usage with TypeScript, and discuss the additional benefits TypeScript offers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bitwise Operators in JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript supports several bitwise operators, which operate directly on the 32-bit integer representations of numbers. These operators are essential for tasks that involve flags, masks, or where performance optimization is crucial, such as in graphics programming or real-time processing.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Operators
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AND (&lt;code&gt;&amp;amp;&lt;/code&gt;)&lt;/strong&gt;: Sets each bit to 1 if both bits are 1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OR (&lt;code&gt;|&lt;/code&gt;)&lt;/strong&gt;: Sets each bit to 1 if one of the bits is 1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;XOR (&lt;code&gt;^&lt;/code&gt;)&lt;/strong&gt;: Sets each bit to 1 if only one of the bits is 1 and the other is 0.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NOT (&lt;code&gt;~&lt;/code&gt;)&lt;/strong&gt;: Inverts all the bits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Left Shift (&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;)&lt;/strong&gt;: Shifts all bits to the left, filling 0 on the right.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sign-Propagating Right Shift (&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;)&lt;/strong&gt;: Shifts bits to the right, preserving the sign.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero-Fill Right Shift (&lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;)&lt;/strong&gt;: Shifts bits to the right, filling 0 on the left.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These operators are powerful but come with a caveat in JavaScript due to the language's type system and automatic type coercion, which can lead to unintended results if not handled carefully.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enhancing Bitwise Operations with TypeScript
&lt;/h2&gt;

&lt;p&gt;TypeScript builds on JavaScript, adding static types and enums, which enhance the clarity and reliability of bitwise operations. Here’s how TypeScript improves upon JavaScript’s capabilities:&lt;/p&gt;

&lt;h3&gt;
  
  
  Static Typing
&lt;/h3&gt;

&lt;p&gt;TypeScript’s static type system allows for defining the types of variables explicitly. This feature can prevent many common bugs in JavaScript, such as those caused by unexpected type coercion. In the context of bitwise operations, this means you can ensure that only numbers are used in places where bitwise operations make sense, reducing runtime errors and improving code quality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enums for Bitwise Flags
&lt;/h3&gt;

&lt;p&gt;TypeScript's enum is a way of giving more friendly names to sets of numeric values. This is especially useful in the case of flags or masks used with bitwise operators. TypeScript enums can be more expressive and safer compared to plain numeric constants in JavaScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example: FileAccess Permissions
&lt;/h4&gt;

&lt;p&gt;Consider an example using TypeScript enums to define file access permissions, illustrating the application of bitwise operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;FileAccess&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;Read&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="nx"&gt;Write&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// 4&lt;/span&gt;
    &lt;span class="nx"&gt;ReadWrite&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Read&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Write&lt;/span&gt;  &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes the code more readable and maintainable, especially when combined with TypeScript's static checking.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enhanced Safety and Maintainability
&lt;/h3&gt;

&lt;p&gt;TypeScript's compiler performs checks based on the types and will issue compile-time errors if bitwise operations are used incorrectly. This leads to safer and more robust code, particularly in large codebases or in applications where reliability is critical.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Applications
&lt;/h2&gt;

&lt;p&gt;Bitwise operators can dramatically increase the efficiency of the code where operations on individual bits are required. Some practical applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Settings and Configuration&lt;/strong&gt;: Use flags to enable or disable features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Permission Systems&lt;/strong&gt;: Efficiently manage user permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Encoding and Decoding&lt;/strong&gt;: Manipulate data at the bit level for custom communication protocols.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;While JavaScript provides the necessary tools to perform low-level operations via bitwise operators, TypeScript enhances these operations with its robust type system and enums. For developers aiming to write more maintainable, error-resistant code, TypeScript offers significant advantages. The combination of JavaScript's flexibility with TypeScript's enhancements allows developers to leverage the best of both worlds, making it an excellent choice for projects that demand both performance and reliability.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>coding</category>
    </item>
    <item>
      <title>Implementing Custom Dark Mode with Tailwind CSS: A Complete Guide</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Mon, 22 Apr 2024 03:26:55 +0000</pubDate>
      <link>https://dev.to/dirheimerb/implementing-custom-dark-mode-with-tailwind-css-a-complete-guide-3n0h</link>
      <guid>https://dev.to/dirheimerb/implementing-custom-dark-mode-with-tailwind-css-a-complete-guide-3n0h</guid>
      <description>&lt;p&gt;As the digital landscape evolves, the adoption of dark mode on websites has become increasingly important, offering users a more personalized and eye-friendly browsing experience. Tailwind CSS, a utility-first CSS framework, has continually updated its features to allow seamless integration of dark mode into web projects. One significant enhancement is Tailwind CSS's transition from the 'class' strategy to the more flexible 'selector' strategy, which provides greater control over user experience. This blog post explores this transition, illustrating how to manually implement and manage dark mode using the latest capabilities of Tailwind CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Selector Strategy
&lt;/h2&gt;

&lt;p&gt;With the release of version 3.4.1, Tailwind CSS introduced the 'selector' strategy, replacing the older 'class' strategy. This new approach allows developers to activate dark mode by adding a specific class to a higher element in the HTML structure, such as the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; tag, thus providing direct control over the dark mode, independent of the user's system settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Tailwind CSS for Selector Strategy
&lt;/h2&gt;

&lt;p&gt;To use the selector strategy in your Tailwind CSS setup, you must update your &lt;code&gt;tailwind.config.js&lt;/code&gt; file as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;darkMode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;selector&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Enable selector strategy for dark mode&lt;/span&gt;
  &lt;span class="c1"&gt;// Additional configuration...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration ensures that dark mode-specific classes such as &lt;code&gt;dark:bg-black&lt;/code&gt; will activate whenever the &lt;code&gt;dark&lt;/code&gt; class is present on the parent or a higher-level HTML element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Usage
&lt;/h2&gt;

&lt;p&gt;Implementing the selector strategy in your HTML is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Dark mode not enabled --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"bg-white dark:bg-black"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Content without dark mode --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- Dark mode enabled --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"dark"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"bg-white dark:bg-black"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Content with dark mode --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this setup, the background of the &lt;code&gt;div&lt;/code&gt; will be white by default and switch to black when the &lt;code&gt;dark&lt;/code&gt; class is added to the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamically Toggling Dark Mode with JavaScript
&lt;/h2&gt;

&lt;p&gt;To allow users to control the appearance of your application, you can use JavaScript to dynamically toggle dark mode based on their preferences. This can be effectively managed using &lt;code&gt;localStorage&lt;/code&gt; to remember user settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Check and apply the user's stored preference or the system preference on page load&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&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="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&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;theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matchMedia&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;(prefers-color-scheme: dark)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;matches&lt;/span&gt;&lt;span class="p"&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="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// User actions to explicitly choose themes&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toggleTheme&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDark&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isDark&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="c1"&gt;// Save preference&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toggle&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="nx"&gt;isDark&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function checks the user's preference stored in &lt;code&gt;localStorage&lt;/code&gt; or defaults to the system preference, adding or removing the &lt;code&gt;dark&lt;/code&gt; class on the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Customizing the Selector
&lt;/h2&gt;

&lt;p&gt;For projects requiring a unique class name for toggling dark mode or integrating with other frameworks, Tailwind CSS allows for customization of the dark mode selector:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;darkMode&lt;/span&gt;&lt;span class="p"&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;selector&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;[data-mode="dark"]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration ensures that your custom selector is wrapped in the &lt;code&gt;:where()&lt;/code&gt; pseudo-class to maintain consistency in specificity.&lt;/p&gt;

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

&lt;p&gt;Tailwind CSS’s selector strategy for dark mode provides a flexible and powerful approach to implementing custom dark mode toggling in web projects. It equips developers with the tools to create dynamic, user-centric web applications that cater to individual preferences, enhancing the overall user experience. Whether you’re developing a new website or updating an existing one, integrating Tailwind CSS’s dark mode features can significantly enhance your project's accessibility and aesthetic appeal.&lt;/p&gt;

</description>
      <category>tailwindcss</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>css</category>
    </item>
    <item>
      <title>Mastering Next.js Navigation Hooks: An In-Depth Review</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Sat, 05 Aug 2023 10:05:45 +0000</pubDate>
      <link>https://dev.to/dirheimerb/mastering-nextjs-navigation-hooks-an-in-depth-review-3k61</link>
      <guid>https://dev.to/dirheimerb/mastering-nextjs-navigation-hooks-an-in-depth-review-3k61</guid>
      <description>&lt;p&gt;Next.js, a versatile framework for React applications, has a suite of Client Component hooks that significantly enhance web app navigation. This article takes an in-depth look at four of these hooks: &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt;, &lt;code&gt;usePathname&lt;/code&gt;, &lt;code&gt;useParams&lt;/code&gt;, and &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt;. Understanding these hooks' functionalities and nuances can profoundly enrich the navigational dynamics of your Next.js apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  useSelectedLayoutSegment
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; is a Client Component hook that gives you access to the active route segment one level below the Layout from which it is invoked. This function is valuable for creating navigation UI components, like tabs within a parent layout, which change their style based on the active child segment. &lt;/p&gt;

&lt;p&gt;Notably, &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; does not accept any parameters and returns a string of the active segment or &lt;code&gt;null&lt;/code&gt; if one doesn't exist. It's typically utilized within a Client Component that is then imported into a Layout, providing an efficient way to handle specific navigational changes at the granular segment level.&lt;/p&gt;

&lt;h2&gt;
  
  
  usePathname
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;usePathname&lt;/code&gt; hook offers a straightforward way to read the current URL's pathname, allowing for dynamic adjustments based on the URL. It can be effectively used within Client Components to conditionally render components or adjust styles based on the current pathname. &lt;/p&gt;

&lt;p&gt;Crucially, the &lt;code&gt;usePathname&lt;/code&gt; hook returns &lt;code&gt;null&lt;/code&gt; in instances when a fallback route is being rendered or when a pages directory page has been automatically statically optimized by Next.js and the router isn't ready. This capability demonstrates the hook's flexibility and usefulness in various scenarios, including handling route changes and responding to dynamic path changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  useParams
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useParams&lt;/code&gt; shines in scenarios where you need to access a route's dynamic parameters filled in by the current URL. This hook is especially useful when working with dynamic routes, as it provides an object containing the current route's dynamic parameters.&lt;/p&gt;

&lt;p&gt;Each property in the object corresponds to an active dynamic segment. The property's name is the segment's name, and the property's value is what the segment is filled in with. It can either be a string or an array of strings, depending on the type of dynamic segment. If the route contains no dynamic parameters, &lt;code&gt;useParams&lt;/code&gt; returns an empty object, providing a fail-safe mechanism for dealing with diverse routing scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  useSelectedLayoutSegments
&lt;/h2&gt;

&lt;p&gt;Lastly, &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; offers a solution for accessing the active route segments from the Layout where it's called. This functionality is vital for creating complex UI elements, such as breadcrumbs, where the context of active child segments is essential.&lt;/p&gt;

&lt;p&gt;This hook does not accept any parameters and returns an array of strings containing the active segments one level down from the layout the hook was called from, or an empty array if none exist. Additionally, the returned segments can include Route Groups which, if not desired, can be filtered out using JavaScript's &lt;code&gt;filter()&lt;/code&gt; array method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Each of these hooks brings its own unique advantages to Next.js development. &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; allows for granular segment-level navigational changes, while &lt;code&gt;usePathname&lt;/code&gt; provides a convenient way to read the current URL's pathname. &lt;code&gt;useParams&lt;/code&gt; simplifies access to dynamic route parameters, and &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; offers a means of generating context-aware UI elements like breadcrumbs.&lt;/p&gt;

&lt;p&gt;By understanding these hooks and their differences, you can create more sophisticated, dynamic, and user-friendly navigational structures in your Next.js applications. Mastering these hooks equips you to take full advantage of the power and flexibility of the Next.js framework, enhancing your web development process.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>Next.js Navigation Hooks: A Deep Dive into useSelectedLayoutSegments</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Sat, 05 Aug 2023 10:04:39 +0000</pubDate>
      <link>https://dev.to/dirheimerb/nextjs-navigation-hooks-a-deep-dive-into-useselectedlayoutsegments-22jj</link>
      <guid>https://dev.to/dirheimerb/nextjs-navigation-hooks-a-deep-dive-into-useselectedlayoutsegments-22jj</guid>
      <description>&lt;p&gt;In the Next.js framework, managing navigation within your web applications can be simplified by using Client Component hooks. One such hook, &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt;, offers a unique capability in accessing the active route segments from the Layout where it's called, thus proving invaluable for creating complex UI elements such as breadcrumbs. This blog post will take an in-depth look at &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding useSelectedLayoutSegments
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; is a Client Component hook designed to read active route segments, one level below the Layout it's called from. Here is a simple demonstration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegments&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;next/navigation&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleClientComponent&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;segments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegments&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;ul&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;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;))}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This hook is typically invoked within a Client Component which is then imported into a Layout. This is because &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; is a Client Component hook, while Layouts are Server Components by default.&lt;/p&gt;

&lt;p&gt;The hook returns an array of strings representing active segments one level below from where the hook was called, or an empty array if none exist. &lt;/p&gt;

&lt;p&gt;In situations where the returned segments include Route Groups (which you might not want to be included in your UI), you can leverage JavaScript's &lt;code&gt;filter()&lt;/code&gt; array method to exclude items starting with a bracket.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; does not accept any parameters. Here is how the return values look for different Layouts and URLs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layout&lt;/th&gt;
&lt;th&gt;Visited URL&lt;/th&gt;
&lt;th&gt;Returned Segments&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;app/layout.js&lt;/td&gt;
&lt;td&gt;/&lt;/td&gt;
&lt;td&gt;[]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard&lt;/td&gt;
&lt;td&gt;['dashboard']&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard/settings&lt;/td&gt;
&lt;td&gt;['dashboard', 'settings']&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/dashboard/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard&lt;/td&gt;
&lt;td&gt;[]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/dashboard/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard/settings&lt;/td&gt;
&lt;td&gt;['settings']&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Practical Use Case: Breadcrumbs
&lt;/h2&gt;

&lt;p&gt;Breadcrumbs are a useful navigational aid in web interfaces. They allow users to understand their current location in relation to the site's hierarchy, providing a simple way to navigate back to previous levels.&lt;/p&gt;

&lt;p&gt;In a Next.js application, you can leverage &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; to generate breadcrumbs dynamically. For example, consider a site structure where a user navigates from Home &amp;gt; Blog &amp;gt; Article. You can create a breadcrumbs component using this hook as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegments&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;next/navigation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Link&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;next/link&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Breadcrumbs&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;segments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegments&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;nav&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&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;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&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="nx"&gt;segments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&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="nx"&gt;index&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="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&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="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&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;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;))}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&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;/nav&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the hook returns an array of active route segments. We then map through these segments to create a Link component for each, generating the href by slicing the segments array up to the current index and joining the slices with a slash.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; is a potent tool that comes bundled with the Next.js framework, granting you a way to enhance the UI and UX of your web applications. It simplifies the process of creating dynamic UI elements like breadcrumbs, providing users with a clearer view of their path within the website. Mastering this hook is a sure way to level up your Next.js development skills.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>nextjs</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Harnessing the Power of Next.js Navigation Hooks: usePathname and useParams</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Sat, 05 Aug 2023 10:04:17 +0000</pubDate>
      <link>https://dev.to/dirheimerb/harnessing-the-power-of-nextjs-navigation-hooks-usepathname-and-useparams-51nf</link>
      <guid>https://dev.to/dirheimerb/harnessing-the-power-of-nextjs-navigation-hooks-usepathname-and-useparams-51nf</guid>
      <description>&lt;p&gt;Navigation in web development is key. Knowing where the user is and the parameters that define the current route can greatly enhance the user experience and functionality of an application. In this blog post, we delve into two client component hooks that Next.js offers for managing navigation and routing: &lt;code&gt;usePathname&lt;/code&gt; and &lt;code&gt;useParams&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Insight into usePathname
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;usePathname&lt;/code&gt; is a hook that allows you to read the current URL's pathname. To implement it in a Next.js application, you call &lt;code&gt;usePathname()&lt;/code&gt; in a client component and it will return the current pathname.&lt;/p&gt;

&lt;p&gt;Here's a simple usage of the hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;usePathname&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;next/navigation&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleClientComponent&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;pathname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;usePathname&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Current&lt;/span&gt; &lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This hook is intentionally designed to be used within a Client Component. These components are an integral part of the Server Components architecture and are not considered a de-optimization. &lt;/p&gt;

&lt;p&gt;For instance, a Client Component with &lt;code&gt;usePathname&lt;/code&gt; will render into HTML on the initial page load. Subsequent navigation does not necessitate a re-fetch of this component; instead, it is downloaded once (in the client JavaScript bundle), and re-renders based on the current state.&lt;/p&gt;

&lt;p&gt;Note that reading the current URL from a Server Component is not supported. This design preserves layout state across page navigations.&lt;/p&gt;

&lt;p&gt;In compatibility mode, &lt;code&gt;usePathname&lt;/code&gt; may return null when a fallback route is being rendered, or when a pages directory page has been automatically statically optimized by Next.js and the router is not ready.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;usePathname&lt;/code&gt; hook does not accept any parameters. It returns a string representing the current URL's pathname. &lt;/p&gt;

&lt;p&gt;Here are a few examples of its usage:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;Returned value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;/&lt;/td&gt;
&lt;td&gt;'/'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;/dashboard&lt;/td&gt;
&lt;td&gt;'/dashboard'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;/dashboard?v=2&lt;/td&gt;
&lt;td&gt;'/dashboard'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;/blog/hello-world&lt;/td&gt;
&lt;td&gt;'/blog/hello-world'&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This hook is handy when you need to execute something in response to a route change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;usePathname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useSearchParams&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;next/navigation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleClientComponent&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;pathname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;usePathname&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;searchParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSearchParams&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&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;// Do something 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;pathname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Leveraging useParams
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useParams&lt;/code&gt; is another Client Component hook provided by Next.js that lets you read a route's dynamic params filled in by the current URL.&lt;/p&gt;

&lt;p&gt;Consider the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useParams&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;next/navigation&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleClientComponent&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;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useParams&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="c1"&gt;// Route -&amp;gt; /shop/[tag]/[item]&lt;/span&gt;
  &lt;span class="c1"&gt;// URL -&amp;gt; /shop/shoes/nike-air-max-97&lt;/span&gt;
  &lt;span class="c1"&gt;// `params` -&amp;gt; { tag: 'shoes', item: 'nike-air-max-97' }&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;useParams&lt;/code&gt; hook does not accept any parameters. It returns an object containing the current route's filled in dynamic parameters. If the route contains no dynamic parameters, useParams returns an empty object. In pages, useParams will return null. &lt;/p&gt;

&lt;p&gt;Here's an example illustrating how it behaves for various routes:&lt;/p&gt;

&lt;p&gt;| Route | URL | useParams() |&lt;br&gt;
|-------|-----|---——--------|&lt;br&gt;
| app/shop/page.js         | /shop       | null                     |&lt;br&gt;
| app/shop/[slug]/page.js  | /shop/1     | { slug: '1' }            |&lt;br&gt;
| app/shop/[tag]/[item]/page.js | /shop/1/2  | { tag: '1', item: '2' } |&lt;br&gt;
| app/shop/[...slug]/page.js | /shop/1/2  | { slug: ['1', '2'] }     |&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;usePathname&lt;/code&gt; and &lt;code&gt;useParams&lt;/code&gt; are powerful tools provided by Next.js to handle navigation and routing within an application. They provide a developer with the ability to read and react to the current URL's pathname and dynamic parameters, enabling the development of dynamic and interactive web applications. Harnessing these hooks will take your Next.js development to the next level.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Making the Most of useSelectedLayoutSegment</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Sat, 05 Aug 2023 10:03:55 +0000</pubDate>
      <link>https://dev.to/dirheimerb/making-the-most-of-useselectedlayoutsegment-37oo</link>
      <guid>https://dev.to/dirheimerb/making-the-most-of-useselectedlayoutsegment-37oo</guid>
      <description>&lt;p&gt;The web is all about navigation, and as developers, we often need to keep track of where a user is in our app. Navigational elements often change based on where the user is currently at. In Next.js, a popular React framework, this can be accomplished by using the &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; hook. This client component hook lets you read the active route segment one level below the layout it is called from, making it an invaluable tool for navigation UI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding useSelectedLayoutSegment
&lt;/h2&gt;

&lt;p&gt;In the following example, the &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; hook is called from a client component and returns the active segment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegment&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;next/navigation&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleClientComponent&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;segment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Given that useSelectedLayoutSegment is a Client Component hook and Layouts are Server Components by default, useSelectedLayoutSegment is typically invoked via a Client Component that is imported into a Layout. The hook returns the segment one level down, making it particularly useful for UI elements such as tabs inside a parent layout, which often need to change their style based on the active child segment. &lt;/p&gt;

&lt;p&gt;If you need to return all active segments, consider using &lt;code&gt;useSelectedLayoutSegments&lt;/code&gt; instead. &lt;/p&gt;

&lt;h2&gt;
  
  
  Hook Parameters and Returns
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; does not take any parameters. It does, however, return a string of the active segment or null if one doesn't exist. Here are a few examples:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layout&lt;/th&gt;
&lt;th&gt;Visited URL&lt;/th&gt;
&lt;th&gt;Returned Segment&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;app/layout.js&lt;/td&gt;
&lt;td&gt;/&lt;/td&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard&lt;/td&gt;
&lt;td&gt;'dashboard'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/dashboard/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard&lt;/td&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/dashboard/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard/settings&lt;/td&gt;
&lt;td&gt;'settings'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/dashboard/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard/analytics&lt;/td&gt;
&lt;td&gt;'analytics'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;app/dashboard/layout.js&lt;/td&gt;
&lt;td&gt;/dashboard/analytics/monthly&lt;/td&gt;
&lt;td&gt;'analytics'&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Practical Example: Active Link Component
&lt;/h2&gt;

&lt;p&gt;The real power of &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; comes into play when we use it to create an active link component that changes its style based on the active segment. Here's an example of how to do this, using a featured posts list in the sidebar of a blog:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Link&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;next/link&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegment&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;next/navigation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// This *client* component will be imported into a blog layout&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;BlogNavLink&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;slug&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="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReactNode&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Navigating to `/blog/hello-world` will return 'hello-world'&lt;/span&gt;
  &lt;span class="c1"&gt;// for the selected layout segment&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;segment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSelectedLayoutSegment&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;isActive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;slug&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;segment&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;Link&lt;/span&gt;
      &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/blog/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;slug&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="c1"&gt;// Change style depending on whether the link is active&lt;/span&gt;
      &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;fontWeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isActive&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bold&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;normal&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;&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;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the parent Layout component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Import the Client Component into a parent Layout (Server Component)&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BlogNavLink&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;./blog-nav-link&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;getFeaturedPosts&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;./get-featured-posts&lt;/span&gt;&lt;span class="dl"&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Layout&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="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReactNode&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;featuredPosts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;getFeaturedPosts&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;featuredPosts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BlogNavLink&lt;/span&gt; &lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/BlogNavLink&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;BlogNavLink&lt;/code&gt; component uses &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; to determine the active segment. Depending on whether the link is active, the link's style will change, providing a clear visual cue for the user. &lt;/p&gt;

&lt;p&gt;In conclusion, Next.js's &lt;code&gt;useSelectedLayoutSegment&lt;/code&gt; is a handy hook that can help you build dynamic, user-friendly navigation interfaces. By using it wisely, you can greatly enhance the user experience on your site.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Deep Dive into useSearchParams: Mastering Query String Manipulation</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Sat, 05 Aug 2023 10:02:43 +0000</pubDate>
      <link>https://dev.to/dirheimerb/deep-dive-into-usesearchparams-mastering-query-string-manipulation-3m8p</link>
      <guid>https://dev.to/dirheimerb/deep-dive-into-usesearchparams-mastering-query-string-manipulation-3m8p</guid>
      <description>&lt;p&gt;The rise of Server-side rendering (SSR) has gained popularity for a few reasons. One of the main benefits is improved performance and user experience. With SSR, the server generates the initial HTML content and sends it directly to the client, making the website or application load faster. This can reduce the time to first meaningful paint and improve SEO since search engines can easily crawl and index the content.&lt;/p&gt;

&lt;p&gt;This has left popular libraries like React JS, looking for an answer. This is lead to the partnership of react, and next JS to develop a solution working together. This has lead to so awesome new hooks, among which one that often goes unnoticed is 'useSearchParams'. This client component hook lets you read the current URL's query string, making it easier than ever to manage URL parameters within your Next.js applications.&lt;/p&gt;

&lt;p&gt;Let's start by examining the basics of the useSearchParams hook and how to implement it in your Next.js application.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Basic Example: The Search Bar
&lt;/h2&gt;

&lt;p&gt;Consider the following TypeScript example of a search bar component, which uses useSearchParams to read the query string from the URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSearchParams&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;next/navigation&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;SearchBar&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;searchParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSearchParams&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;search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;search&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// URL -&amp;gt; `/dashboard?search=my-project`&lt;/span&gt;
  &lt;span class="c1"&gt;// `search` -&amp;gt; 'my-project'&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Search&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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 the example above, useSearchParams returns a read-only version of the URLSearchParams interface. This interface provides several utility methods for reading and manipulating the URL's query string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the URLSearchParams Interface
&lt;/h2&gt;

&lt;p&gt;When you call useSearchParams, it returns a read-only version of the URLSearchParams interface. This interface includes several utility methods such as get(), has(), getAll(), keys(), values(), entries(), forEach(), and toString(). &lt;/p&gt;

&lt;p&gt;Notably, the &lt;code&gt;get()&lt;/code&gt; method returns the first value associated with the specified search parameter, while the &lt;code&gt;has()&lt;/code&gt; method returns a boolean value indicating if a given parameter exists. &lt;/p&gt;

&lt;p&gt;Do note that useSearchParams is a Client Component hook and is not supported in Server Components. This is to prevent stale values during partial rendering, maintaining the freshness and accuracy of your data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Static Rendering and Dynamic Rendering
&lt;/h2&gt;

&lt;p&gt;Static rendering and dynamic rendering are two significant aspects of how useSearchParams behaves. &lt;/p&gt;

&lt;p&gt;In statically rendered routes, calling useSearchParams will cause the tree up to the closest Suspense boundary to be client-side rendered. To reduce the portion of the route that is client-side rendered, you can wrap the component using useSearchParams in a Suspense boundary. &lt;/p&gt;

&lt;p&gt;For dynamically rendered routes, useSearchParams will be available on the server during the initial server render of the Client Component. You can force dynamic rendering by setting the dynamic route segment config option to force-dynamic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server Components
&lt;/h2&gt;

&lt;p&gt;In Pages (Server Components), you can access search params through the searchParams prop. However, Layouts (Server Components) do not receive this prop to prevent stale searchParams between navigations. In such cases, use the Page searchParams prop or the useSearchParams hook in a Client Component, which is re-rendered on the client with the latest searchParams.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating searchParams
&lt;/h2&gt;

&lt;p&gt;To set new searchParams, you can use useRouter or Link. After a navigation is performed, the current page.js will receive an updated searchParams prop. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleClientComponent&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;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRouter&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;pathname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;usePathname&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;searchParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSearchParams&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

  &lt;span class="c1"&gt;// Get a new searchParams string by merging the current&lt;/span&gt;
  &lt;span class="c1"&gt;// searchParams with a provided key/value pair&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createQueryString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&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;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;searchParams&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="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Sort&lt;/span&gt; &lt;span class="nx"&gt;By&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* using useRouter */&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="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// &amp;lt;pathname&amp;gt;?sort=asc&lt;/span&gt;
          &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;createQueryString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sort&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;asc&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="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;ASC&lt;/span&gt;
      &lt;span class="o"&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="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* using &amp;lt;Link&amp;gt; */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt;
        &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// &amp;lt;pathname&amp;gt;?sort=desc&lt;/span&gt;
          &lt;span class="nx"&gt;pathname&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;createQueryString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sort&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;desc&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="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;DESC&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, useSearchParams is a powerful tool in the Next.js arsenal, allowing you to elegantly handle URL query strings within your application. Understanding and mastering this hook will undoubtedly bring your Next.js development skills to the next level.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>Understanding and Implementing One-Time Password (OTP) Input Component in React</title>
      <dc:creator>Bradley Dirheimer</dc:creator>
      <pubDate>Tue, 18 Jul 2023 02:17:24 +0000</pubDate>
      <link>https://dev.to/dirheimerb/understanding-and-implementing-one-time-password-otp-input-component-in-react-3ed5</link>
      <guid>https://dev.to/dirheimerb/understanding-and-implementing-one-time-password-otp-input-component-in-react-3ed5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Do you find yourself curious about how the one-time password (OTP) input component operates on an application or webpage? This OTP feature, composed of several input elements, each accepting only one character, behaves as a cohesive unit, akin to a normal input component. In this blog post, we will explain how to create this OTP component, understand its workings, and delve into the various use cases involved with it. Let's dive into it right away!&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the OTP Component
&lt;/h2&gt;

&lt;p&gt;The OTP or passcode component is essentially a collection of individual input boxes where each box corresponds to a single character of the passcode. This has become a prevalent practice in modern web and mobile applications as part of the validation process. However, implementing this feature is not as straightforward as it seems. It requires handling a variety of scenarios, including key event handling, providing a user-friendly pasting experience, and managing focus shifts in a sensible manner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Scenarios in a Passcode Component
&lt;/h2&gt;

&lt;p&gt;When designing the passcode component, it's crucial to consider the following scenarios:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;View a live view on &lt;a href="https://react-next-passcode.vercel.app"&gt;vercel&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Fundamental scenarios:
&lt;/h3&gt;

&lt;p&gt;Each input box should accept only a single character.&lt;br&gt;
Upon character entry, the focus should automatically shift to the next input element.&lt;br&gt;
When backspacing, the focus should shift from the current to the previous input element.&lt;br&gt;
Advanced scenarios:&lt;/p&gt;

&lt;p&gt;Pasting Experience: This includes handling user permissions, determining where the focus should land after pasting, and managing how the input fields are populated when a value is pasted.&lt;br&gt;
Diving into the Code Implementation&lt;br&gt;
Now that we've explored the key scenarios, let's delve into how to implement them. Below is a code snippet demonstrating a more advanced version of the OTP component:&lt;/p&gt;
&lt;h3&gt;
  
  
  Advanced scenarios:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Pasting Experience: This includes handling user permissions, determining where the focus should land after pasting, and managing how the input fields are populated when a value is pasted.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Diving into the Code Implementation
&lt;/h2&gt;

&lt;p&gt;Now that we've explored the key scenarios, let's delve into how to implement them. Below is a code snippet demonstrating a more advanced version of the OTP component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dirheimerb/react-next-passcode.git"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;State Variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&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;arrayValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setArrayValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&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="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;maskedValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMaskedValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&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="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;showTooltip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setShowTooltip&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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;inputRefs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The component maintains four separate pieces of state:&lt;/p&gt;

&lt;p&gt;arrayValue is the actual value of the four-digit code being entered.&lt;br&gt;
maskedValue is a display-only value which replaces the numbers in arrayValue with asterisks.&lt;br&gt;
showTooltip is a boolean value that toggles the display of a tooltip.&lt;br&gt;
inputRefs holds references to the four input elements.&lt;br&gt;
Page load focus:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;inputRefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]?.&lt;/span&gt;&lt;span class="nx"&gt;focus&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This useEffect hook is triggered on initial render, and it sets the focus to the first input field.&lt;/p&gt;

&lt;p&gt;Paste function:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Copy&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onPaste&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ClipboardEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This function handles pasting from the clipboard. It prevents the default paste action, and instead splits the pasted text into an array. If every character in the array is a number, it updates arrayValue and maskedValue with the pasted values.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Key down function:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onKeyDown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;KeyboardEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;p&gt;This function handles key down events. It prevents the default action for every key that isn't Backspace, Delete, Tab, or a number between 0 and 9. If one of the forbidden keys is pressed, it displays a tooltip for two seconds.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Change function:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BaseSyntheticEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This function is called whenever the user types into an input. If the input is a number, it updates arrayValue and maskedValue. If the input is not empty, it moves the focus to the next input.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Key up function:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onKeyUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;KeyboardEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&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;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This function is called when a key is released. If Backspace or Delete is released, it clears the current input and moves the focus to the previous input.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Reset function:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;resetInputs&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="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This function resets all inputs and refocuses on the first input.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Submit function:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleSubmit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="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="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This function handles the form submission. Right now, it simply logs the entered value. You would typically add your database logic here.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://codesandbox.io/p/github/dirheimerb/react-next-passcode/draft/nervous-fog?layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522vertical%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522clk7nwfu605tp3b6m38clqgkm%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522clk7nwfu605tq3b6m5raffk76%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B50%252C50%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522clk7nwfu605tp3b6m38clqgkm%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clk7nvzl105ol3b6m5u9gn1ne%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522filepath%2522%253A%2522%252FREADME.md%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%255D%252C%2522id%2522%253A%2522clk7nwfu605tp3b6m38clqgkm%2522%252C%2522activeTabId%2522%253A%2522clk7nvzl105ol3b6m5u9gn1ne%2522%257D%252C%2522clk7nwfu605tq3b6m5raffk76%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A3000%252C%2522id%2522%253A%2522clk7ngaox02lp3b6m6qdumg64%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522path%2522%253A%2522%252F%2522%257D%252C%257B%2522id%2522%253A%2522clk7nhs9z03te3b6mww3r0gi5%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A3000%252C%2522path%2522%253A%2522%252F%2522%257D%252C%257B%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522dev%2522%252C%2522id%2522%253A%2522clk7lence01ff3b6m20w3ltyj%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%255D%252C%2522id%2522%253A%2522clk7nwfu605tq3b6m5raffk76%2522%252C%2522activeTabId%2522%253A%2522clk7ngaox02lp3b6m6qdumg64%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Afalse%252C%2522showSidebar%2522%253Afalse%252C%2522sidebarPanelSize%2522%253A15%257D" rel="noopener noreferrer"&gt;
      codesandbox.io
    &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>opensource</category>
      <category>typescript</category>
      <category>reactjsdevelopment</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
