<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muhammad Rauf Pratama on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Rauf Pratama on Medium]]></description>
        <link>https://medium.com/@raufpratama?source=rss-1c3733efe812------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lYp0aHai_UUe5po4faNrgA@2x.jpeg</url>
            <title>Stories by Muhammad Rauf Pratama on Medium</title>
            <link>https://medium.com/@raufpratama?source=rss-1c3733efe812------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 20:13:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raufpratama/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React-Query: You may not need state management‍♂️]]></title>
            <link>https://javascript.plainenglish.io/react-query-you-may-not-need-state-management-%EF%B8%8F-15de0837a569?source=rss-1c3733efe812------2</link>
            <guid isPermaLink="false">https://medium.com/p/15de0837a569</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Muhammad Rauf Pratama]]></dc:creator>
            <pubDate>Mon, 30 Aug 2021 10:07:25 GMT</pubDate>
            <atom:updated>2021-08-31T15:01:17.002Z</atom:updated>
            <content:encoded><![CDATA[<h3>React-Query: You Might Not Need State Management🤷🏽‍♂️</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*IJBBRmlBuUSnlhZN" /></figure><h4>TL;DR</h4><blockquote><strong>react-query</strong> is often called “the missing data fetching library”. It’s server-state, able to manage cache data, asynchronously update stale data in the background. It might be a good replacement for global state management library such as Redux or MobX to handle the data. The core of this story is in the <a href="#fe93">“<strong>Basic Usage</strong>”</a> section. The code example shown below is available <a href="https://codesandbox.io/s/react-query-basics-c2yom">here</a>.</blockquote><h4>Table of contents</h4><blockquote>*) <a href="#332f">Introduction</a><br>*) <a href="#b35d">Concept</a><br>*) <a href="#fe93">Basic Usage<br></a>*) <a href="#4a2e">a. useQuery</a><br>*) <a href="#16a9">b. useMutation</a><br>*) <a href="#841c">c. Query invalidation</a><br>*) <a href="#8a1d">Prefetch</a><br>*) <a href="#740f">Conclusion</a><br>*) <a href="#afaf&#39;">Reference</a></blockquote><h3>Introduction</h3><p>Using global state pattern with libraries like Redux, MobX, or Context API has many advantages, but the weakness lies in the difference in data types. There is data that stores information about applications, for example, dark mode, and there is also data that is often manipulated, such as product lists and product details. The two types of data can be referred to as <strong>Client State</strong> and <strong>Server State</strong>.</p><p>I often find myself in need of 30 minutes to wire up the redux boilerplate code till it’s ready to use. I use create-react-app with a TypeScript template, and it also needs an additional library like redux-thunk or redux-saga for handling asynchronous processes.</p><p>The most striking difference between client-state and server-state is that server-state comes from 2 sources, namely the backend and frontend, which means that the application must sync all the time so that the state or data remains updated. It is not the best practice to make requests all the time. We need a tool that can display the old state but can also update in the background.</p><h3>Concept</h3><p>react-query is often referred to as the missing react data-fetching library. Yes, this library is a solution to the problem described earlier. Using react-query allows us to fetch, update, caching data async very easily.</p><p>react-query have 3 (three) core concepts:</p><ul><li><strong>Queries</strong>: A query is a declarative dependency on an asynchronous source of data that is tied to a <strong>unique key.</strong></li><li><strong>Mutation</strong>: Just like queries, the difference lies in the process to the server. Mutation is often and recommended for modifying data create/update/delete.</li><li><strong>Query Invalidation</strong>: It enables us to do background update for certain query after the query is invalid. It’s useful when it comes to a case where the user made some changes about a product and you might want to refetch the product right after being updated.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*B_avuXAu1XDBxA6C1NSNWQ.png" /><figcaption>react-query data behavior</figcaption></figure><p>react-query has terms for its states:</p><ul><li><strong>Fetching</strong>: It’s the beginning process to get data from the server, it either resolves data or returns an error.</li><li><strong>Fresh data</strong>: The data that just return by the successful fetching process.</li><li><strong>Stale data</strong>: At a certain time the fresh data will turn into stale data (out of date), which indicates data needs to be updated in the background. One of many ways to automatically refetch stale data is window refocusing, or we can configure with refetch interval in the query.</li><li><strong>Inactive data</strong>: The data indicates that it is no longer used, and it will be deleted for a certain amount of time.</li><li><strong>Deleted data</strong>: This process will run after a certain amount period of inactive data. The time can be configured for each query, or global.</li></ul><h3>Basic usage</h3><p>There are several hooks that are used most often when using this library. But before we start, we need to connect our component to react-query by QueryClient instance and wrap our component with QueryClient provider.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/225db66eb17e2e108ed857195ac982b8/href">https://medium.com/media/225db66eb17e2e108ed857195ac982b8/href</a></iframe><p>react-query provides a useful dev-tool for debugging to detect errors, you can directly import ReactQueryDevTools from react-query/devtool. Some props that can be pass to ReactDevTools:</p><ul><li><strong>initialIsOpen: boolean </strong>: defaults to false, set this to true to automatically opens dev-tool when initiating the app.</li><li><strong>position?: “top-left&quot; | “top-right&quot; | “bottom-left&quot; | “bottom-right&quot;: </strong>defaults to “bottom-left”.</li></ul><p>note: Don’t worry about excluding ReactQueryDevTools to production build, by default it’s it will not be included in production bundles when process.env.NODE_ENV === ”production”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0d79e7978d8129fb0ab0a7d2774ec31/href">https://medium.com/media/e0d79e7978d8129fb0ab0a7d2774ec31/href</a></iframe><h4>a. useQuery</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b08e3a1db320433d2a52aee52cf3c457/href">https://medium.com/media/b08e3a1db320433d2a52aee52cf3c457/href</a></iframe><p>This hook has some very useful return values, some of them are:</p><ul><li><strong>data: TData</strong> : if <strong>queryFn</strong> is successful, it will return results data.</li><li><strong>error: null | TError</strong> : otherwise, it will return an error, it’s an object that the value depends on the <strong>queryFn</strong> throw.</li><li><strong>isLoading: boolean</strong> : It will be set to true if the query is processing or there’s no cached data related.</li></ul><p>This hook also requires 2 parameters that need to be passed:</p><ul><li><strong>queryKey </strong>: used to uniquely identify and caching your query, it can be any kind of data type. The simplest one is String, or if it comes to specific information, for example, detail of contact, you might want to use Array as a key.</li><li><strong>queryFn</strong> : promise-based that resolved data, function like fetch or Axios.</li></ul><p>In general, this API is used for the process of getting information, for data manipulation, it is recommended to use <strong>useMutation</strong>.</p><h4>b. useMutation</h4><p>Unlike the useQuery hook, this query is usually used for data manipulation create/update/delete. This hook returns several functions and values, but we will only focus on mutate: (variables: TVariables, { onSuccess, onSettled, onError}) because it is used the most and for sake of simplicity. This hook also accepts several functions and callbacks:</p><ul><li><strong>mutationFn: (variables: TVariables) =&gt; Promise&lt;TData&gt; Required</strong> : same as the query function, the difference is this function receives variables: TVariables by invoking mutate.</li><li><strong>onSuccess: (data: TData, variables: TVariables, context?: TContext) =&gt; Promise&lt;unknown&gt; | void</strong> : a callback function when the mutation is successful and contains mutation results.</li><li><strong>onError: (err: TError, variables: TVariables, context?: TContext) =&gt; Promise&lt;unknown&gt; | void</strong> : a callback function when the mutation is throwing an error and contains mutation result.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1048ca6207e11aac514ad33cf46b84e/href">https://medium.com/media/c1048ca6207e11aac514ad33cf46b84e/href</a></iframe><h4>c. Query invalidation</h4><p>Query invalidation is performed to indicate that the query or related data is old data and needs to be updated, first it will mark related query as stale, then automatically refetch. There are several ways to invalidate a query of on them are <strong>stale time</strong> and <strong>invalidateQueries</strong> method.</p><p>To stale a query, it can be pass to useQuery hooks as an option:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/636e78d4a927cebe09c6458eeddb012b/href">https://medium.com/media/636e78d4a927cebe09c6458eeddb012b/href</a></iframe><p>Or we can use <strong>invalidateQueries</strong> method from <strong>QueryClient</strong> instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9442e6c4f805f95ad6f1835b6c651aad/href">https://medium.com/media/9442e6c4f805f95ad6f1835b6c651aad/href</a></iframe><p>We can also do using <strong>invalidateQueries</strong> method after doing a mutation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f87491c32667bb48ee59c3e8178d6766/href">https://medium.com/media/f87491c32667bb48ee59c3e8178d6766/href</a></iframe><h3>Prefetching</h3><p>Imagine if you have an e-commerce app, and you have a preview feature that displays minimum information about a product directly when you hover over it. If this the case, you might want to do a prefetch to the data for better UX.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/439dc95cd59012b5500338cf4d4bc325/href">https://medium.com/media/439dc95cd59012b5500338cf4d4bc325/href</a></iframe><h3>Conclusion</h3><p>There’s more impactful features react-query can do, some of it’s <strong>Parallel Query</strong>, <strong>Dependent Query</strong>, <strong>Paginated Query</strong>, and <strong>Infinite Query</strong>,<strong> etc</strong>. All of react-query mention before are the most commonly used. react-query manage cache data and does asynchronous processes such as runs background update. It also replaces all the boilerplate code that Redux, etc use to manage data.</p><p>So, we bump into a question: does react-query replace global state management pattern tools like Redux, MobX, or Context API? Well, if the context is about managing data that needs to be updated in a certain amount of time to keep the data fresh, I am more inclined to answer yes, you can combine it. But for global state management on client state only, use it as a protocol so the component can communicate, e.g., theming. But if your app is more like “page builder” where the client-state often changes synchronously, then you should stick with the global state management pattern.</p><h3>Reference</h3><ul><li><a href="https://react-query.tanstack.com/overview">Linsley, Tanner. “Official Docs.” <em>React Query</em>, react-query.tanstack.com/overview.</a></li><li><a href="https://dev.to/rootstrap/react-query-and-management-of-server-state-8ol">Santos, Nicolas. “React Query and Management of Server State.” <em>DEV Community</em>, DEV Community, 29 Sept. 2020, dev.to/rootstrap/react-query-and-management-of-server-state-8ol.</a></li><li><a href="https://tkdodo.eu/blog/react-query-and-type-script">TkDodo, Dominic Dorfmeister. “React Query and Typescript.” <em>TkDodo’s Blog</em>, 16 May 2021, tkdodo.eu/blog/react-query-and-type-script.</a></li><li><a href="https://tsh.io/blog/react-query-tutorial/">Developer, Dawid Piela Frontend, et al. “The next Big Thing! React Query for Server-State Data Management.” <em>The Software House</em>, 14 May 2021, tsh.io/blog/react-query-tutorial/.</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15de0837a569" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/react-query-you-may-not-need-state-management-%EF%B8%8F-15de0837a569">React-Query: You may not need state management🤷🏽‍♂️</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s Validate Our Form: React Hook Form ft. TypeScript]]></title>
            <link>https://javascript.plainenglish.io/lets-validate-our-form-react-hook-form-ft-typescript-1c5184c04429?source=rss-1c3733efe812------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c5184c04429</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Muhammad Rauf Pratama]]></dc:creator>
            <pubDate>Fri, 06 Aug 2021 17:23:09 GMT</pubDate>
            <atom:updated>2021-09-02T13:51:07.081Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJR3ANI2meHz5D5i1pPvlg.png" /><figcaption>picture from Desmond Gerber in <a href="https://morioh.com/p/0d8a67bc4b5e">morioh</a></figcaption></figure><blockquote><strong>TLDR</strong>: react-hook-form is a powerful library for handling form, it reduces the number of re-render because it’s embracing the uncontrolled way of handling form. You will often use useForm hook provided by the library to use its power. This hook is explained in section <strong>2a. useForm.</strong></blockquote><p>Validating a form in JavaScript or React applications does not come in an opinionated way, of course! cause it depends on which context you are working on and the UI design is. We might handle it by ourselves or use a third-party library. Sometimes it will take more time to do validation ourselves, it will also confuse other developers who just take place to handle related features, every developer has their own style of handling it.</p><p>There’s a third-party library call react-hook-form, it’s intended to help developers handling form validation. So, what’s the point of using a third-party library if it just makes 3–4 fewer lines of code?. Well, react-hook-form come with another advantage, as it says in <a href="https://react-hook-form.com/">official docs</a>:</p><blockquote>Reducing the amount of code you need to write, and removing unnecessary re-renders are some of the primary goals of React Hook Form</blockquote><p>It’s true because one of the reasons is react-hook-form mainly using an Uncontrolled way of handling form. If you are wondering what the uncontrolled way of handling form is, you can read my previous story about <a href="https://medium.com/@pratamarauf8/controlled-vs-uncontrolled-component-%EF%B8%8F-fa707c759967">Controlled vs Uncontrolled</a> in React. Enough talk, show me the code!</p><h3>1. Installation</h3><p>It only needs a single command, and we’re ready to go:</p><pre>//npm<br>npm install react-hook-form</pre><pre>// yarn<br>yarn add react-hook-form</pre><blockquote>Disclaimer: in this story, I’m using <a href="https://github.com/react-hook-form/react-hook-form/releases/tag/v7.12.2">react-hook-form v7.12.2</a> as an example.</blockquote><h3>2. Usage</h3><p>react-hook-form provides many functions and hook to work with handling forms, but there’s a hook called <a href="https://react-hook-form.com/api/useform">useForm</a> , useController, and useFieldArray, this hook is often used, more than any other.</p><h4>a. useForm</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cbfb8abe4941d925ec6f9865247591a/href">https://medium.com/media/2cbfb8abe4941d925ec6f9865247591a/href</a></iframe><p>There are several functions return by useForm hook, let’s break it down.</p><p><a href="https://react-hook-form.com/api/useform/register"><strong>register</strong></a> it registers the input or select element to react-hook-form so it enables us to use the library validation/features. Technically we just pass the input/select ref to the function. We can also add validation by passing an object to the second argument, these are validation rules supported:</p><ul><li>required: boolean</li><li>min: number</li><li>max: number</li><li>minLength: number</li><li>maxLength: number</li><li>pattern: RegExp</li><li>validate: Function | Object</li></ul><p><a href="https://react-hook-form.com/api/useform/handlesubmit"><strong>handleSubmit</strong></a><strong> </strong>this function accepts 2 callbacks<strong>, </strong>SubmitHandler for successful cases, and SubmitErrorHandler for error cases<strong>. </strong>this function will first validate our form before invoking onSubmit/onSubmitError function.</p><p><a href="https://react-hook-form.com/api/useform/watch"><strong>watch</strong></a><strong> </strong>a function that keeps watching inputs changes, specified by passing input key/name to the function, it accepts string or array of strings. this function will cause re-render to the root component, we can use <a href="https://react-hook-form.com/api/usewatch">useWatch</a> hook for better performance.</p><p><a href="https://react-hook-form.com/api/useform/formstate"><strong>formState</strong></a> is an object that keeps the information about the form state, one of the values it returns is errors . errors is an object that contains every invalid input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*_LqEeH9eL_hhwf4-KIUmJQ.png" /><figcaption>useForm basic usage flow</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2F971471c3a8d041c3a93858e2e13978af&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2F971471c3a8d041c3a93858e2e13978af&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2F971471c3a8d041c3a93858e2e13978af-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="1096" height="822" frameborder="0" scrolling="no"><a href="https://medium.com/media/95ce4b0796fd7f9b2482c8d167bf0e0b/href">https://medium.com/media/95ce4b0796fd7f9b2482c8d167bf0e0b/href</a></iframe><h4>b. <strong>useController</strong></h4><p>It allows us to work with external controlled components such as React-Select, AntD, Material-UI, or any inputs that don’t expose refs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83d05932bbdc8b1c57695c1ed6ed5dbc/href">https://medium.com/media/83d05932bbdc8b1c57695c1ed6ed5dbc/href</a></iframe><p>We create a reusable controlled component using useController, it accepts <a href="https://react-hook-form.com/api/usecontroller">UseControllerProps&lt;T&gt;</a> as props. useController returns several objects and functions. Let’s break it down.</p><p><a href="https://react-hook-form.com/api/usecontroller"><strong>field</strong></a> this object returns several functions like onChange, onBlur, ref, name, and value. To wire up react-hook-form with controlled inputs, basically, we just pass all of its value inside the field by doing spread operator</p><p><a href="https://react-hook-form.com/api/usecontroller"><strong>fieldState</strong></a> an object that keeps information to the specific inputs, similar to formState in useForm.</p><p>We also pass some UseControllerProps&lt;T&gt; to the controlled reusable component we just create, that is control , name , and rules . control contains a method for registering related input, name is used as a unique input key, and rules are options for how we can apply validations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*0D1JxXZiNa8lJJJIJutsJA.png" /><figcaption>Wiring up controlled input to react-hook-form flow using useController</figcaption></figure><h4><strong>c. useFieldArray</strong></h4><p>useFieldArray is one of the powerful hooks provided by react-hook-form , it enables us to work with dynamic inputs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fccbb79d566f323d02185321e673d87/href">https://medium.com/media/5fccbb79d566f323d02185321e673d87/href</a></iframe><p>Similar to useController, fields in useFieldArray value is initiated by setting defaultValues on useForm.</p><p>in the code example, first, we declare useForm hook and pass an array of objects to defaultValues prop which will be passed onto useFieldArray hook by providing control function from useForm to the useFieldArray prop.</p><p>useForm accepts minimal props and returns some useful objects/functions line 32.</p><p>We use fields object that initiated by setting defaultValues to display inputs by mapping it, by default fields object has and id that useful as a key prop when mapping the object. the id generated using uuid this provides by react-hook-form</p><p>Then, we can modify the array with other functions returns by useFieldArray , such as append, prepend, insert at, remove, swap, and move</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2F3f73a2f1daeb48c795f050daadbfe6b5&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2F3f73a2f1daeb48c795f050daadbfe6b5&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2F3f73a2f1daeb48c795f050daadbfe6b5-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="960" height="720" frameborder="0" scrolling="no"><a href="https://medium.com/media/f68a4ab21ef980fe0fbe44e0b6dc4bd2/href">https://medium.com/media/f68a4ab21ef980fe0fbe44e0b6dc4bd2/href</a></iframe><h3><strong>Conclusion</strong></h3><p>react-hook-form is a powerful third-party library for handling form, and it has advantages over other libraries, which reduces the number of re-renders. react-hook-form has other powerful APIs/hook to boost performance such as useWatch, useFormState, and useFormContext .</p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c5184c04429" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/lets-validate-our-form-react-hook-form-ft-typescript-1c5184c04429">Let’s Validate Our Form: React Hook Form ft. TypeScript📝</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Controlled vs Uncontrolled Component ⚛️]]></title>
            <link>https://medium.com/@raufpratama/controlled-vs-uncontrolled-component-%EF%B8%8F-fa707c759967?source=rss-1c3733efe812------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa707c759967</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Muhammad Rauf Pratama]]></dc:creator>
            <pubDate>Mon, 02 Aug 2021 03:39:55 GMT</pubDate>
            <atom:updated>2021-08-02T03:39:55.294Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Kj9skikPpm1pm9v0" /><figcaption>Photo by <a href="https://unsplash.com/@oskaryil?utm_source=medium&amp;utm_medium=referral">Oskar Yildiz</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When it comes to handling form, and we suddenly might thinking about how React handling its component. There are two ways to handling form in React, with <strong>Controlled</strong> and <strong>Uncontrolled</strong> inputs/components. What’s the difference?</p><h3>1. Controlled Component</h3><p>Controlled input/component accept value by passing it to its props. It also has an event handler to update the value. The value needs to be store somewhere to keep track, in React we can store it in the local state, and we can handle it with this.setState or useState if using hooks. We take an&lt;Input {…props}/&gt; for example, note: it’s just a standard HTML input. It takes value and onChange , we pass value from local state to input value props, and onChange to update the value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/904b8157bc968f77398389db6c621f4b/href">https://medium.com/media/904b8157bc968f77398389db6c621f4b/href</a></iframe><p>It will update the state on every keystroke. When the state change on parent component, it will re-render the component including any other child component which in this example parent component only have input component as child.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*eNpEs2MaC5fMuV-7ak7eyA.png" /><figcaption>Controlled input/component flow in React</figcaption></figure><h3>2. Uncontrolled Component</h3><p>Similar to traditional HTML form inputs. It handles the state and maintains the state by the DOM itself. There’s zero need to use an event handler to update the state. To access its value we use a ref, in React we can use createRef or useRef .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4e4a2568c68c1c8ca81bcb9d5a17baf/href">https://medium.com/media/e4e4a2568c68c1c8ca81bcb9d5a17baf/href</a></iframe><p>When the value inside the DOM change, it does not re-render the component. The value can only be accessed when the onSubmit button press. Using the Uncontrolled can reduces component re-render, this can be an advantage in a certain context, for example, the UI has a simple form, which validation is done only when submit.</p><h3>Conclusion</h3><p>Controlled inputs are a recommended way by React to handling form, but this does not mean that we should or always use Controlled way. Both Controlled and Uncontrolled have advantages, we could pick one or another in a specific situation. If the UI form is quite simple or it&#39;s validating form when submitted, then Uncontrolled with refs is enough, or if the UI form requires a specific condition when it can hide/unhide/disable button, then we should use Controlled instead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6gdXk7SPtxVZQuSAU41QIw.png" /><figcaption>Controlled &amp; Uncontrolled advantages based on context</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa707c759967" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make your code more “Human Readable” with JSDoc]]></title>
            <link>https://medium.com/@raufpratama/make-your-code-more-human-readable-with-jsdoc-6a89f8267f0c?source=rss-1c3733efe812------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a89f8267f0c</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[node]]></category>
            <dc:creator><![CDATA[Muhammad Rauf Pratama]]></dc:creator>
            <pubDate>Thu, 29 Jul 2021 06:06:14 GMT</pubDate>
            <atom:updated>2021-07-29T08:54:45.475Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FQw7gtQtQUa5AFk7WQQ%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FBounce-TV-reaction-phone-oh-no-Qw7gtQtQUa5AFk7WQQ&amp;image=https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2FQw7gtQtQUa5AFk7WQQ%2Fgiphy.gif%3Fcid%3D790b761105b09365a31bcdc88b589b36b45170182432ed37%26rid%3Dgiphy.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe98b4dfcaaac7164b7935397708da1b/href">https://medium.com/media/fe98b4dfcaaac7164b7935397708da1b/href</a></iframe><p>Javascript is known as <em>“weakly typed”</em> language, there are no strict rules when you build your app with it. Developers are free to code and implement the logic with their minds. Well, the world still looks beautiful until you got a legacy project without docs or even comment in its code, feels like becoming an archeologist try to understand ancient language, it will got us much time to understand.</p><p>One of many ways to solve this is using <a href="https://jsdoc.app/"><strong>JSDoc</strong></a><strong>. </strong>As it says in the official docs of JSDoc</p><blockquote>JSDoc 3 is an API documentation generator for JavaScript, similar to Javadoc or phpDocumentor</blockquote><h3>1. Declare</h3><p>JSDoc already built in, to start declare JSDoc we just have to put comment like below exactly before ourvariable, function, and any other primitives.</p><blockquote>/** */</blockquote><h4>a. @author</h4><p>let say for example we got a task in authorization, and we want notice other developers, that we are the PIC’s of its, using the <strong><em>@author</em> </strong>tag:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UTfOFEyfy-FD8RflnYImeQ.png" /><figcaption>JSDoc declaration with <strong>@author</strong> tag</figcaption></figure><p>it will display at description of the suggestion about <strong>foo</strong> function we created, we also can add extra description about the function.</p><h4>b. @param</h4><p>it tells you what type of param can be pass to the function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uecdL3CTuCf_J5_DyOVDnA.png" /><figcaption>JSDoc declaration using <strong>@param tag</strong></figcaption></figure><p>we can pass any javascript type as reference but unfortunately it won’t display on function suggestion we might pass extra description.</p><h4>c. @returns &amp; @example</h4><p><em>@return</em> tag represent what type this function return, while <em>@example</em> tag is telling us how we call the function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uKEYXSnFI5bMPaMOHW33hg.png" /><figcaption>JSDoc declaration using <strong>@returns</strong> &amp; <strong>@example</strong> tags</figcaption></figure><h3>2. Generate JSDoc to HTML</h3><p>There’s a lot other useful <a href="https://jsdoc.app/"><strong>JSDoc block tags</strong></a>, and could make your code more readable by other developers, The tags i mention before is just common tag which in my case i think it’s enough.</p><p>JSDoc also have a capability turn all the declaration into HTML which display an UI that describe about function, object, etc. First you need to install the <a href="https://www.npmjs.com/package/jsdoc">jsdoc html generator</a>, and just run:</p><blockquote>jsdoc &lt;.js&gt;</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gLaZc1kWEURq6fKmnZdazA.png" /><figcaption>Generated with jsdoc</figcaption></figure><h3>3. Conclusion</h3><p>Well, in the context of documenting our code there are many ways to solve it, such as using <a href="https://eslint.org/"><strong>ESLint</strong></a>, <a href="https://docusaurus.io/"><strong>Docusaurus</strong></a>, or any other third party libraries. Or even we can just use <a href="https://www.typescriptlang.org/"><strong>Typescript</strong></a> for clean and robust code, yet we still can use JSDoc even if it’s not significant for documenting using Typescript. For documenting Typescript there’s “JSDoc” like but with more advance capability called <a href="https://typedoc.org/"><strong>TypeDoc</strong></a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a89f8267f0c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>