<?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: Tran Van Quyet</title>
    <description>The latest articles on DEV Community by Tran Van Quyet (@vanquyet).</description>
    <link>https://dev.to/vanquyet</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%2F971407%2Fdb36f31b-97d4-47bf-b66f-623a66fef260.jpeg</url>
      <title>DEV Community: Tran Van Quyet</title>
      <link>https://dev.to/vanquyet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vanquyet"/>
    <language>en</language>
    <item>
      <title>Introducing useCatch: The Easy Way to Create Error Boundaries in Fiddlehead</title>
      <dc:creator>Tran Van Quyet</dc:creator>
      <pubDate>Tue, 07 Feb 2023 18:17:37 +0000</pubDate>
      <link>https://dev.to/vanquyet/introducing-usecatch-the-easy-way-to-create-error-boundaries-in-fiddlehead-11jg</link>
      <guid>https://dev.to/vanquyet/introducing-usecatch-the-easy-way-to-create-error-boundaries-in-fiddlehead-11jg</guid>
      <description>&lt;p&gt;Have you ever encountered a situation where a component in your React app throws an unexpected error and causes the entire app to break down? This can be a frustrating experience for both you as the developer and for your users.&lt;/p&gt;

&lt;p&gt;Fortunately, you can handle such errors with error boundaries. Error boundaries are components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.&lt;/p&gt;

&lt;p&gt;In React, to create an error boundary, you typically need to use a class component with methods such as &lt;code&gt;componentDidCatch&lt;/code&gt; and &lt;code&gt;getDerivedStateFromError&lt;/code&gt;. However, in &lt;a href="https://github.com/fiddleheadjs/fiddlehead"&gt;Fiddlehead&lt;/a&gt;, creating an error boundary is much simpler. With the &lt;a href="https://fiddleheadjs.com/API/useCatch"&gt;useCatch&lt;/a&gt; hook, you can easily catch errors during rendering and in hook callbacks in the subtree below, and provide a user-friendly message instead of a blank screen.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use the &lt;code&gt;useCatch&lt;/code&gt; hook to create an error boundary in Fiddlehead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useCatch&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;fiddlehead&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ErrorBoundary&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="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clearError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&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="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Oops... Something went wrong!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;children&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 the example above, the &lt;code&gt;ErrorBoundary&lt;/code&gt; component uses the &lt;code&gt;useCatch&lt;/code&gt; hook to catch errors during rendering and in hook callbacks in the subtree below. When an error occurs, it displays either the error name and message, or a user-friendly message of "Oops... Something went wrong!"&lt;/p&gt;

&lt;p&gt;In addition to catching and displaying errors, you may also want to log the errors to a logging service. Here's an example of how you can use the &lt;code&gt;useCatch&lt;/code&gt; hook to log errors to a logging service in the background:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useCatch&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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fiddlehead&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;logErrorToService&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;./logging&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ErrorBoundary&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="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clearError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCatch&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Log the error to a logging service&lt;/span&gt;
      &lt;span class="nx"&gt;logErrorToService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Oops... Something went wrong!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;children&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;The name "useCatch" in Fiddlehead was inspired by the "catch" keyword in the traditional try-catch statement. Just as the "catch" keyword is used to handle errors in a try-catch block, the &lt;code&gt;useCatch&lt;/code&gt; hook in Fiddlehead allows you to handle errors during the rendering of your components, or the execution of the callbacks of hooks (&lt;code&gt;useEffect&lt;/code&gt;, &lt;code&gt;useState&lt;/code&gt;,...), making it an error boundary for your application.&lt;/p&gt;

&lt;p&gt;Similar to React, &lt;a href="https://fiddleheadjs.com/API/useCatch"&gt;useCatch&lt;/a&gt; in Fiddlehead do NOT catch errors for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Errors that occur within event handlers&lt;/li&gt;
&lt;li&gt;Asynchronous errors, such as those that occur within &lt;code&gt;setTimeout&lt;/code&gt; or &lt;code&gt;requestAnimationFrame&lt;/code&gt; callbacks&lt;/li&gt;
&lt;li&gt;Errors that are thrown within the error boundary component itself, instead of its children components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whether you want to display a user-friendly message, log errors to a service, or both, the &lt;code&gt;useCatch&lt;/code&gt; hook has you covered. So why not give &lt;a href="https://fiddleheadjs.com"&gt;Fiddlehead&lt;/a&gt; a try and transform your web development experience today?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>hook</category>
    </item>
    <item>
      <title>Introduction to Fiddlehead: A Fast and Lightweight UI Library</title>
      <dc:creator>Tran Van Quyet</dc:creator>
      <pubDate>Sat, 04 Feb 2023 21:20:40 +0000</pubDate>
      <link>https://dev.to/vanquyet/introduction-to-fiddlehead-a-fast-and-lightweight-ui-library-273p</link>
      <guid>https://dev.to/vanquyet/introduction-to-fiddlehead-a-fast-and-lightweight-ui-library-273p</guid>
      <description>&lt;p&gt;&lt;a href="https://fiddleheadjs.com" rel="noopener noreferrer"&gt;Fiddlehead&lt;/a&gt; is a fast and lightweight UI library that offers a simple, yet powerful approach to building user interfaces. Built with performance and simplicity in mind, Fiddlehead provides an intuitive API for managing state and updating the user interface in response to changes in state. This makes it an ideal choice for building single-page applications or UI libraries.&lt;/p&gt;

&lt;p&gt;Fiddlehead is designed to be easy to use for developers of all skill levels, and is built with a functional programming style. This means that it is easy to understand, write, and debug, and it eliminates the need for complex class components. It also eliminates the need for third-party state management libraries, as it provides built-in support for managing state.&lt;/p&gt;

&lt;p&gt;The library also comes with a suite of built-in hooks that make it easy to manage state and update the user interface in response to changes in state. For example, the &lt;a href="https://fiddleheadjs.com/API/useState" rel="noopener noreferrer"&gt;useState&lt;/a&gt; hook can be used to manage the state of a component, while the &lt;a href="https://fiddleheadjs.com/API/useEffect" rel="noopener noreferrer"&gt;useEffect&lt;/a&gt; hook can be used to run side-effects such as making API calls or setting up event listeners.&lt;/p&gt;

&lt;p&gt;Additionally, the &lt;a href="https://fiddleheadjs.com/API/store" rel="noopener noreferrer"&gt;store&lt;/a&gt; API provides a simple way to share state between components, making it easy to build complex and reusable user interfaces. The API includes a set of hooks for reading, writing, and initializing store data, and it makes it easy to manage the state of an application in a single place.&lt;/p&gt;

&lt;p&gt;Overall, Fiddlehead offers a fast, lightweight, and intuitive approach to building user interfaces, making it an ideal choice for developers who are looking for a simple, yet powerful tool for building web applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fiddleheadjs.com/#try-it-now" rel="noopener noreferrer"&gt;Try it now!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
      <category>workplace</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
