<?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: Catur Wicaksono</title>
    <description>The latest articles on DEV Community by Catur Wicaksono (@catur).</description>
    <link>https://dev.to/catur</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%2F653758%2F33cebc06-6b0d-4e95-b14e-f1738143f14e.jpg</url>
      <title>DEV Community: Catur Wicaksono</title>
      <link>https://dev.to/catur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/catur"/>
    <language>en</language>
    <item>
      <title>Simple Encode And Decode With Javascript</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Tue, 14 Feb 2023 01:34:03 +0000</pubDate>
      <link>https://dev.to/catur/simple-encode-and-decode-with-javascript-od6</link>
      <guid>https://dev.to/catur/simple-encode-and-decode-with-javascript-od6</guid>
      <description>&lt;p&gt;In this short article, we will try to encode and decode a string using JavaScript easily and practically. Sometimes we are faced with conditions for encoding and decoding when coding. Javascript has several methods for doing this, but in this short article, we'll try to do it easily and practically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Base64
&lt;/h2&gt;

&lt;p&gt;One method that is quite popular for encoding and decoding a string is base64. For those who are familiar with the PHP language, this method seems familiar. The question now is, how to do it using javascript. Here's how to do it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ms023a7o7sjb0dfpld6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ms023a7o7sjb0dfpld6.png" alt="Decoding and encoding with base64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the method above is how to do encoding and decoding using javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hex (Alphanumeric)
&lt;/h2&gt;

&lt;p&gt;Using base64, the results that appear may contain special characters, such as the "=" character. If we want the result without using special characters, we can use another method, namely with hex. Here's how to do encoding and decoding using the hex method:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1gh5g52x0kju3einmczz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1gh5g52x0kju3einmczz.png" alt="Decoding and encoding with hex"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the method above is how to do encoding and decoding using hex in javascript. Using this method, the encoding results will be free of special characters, and produce alphanumeric results.&lt;/p&gt;




&lt;h2&gt;
  
  
  Epilogue ☕
&lt;/h2&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwve1hh3j8ewl5aowo7r.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>What's The Difference Between Tailwindcss and UI Framework (Bootstrap, etc.)</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Sat, 10 Dec 2022 01:30:16 +0000</pubDate>
      <link>https://dev.to/catur/whats-the-difference-between-tailwindcss-and-ui-framework-bootstrap-etc-aaf</link>
      <guid>https://dev.to/catur/whats-the-difference-between-tailwindcss-and-ui-framework-bootstrap-etc-aaf</guid>
      <description>&lt;p&gt;In this short discussion, we will briefly discuss what Tailwind CSS is and how it is different from the UI kit components framework that we often use, such as Bootstrap, Material UI, and so on. Tailwind is basically the same as a general CSS framework, but this library is built at a lower level than a UI kit framework. With this concept, Tailwind CSS is starting to be very popular among developers, especially front-end developers, because they can develop the appearance of their applications more flexibly according to their wishes.&lt;/p&gt;

&lt;p&gt;In contrast to UI kits framework such as Bootstrap, Tailwind CSS does not provide instant components. For example, in Bootstrap to make a button, we only need the &lt;code&gt;btn&lt;/code&gt; class and the class to determine the variant of the button we want, for example the primary button, we can use &lt;code&gt;btn-primary&lt;/code&gt;, and the element will automatically become a beautiful button element, of course with Bootstrap style. Notice the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F631mxa27rhqtwdc03vbp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F631mxa27rhqtwdc03vbp.png" alt="Create a button using Bootstrap" width="800" height="94"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above will display a display like the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyski4a0k40gepzwctl34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyski4a0k40gepzwctl34.png" alt="Create a button using Bootstrap" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using Bootstrap, we can easily make a button look beautiful, just using a class.&lt;/p&gt;

&lt;p&gt;Tailwind can also do the same as Bootstrap above. Because Tailwind is built with a low level concept, it is necessary to unite enough classes to make the button look like what Bootstrap did above. Using Tailwind, we need to assemble a class because in Tailwind there is no instant class that can instantly form a button component instantly. Look at the example in the line of code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs7k0d8xg6pw67vmbohx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs7k0d8xg6pw67vmbohx.png" alt="Create a button using Tailwind" width="800" height="94"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above will display a display like the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frpu2r3d8ggdnjpjfn2ss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frpu2r3d8ggdnjpjfn2ss.png" alt="Create a button using Tailwind" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we pay attention, Tailwind requires a series of classes to form a button appearance which is quite similar to what Bootstrap does. From the two examples above, we will be able to understand more clearly about Tailwind and Bootstrap.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which One Is Better
&lt;/h2&gt;

&lt;p&gt;Based on our experience using the Tailwind and UI kit components framework, we can't definitively say which is the better of the two. Both of these libraries have their own advantages and disadvantages. In this case we will discuss Tailwind and Bootstrap which represent UI kits. Using Bootstrap, it is very easy for us to create a component instantly, and it will speed up the work on the front-end of our application, but this sacrifices flexibility. We can't be flexible in making UI components, to be more flexible, we need to do custom CSS outside of the Bootstrap class itself.&lt;/p&gt;

&lt;p&gt;Using Tailwind, maybe we need to work more to make a certain component name, but this is precisely what front-end developers like a lot. Front-end developers love the concept of Tailwind because they can get creative with creating their own views. Meanwhile, Tailwind also provides a feature to extract existing classes to combine them into new classes. Take a look at the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mi7u4uu30h1fbbmxsts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mi7u4uu30h1fbbmxsts.png" alt="Tailwind class extraction example" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frixjtax166g2vu00mnmk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frixjtax166g2vu00mnmk.png" alt="Tailwind class extraction example" width="800" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above will display a display like the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxnpymi120kltx13bs8f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxnpymi120kltx13bs8f.png" alt="Tailwind class extraction example" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first picture above is that we extracted the component from the Tailwind class by creating a new class. In the second picture we implement our new class as a result of the component extraction in the second button example. In this way, we can almost use Tailwind which is similar to Bootstrap, but more flexible and can be customized easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion and Tips
&lt;/h2&gt;

&lt;p&gt;From our discussion above, at least we can distinguish between Tailwind and the CSS framework with the UI Kit concept. In its use, which one will we choose, depending on our needs. Based on experience in building application front-ends, it is highly recommended to combine the UI framework kit with Tailwind. The UI Kit framework is used to generate components so that we can create views quickly, and Tailwind as a component for customizing our UI components. For example we can use Bootstrap or Material UI (MUI) as a component generator, and Tailwind to do custom views.&lt;/p&gt;




&lt;p&gt;Epilogue ☕&lt;/p&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwve1hh3j8ewl5aowo7r.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>React Efficiency with React.memo</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Wed, 07 Dec 2022 08:14:50 +0000</pubDate>
      <link>https://dev.to/catur/react-efficiency-with-reactmemo-3moc</link>
      <guid>https://dev.to/catur/react-efficiency-with-reactmemo-3moc</guid>
      <description>&lt;p&gt;In this article, we will discuss one of the tools provided by React to make our React application lighter and more efficient. We will discuss React.memo, this is different from the useMemo Hook in React. In short, useMemo is used to retain a value to avoid unnecessary re-rendering, for more details about useMemo, you can refer to the following article:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/catur/reactjs-using-hook-usememo-5a73"&gt;React useMemo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React.memo is different from useMemo, React.memo is useful for maintaining child components to avoid unnecessary re-rendering. For further discussion, we will use a case study example. First, please consider the following code sample.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4QLPsddd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zycbzv66pbphd0ftzha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4QLPsddd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zycbzv66pbphd0ftzha.png" alt="React component: an example of using react memo" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I55inAYo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxk1ix4rpz1qs0ckjd2u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I55inAYo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxk1ix4rpz1qs0ckjd2u.png" alt="React component: an example of using react memo" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The sample code above, when we run it, will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zgiX6tjf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm3okzedfgb5v8jfw19g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zgiX6tjf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm3okzedfgb5v8jfw19g.png" alt="React application: an example of using react memo" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the simple application above, we have two different components, named ParentComponent and ChildComponent which are in separate files, ChildComponent will be loaded in parentComponent (line 21). Each component has its own state and has a function for setState which is used in the onClick event of each component, in the parentComponent on line 10, and the childComponent on line 9.&lt;/p&gt;

&lt;p&gt;If we take a quick look, it seems that there is nothing wrong with the results of the code above, but if we pay closer attention, we will find something wrong. We try to open the console in our browser, for firefox you can press the ctrl + shift + k keys, if successful, a display will appear as follows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HTfPYHwM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jadbh8wc47760tb9jgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HTfPYHwM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jadbh8wc47760tb9jgx.png" alt="React application: an example of using react memo" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pay attention to our browser console, there will be written "Parent Component Rendered" and "Child Component Rendered". The log indicates that there is rendering of the ParentComponent and ClientComponent.&lt;/p&gt;

&lt;p&gt;If we press the "Increase Parent State Value" button, you will see changes to the parent state value. If we look at the log in our browser console, we will get a log like the following image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--os34cT4D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28owujjiaqivjldv19do.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--os34cT4D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28owujjiaqivjldv19do.png" alt="React application: an example of using react memo" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can note that in the console there are logs "parent function fired", "Parent Component Rendered" and "Child Component Rendered".&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;“parent function fired” log: indicates that the setStateHandler function in the parent component has been executed.&lt;/li&gt;
&lt;li&gt;“Parent Component Rendered” log: indicates that the ParentComponent has been rendered.&lt;/li&gt;
&lt;li&gt;“Child Component Rendered” log: indicates that ChildComponent has been rendered.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The thing that needs to be considered is, when we press the "Increase Parent State Value" button, what actually changes is the state in the ParentComponent, under normal conditions, what should be re-rendered is only the ParentComponent, but in the simple application example above, the ClientComponent follows. is re-rendered, while no changes are made to the ClientComponent. Re-rendering this ClientComponent is an unnecessary action.&lt;/p&gt;

&lt;p&gt;Treating this re-rendering of ClientComponent may not significantly affect the performance of this simple example application, because not so many resources are used. This will only be felt if the ClientComponent uses a lot of resources, for example making requests to the API and rendering large amounts of data, or doing large amounts of calculations. Unnecessary re-rendering like this must be handled, so that the application will run efficiently and effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using React Memo
&lt;/h2&gt;

&lt;p&gt;React has provided facilities to deal with problems as described earlier. To solve this problem we can use the memo method in the react library. For implementation using this memo method, we can pay attention to the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V4nWiN3d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qp4scp1m811jfche8ydb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V4nWiN3d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qp4scp1m811jfche8ydb.png" alt="React component: an example of using react memo" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use the memo method when exporting the component as in the example above at line 26.&lt;/p&gt;

&lt;p&gt;After using the memo method, react will retain the ClientComponent, and guard against unnecessary re-renders. As long as there are no changes to the ClientComponent, the ClientComponent will not be rendered again. In our previous simple application, let's pay attention to the console, the following is the display of the browser console in our simple application at the start of the render.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CozZS-19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3ufzuhbr8q11vtm02hq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CozZS-19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3ufzuhbr8q11vtm02hq.png" alt="React application: an example of using react memo" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above shows that nothing has changed from the previous render, we can still see that we can see the "Parent Component Rendered" and "Child Component Rendered" logs. The log indicates that there is rendering of the ParentComponent and ClientComponent.&lt;/p&gt;

&lt;p&gt;We can see the difference, if we press the "Increase Parent State Value" button, and if we look at our browser console, we will find a log like the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8k6-418V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/woinswcuo4edt7ll71h6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8k6-418V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/woinswcuo4edt7ll71h6.png" alt="React application: an example of using react memo" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we pay attention, we can no longer find the "Child Component Rendered" log. The results of the log indicate that the ClientComponent has not been rendered again when pressing the "Increase Parent State Value" button. Thus we have succeeded in preventing unnecessary re-rendering of our simple application. With this technique, our application will become more efficient and more effective.&lt;/p&gt;




&lt;p&gt;Epilogue ☕&lt;/p&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CQvhqaK6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>ReactJs using Hook useMemo</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Sun, 06 Nov 2022 00:33:48 +0000</pubDate>
      <link>https://dev.to/catur/reactjs-using-hook-usememo-5a73</link>
      <guid>https://dev.to/catur/reactjs-using-hook-usememo-5a73</guid>
      <description>&lt;p&gt;This article will discuss one of the ReactJS Hooks that is quite important for React developers to understand, namely useMemo. The useMemo hook is used to make our code more efficient, but there are still many React developers, especially beginners, who haven't implemented it yet. In simple terms, if we add a value to useMemo, then that value will be remembered and will not be re-rendered in case of re-rendering until certain conditions have been determined when the value must be re-rendered.&lt;/p&gt;

&lt;p&gt;We will try to discuss this further by using an example, consider the following code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbnfo3zjkydkaee7ptqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbnfo3zjkydkaee7ptqk.png" alt="Sample code without using useMemo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in the code example above we try to create a function called &lt;code&gt;expensiveProcessing&lt;/code&gt;(line 11), imagine this function performs a very heavy process, but this time, this function only performs simple calculations. In the function, we add console.log() to log in to our browser console later, as a marker that this function is executed and performs calculations. After finishing creating the &lt;code&gt;expensiveProcessing&lt;/code&gt; function, then we run and store the return value of the &lt;code&gt;expensiveProcessing&lt;/code&gt; function in the value variable (line 16), then display it in the h1 element tag on (line 21).&lt;/p&gt;

&lt;p&gt;In the next section, we create a button element (line 22) that executes the &lt;code&gt;stateChangeHandler&lt;/code&gt; function (line 7). The &lt;code&gt;stateChangeHandler&lt;/code&gt; function is used to increase the state value with a value of 1, using the setState function. The value of the state we display in the button element, so that every time the button is pressed, the sentence in the button will display the number of times the button has been pressed, taken from the state value. From the code example above, if we run it, it will more or less look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6p72wk9pe80tw2p8p9df.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6p72wk9pe80tw2p8p9df.png" alt="Code display example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The display in our previous sample code involved is quite simple and simple as above, at a glance nothing is wrong, let's take a deeper look at what is wrong with the results of our code above. From the results of running the code above, we open the console in our browser, for those who use firefox can display with the shortcut &lt;code&gt;ctrl + shift + k&lt;/code&gt;. To be more clear, let's try to refresh the page. After we successfully refresh we will find a log that reads "expensive Processing function has run" which indicates that the &lt;code&gt;expensiveProcessing&lt;/code&gt; function has been run. The display console will more or less look like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxe2v7ok66qcq05anh45t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxe2v7ok66qcq05anh45t.png" alt="Code display example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;note: there may be confused why there are 2x "expensiveProcessing function has run" which indicates the function has been executed twice in every page refresh. This happens because in index.js we run our App component on &lt;code&gt;&amp;lt;React.StrictMode&amp;gt;&lt;/code&gt; which causes setState to run twice in development mode. In this case, this is not a problem, we just need to understand the difference&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We have successfully run the above simple application and displayed the console in the browser, but so far nothing seems to be wrong. Now it's time to take a closer look at our simple application. If we try to press the "click" button we will again see the log on the console that reads "expensiveProcessing function has run" again. This happens because the &lt;code&gt;expensiveProcessing&lt;/code&gt; function is restarted after we press the “click” button, and this means that our function is recalculated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktpvo5be8950h29mmymr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktpvo5be8950h29mmymr.png" alt="Code display example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This process may not be very noticeable to us in this simple application, but it will be very annoying when our application is very complex and related to many resources, and makes our application very heavy. Then we might ask, why did this happen? We can see that the button's click event is handled by the &lt;code&gt;clickHandler&lt;/code&gt; function, which does setState. When we setState on our app, our app will re-render. This causes functions that are in the same component and their descendants to be executed again.&lt;/p&gt;

&lt;p&gt;The more serious question now is, how to solve this? React seems to have anticipated this, thus providing a useMemo Hook that serves to maintain a value, so that if a value does not change, it will not be reprocessed during a re-render, unless it is needed. This facility will be very useful for us to make our application lighter and more efficient. Next, we will immediately try to practice using it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using useMemo
&lt;/h2&gt;

&lt;p&gt;To solve the above problem by using useMemo, we can do it as in the following code sample:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7lsoq2m4p8r8r8dapbow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7lsoq2m4p8r8r8dapbow.png" alt="sample code using useMemo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;notice in the code sample above, we wrapped the &lt;code&gt;expensiveProcessing&lt;/code&gt; function into useMemo, that's all we need to do. The way to use useMemo is no different from using useEffect or useCallback, only includes a callback function in the first parameter, and an array of data in the second parameter, which is useful for marking when the function in useMemo needs to be reprocessed.&lt;/p&gt;

&lt;p&gt;After we have successfully implemented useMemo as above, now we try to run the sample code above, in the same way as before, and it will look like in the following image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frem9xc714akkanxkhn32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frem9xc714akkanxkhn32.png" alt="Code display example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first glance, there is nothing different from the time before we used useMemo before, but let's take a closer look. If we try to press the click button, we will be able to see that the state has changed marked by the number on the button will continue to grow, but pay attention to our console, there are no more logs like before. With no more logs in our console, it indicates that the &lt;code&gt;expensiveProcessing&lt;/code&gt; function has been protected from the effects of re-rendering from setState.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6g1lrg14uj1kdycfn7nc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6g1lrg14uj1kdycfn7nc.png" alt="Difference when using useMemo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The second parameter in useMemo
&lt;/h3&gt;

&lt;p&gt;If we notice, useMemo has two parameters, the first is a callback where we run our function, and the second is to indicate when the function in useMemo should be run again. If we modify our last useMemo to look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F111b4a48e7uo4owixsbj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F111b4a48e7uo4owixsbj.png" alt="useMemo by using the second parameter"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;so our code will be like this now:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvm851hrof8rh7olt84hf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvm851hrof8rh7olt84hf.png" alt="useMemo by using the second parameter"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we add the state to the array in the second parameter, like this, useMemo will mean, for every state, there is a change, then the function in useMemo will be rerun. If we run our simple example application using the modifications as above, we will find that every time we press the button, we will find a log on our console "expensiveProcessing function has run" which indicates that the &lt;code&gt;expensiveProcessing&lt;/code&gt; function is re-run every time the button is pressed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl43xzm3huf6jjpym6ebd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl43xzm3huf6jjpym6ebd.png" alt="Code display example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This happens because we know that every time a button is pressed, the onClick event will run the &lt;code&gt;buttonHandler&lt;/code&gt; function which will change the state using setState, it will cause the state value to change, then useMemo will respond. The system is similar when we use useEffect.&lt;/p&gt;

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

&lt;p&gt;The useMemo hook in React is a tool that helps us to make our code even more efficient, by preserving a value in our application so that it is not reprocessed during a re-render of our application. By using useMemo we can also determine when a value needs to be reprocessed, thus making the value more dynamic.&lt;/p&gt;




&lt;p&gt;Epilogue ☕&lt;/p&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwve1hh3j8ewl5aowo7r.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Best Implementation for Form Fields in ReactJS</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Sat, 29 Oct 2022 04:05:32 +0000</pubDate>
      <link>https://dev.to/catur/best-implementation-for-form-fields-in-reactjs-3mhf</link>
      <guid>https://dev.to/catur/best-implementation-for-form-fields-in-reactjs-3mhf</guid>
      <description>&lt;p&gt;This article will discuss the best implementation of creating a form field with React JS. The focus of this discussion is how to overcome the state in our form field. Often React JS programmers make mistakes that they may not even notice, but which affect the performance of our applications.&lt;/p&gt;

&lt;p&gt;When creating fields in React JS forms, many recommend adding a listener to each field and setState on each field's onChange event. For example, consider the following line of code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zYDQWaZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04xuxuxrlsblyd2ayprg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zYDQWaZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04xuxuxrlsblyd2ayprg.png" alt="Typical use of setState" width="800" height="890"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The picture above is a simple use of setState on the onChange event on the field, we add a function to handle the event on the field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FJyW3Q4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lly3v4pt4h83h8g93c3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FJyW3Q4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lly3v4pt4h83h8g93c3.png" alt="Typical use of setState" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then in the function, we change the state by using the name property in the field obtained from &lt;code&gt;e.event.name&lt;/code&gt; with the value obtained from the field value obtained from &lt;code&gt;e.target.value&lt;/code&gt;. For those who don't know, why to use a callback function when using setState, you can read the following article:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/catur/best-implementation-of-setstate-in-reactjs-159k"&gt;Best Implementation of setState in ReactJs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to use it like this is very common. Let's note once again, using setState on onChange in the field will cause the rendering of elements in every single letter that the user enters into the field.&lt;/p&gt;

&lt;p&gt;By far one of the best recommendations for building applications using React Js, we must make our application as efficient as possible by minimizing components to be re-rendered if there is no need to re-render.&lt;/p&gt;

&lt;p&gt;Thus, using setState on onChange is not an efficient method, and we need to avoid it. The next question arises, then how to replace this method? In this article we will discuss, there are at least two methods to do it, firstly we can use the useRef, and secondly, we can use debounce or throttle method on our onChange field event. More about debounce can read the following article:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/catur/debounce-and-throttle-3kel"&gt;Debounce and Throttle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will discuss them one by one in the following.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Method, Using useRef
&lt;/h2&gt;

&lt;p&gt;In this first method, we will solve the above problem by using the useRef hook. From the previous line of code, we change to be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rhnc7ttX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gf7tmjfcrc85yllup554.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rhnc7ttX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gf7tmjfcrc85yllup554.png" alt="Use of setState by using useRef" width="800" height="690"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above we use useRef to address our field. The idea is very simple, we just need to make a ref using useRef&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Jf-g5c6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drh8by9mocf8bwx222yc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Jf-g5c6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drh8by9mocf8bwx222yc.png" alt="Use of setState by using useRef" width="800" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then we add the ref that we have made into the appropriate field&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FTrU8efE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9aet071jonpxp6ym5js.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FTrU8efE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9aet071jonpxp6ym5js.png" alt="Use of setState by using useRef" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then when we will take the value of the field that we have entered the ref in it, we only need to take the properties of the ref as follows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XMaCr2Gd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tu6dulhchkkdcxny502e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XMaCr2Gd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tu6dulhchkkdcxny502e.png" alt="Use of setState by using useRef" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;to retrieve the value from the ref we use the method &lt;code&gt;nameRef.current.value&lt;/code&gt;. Then it's up to us what we will use it for.&lt;/p&gt;

&lt;p&gt;By using this method, looks shorter and easier, compared to the previous method, and is certainly more efficient because it does not involve setState in the process of inputting data into the field, which means there is no re-rendering, and each user types every letter in the field, no no events are triggered.&lt;/p&gt;

&lt;h2&gt;
  
  
  The second Method, Using Debounce or Throttle
&lt;/h2&gt;

&lt;p&gt;Sometimes we need to monitor changes in our text field to trigger an event, for example when we create an autocomplete field, example on the google.com search page when we type a few letters there, a dropdown will appear that gives us search suggestions, even though we haven't finished in entering words into the field.&lt;/p&gt;

&lt;p&gt;To monitor changes in our text field, it is not recommended to do every letter typed, it would be more efficient for us to do it periodically using throttle or after the user has finished typing using debounce, for those who do not understand what debounce and throttle are, can read the article the following first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/catur/debounce-and-throttle-3kel"&gt;Debounce and Throttle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will try to use the throttle and debounce to solve this problem. To use the throttle or debounce, in this article we will use the lodash library. If we have not added the lodash library to our application, we can add it using the following command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_Ap0oMfA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l31w7l5x2lrpyusbtl88.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_Ap0oMfA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l31w7l5x2lrpyusbtl88.png" alt="Npm lodash" width="800" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we have successfully added lodash, we are ready to use the throttle or debounce.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Throttle
&lt;/h3&gt;

&lt;p&gt;Using throttle, the event function will be executed periodically when the user types letter by letter in our text field. To use the throttle to solve the problem in the previous code sample, consider the following code sample:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M6JBHL9L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bdc87907zs5smlvcu0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M6JBHL9L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bdc87907zs5smlvcu0g.png" alt="Using throttle method" width="800" height="948"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in the code example above, it is very similar to using setState in the previous onChange event, only the difference is that we wrap setState in the throttle function callback&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IvB-d8rZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1oq3usadp5zyshmc5ox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IvB-d8rZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1oq3usadp5zyshmc5ox.png" alt="Image description" width="800" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and we set the time to 1000 ms or 1 second, we are free to enter any time config (calculated in milliseconds), then we enter the function into the function that will be executed on the onChange event in the field&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eZcmMfTq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4ybk5las1p3gr0jwyi1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eZcmMfTq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4ybk5las1p3gr0jwyi1.png" alt="Image description" width="800" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;by configuring 1000 ms or 1 second on the throttle function, it means that the setState function will run for one second as long as the user is still typing something into our field.&lt;/p&gt;

&lt;p&gt;By using this method, we will save rendering on our components, and we can run certain functions as long as the user is still typing something into the text field, such as displaying dropdown suggestions or so on.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;note: in the code example above, we use useCallback to define the throttle function, this method aims also for efficiency we can just eliminate the use of useCallback in the above method. We will discuss useCallback in the next article.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Debounce
&lt;/h3&gt;

&lt;p&gt;Using debounce, the event function is not immediately executed if the user is still typing something into our field. The new event function will respond sometime after the user no longer types anything into our field. To troubleshoot the problem with the previous sample code, consider the following code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GQHVIRpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/296b5rpeyyscjilp75to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GQHVIRpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/296b5rpeyyscjilp75to.png" alt="Image description" width="800" height="938"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The way to use debounce is very similar to throttle, we just need to wrap the setState function into the callback function debounce like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VaWCyLdh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sypk2qjjk5cpqucknx00.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VaWCyLdh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sypk2qjjk5cpqucknx00.png" alt="Image description" width="800" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then we set the time to 1000 ms or 1 second, we are free to enter any time config (calculated in milliseconds), then we enter the function into the function that will be executed on the onChange event in the field&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O_V7QBmY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41msjugcrte0iezrm3h9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O_V7QBmY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41msjugcrte0iezrm3h9.png" alt="Image description" width="800" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in the above line of code, we delay our event listener one second after the user no longer types anything into our field, if the user is still typing with a tempo of less than one second, the function on change will still be held.&lt;/p&gt;

&lt;p&gt;By using this debounce, we have made our program code much more efficient, and it is no longer a problem if our application has grown quite large and complex.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;note: in the code example above, we use useCallback to define the debounce function, this method aims also for efficiency we can just eliminate the use of useCallback in the above method. We will discuss useCallback in the next article.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Epilogue ☕&lt;/p&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CQvhqaK6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Best Implementation of setState in ReactJs</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Wed, 26 Oct 2022 03:34:26 +0000</pubDate>
      <link>https://dev.to/catur/best-implementation-of-setstate-in-reactjs-159k</link>
      <guid>https://dev.to/catur/best-implementation-of-setstate-in-reactjs-159k</guid>
      <description>&lt;p&gt;This short article will try to give recommendations for when we will use React Hooks setState in our ReactJs application. For application developers who use ReactJs as an SDK, we will be very familiar with the name React Hooks in this discussion is the setState Hook, moreover, in the latest React version we have been strongly directed to use function components in creating components, then useState will become mandatory for we understand.&lt;/p&gt;

&lt;p&gt;This useState React Hook is a mechanism that allows us to manipulate the state of function components, instead of setting the state on class components, which are now deprecated.&lt;/p&gt;

&lt;p&gt;Many React developers develop their apps in their own best way. This time we will also discuss one technique in using setState. First, we will create a simple application to increase and decrease a number, as in the following code sample:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u-ukog8r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuvbxh6gjv9m8rk67ihf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u-ukog8r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuvbxh6gjv9m8rk67ihf.png" alt="Code Example" width="800" height="704"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above component is a simple application to increase and decrease the number stored in the state, and manipulated in the increaseNumberHandler function, and the decreaseNumberHandler function, which is called on the onClick event of the button.&lt;/p&gt;

&lt;p&gt;In this discussion, it is necessary to pay attention to the function section for state manipulation, one of which is the following increaseNumberHandler function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j_lZgkL1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bomxedhq2cx38vyelng9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j_lZgkL1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bomxedhq2cx38vyelng9.png" alt="Code Example" width="800" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The example code above is a way of using useState in general and is still widely practiced by programmers, but it should be noted that this method is not a good implementation, and has many weaknesses. First, how to use useState like this, causes an infinity loop when implemented into useEffect, and this sometimes goes undetected and has the potential to crash our application due to a memory leak. For a more complete discussion of this, can read the following article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/catur/best-implement-setstate-on-useeffect-3je9"&gt;Best Implement setState on useEffect&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second weakness is in this way it will be very inconvenient if we want to process the state in the same function, consider the following code example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mq5qejca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7bv828f0uy18ox17s4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mq5qejca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7bv828f0uy18ox17s4a.png" alt="Code Example" width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With layman's logic, we assume that the above function will increase the number by a multiple of 2, but if we run the code above, the function will still increase by a multiple of one. This will be quite confusing, and will probably make our program code have to be modified in such a way that it might make our program code inefficient. Guess will try to discuss how to solve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callback Function
&lt;/h2&gt;

&lt;p&gt;One way to do setState is to use the callback function on setState. consider the following code example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oK0s6dhn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5dj5bq9b2n1iy0l0n9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oK0s6dhn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5dj5bq9b2n1iy0l0n9m.png" alt="Code Example" width="800" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;when we use the callback function on setState, the callback function will receive the current state parameter, and return the desired new state value. Next, we will try to use the setState method using this callback, in states with primitive data and also in states with data in the form of objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  setState With Primitive Data
&lt;/h3&gt;

&lt;p&gt;For an example of using a set state using primitive data, we will try it using the previous code example, because in the previous code example we also use primitive data in the form of integers. To do the same thing in the previous example, we can do it in the following way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a7evsBbR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feafm1qwadkug3n7z2st.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a7evsBbR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feafm1qwadkug3n7z2st.png" alt="Code Example" width="800" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thus we can solve the problem in the previous session. Because by using a technique like this, we can set any state we want in the same function, but the state will be updated every time we set the state, even though it's in the same function scope, consider the following example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0UiPe1OO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8ivu53yxpmjtjgb0s4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0UiPe1OO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8ivu53yxpmjtjgb0s4q.png" alt="Code Example" width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;by using the above technique, the increase Number Handler function will increase the number value to a multiple of two. Here are the changes we have made:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d4zuV7z---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6tg8ywl5y3hfias03ha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d4zuV7z---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6tg8ywl5y3hfias03ha.png" alt="Code Example" width="800" height="704"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  setState With Data Object
&lt;/h3&gt;

&lt;p&gt;The previous example is how to use useState on primitive data, this time we will try to use setState by using object data. To do this, we will first create a simple application that only serves to display the results of input data, as in the following example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QL3pXc_e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahsr8uix3ngd7jkcyyqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QL3pXc_e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahsr8uix3ngd7jkcyyqu.png" alt="Code Example" width="800" height="723"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above component will display a paragraph containing the values of the state name and address which can be manipulated by the nameHandler and addressHandler functions that are called on the onChange event of the input element. In this discussion session, take a look at the functions to manipulate the state, one of which is the nameHandler function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s42KDd3v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1es5oj6m5ulcpx6tcdm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s42KDd3v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1es5oj6m5ulcpx6tcdm.png" alt="Code Example" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code example is how we try to change the name property on our state object by using the input field value retrieved using e.target.value. This setState method is the most common, namely by copying the value of the object in the current state, using …prevState, then replacing the desired property value with a new value. To change the value of another property, namely address, we can use the same method, only changing the name of the property to be set with the address, accompanied by the value to be replaced, as in the other handler function, the addressHandler function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--thWFDiWg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owdebgmceauonooe411e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--thWFDiWg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owdebgmceauonooe411e.png" alt="Code Example" width="800" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;To setState on the useState hook in our react app, it's highly recommended to always use the callback function and get used to it. Although it looks a bit longer even to change the state a little, this method can protect us from errors in our system later, which we don't want. To setState using primitive data we can do like the following schema:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T_X8ijYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqc0ooj87myypp95mbef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T_X8ijYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqc0ooj87myypp95mbef.png" alt="setState by using callback function with primitive data" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To setState using a data object we can do something like the following schema:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1EajVrX0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0931c8bei4p096paw6sh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1EajVrX0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0931c8bei4p096paw6sh.png" alt="setState by using callback function with object data" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Epilogue ☕&lt;/p&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CQvhqaK6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Best Implement setState on useEffect</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Fri, 21 Oct 2022 03:32:45 +0000</pubDate>
      <link>https://dev.to/catur/best-implement-setstate-on-useeffect-3je9</link>
      <guid>https://dev.to/catur/best-implement-setstate-on-useeffect-3je9</guid>
      <description>&lt;p&gt;This time we will discuss the best implementation in using setState in useState in useEffect in React. React developers will often be faced with situations where they have to setState on useEffect, such as when we want to load data from the database and want it to be stored in a state, then we have to retrieve the data in the database and save the query results to the state using useEffect. However, this implementation often creates problems that are sometimes not realized and can have fatal impacts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation of setState on useEffect
&lt;/h2&gt;

&lt;p&gt;On this occasion, we will discuss states with primitive data values (integers, strings, booleans, etc.) and states with data values in the form of objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  State with primitive data
&lt;/h3&gt;

&lt;p&gt;In a state that is implemented using primitive data, it's actually not that complicated and has been widely exemplified in various forums and React's own documentation, here's the implementation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhijdrye7lzeso54331bw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhijdrye7lzeso54331bw.png" alt="State with primitive data"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  State with a data object
&lt;/h3&gt;

&lt;p&gt;Then for the state that is implemented using object values, it is a little complex but powerful enough for various needs. An example of an initialized state using an object value is as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr5aum9kmtlejqe2gb2n1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr5aum9kmtlejqe2gb2n1.png" alt="State with a data object"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Case study
&lt;/h3&gt;

&lt;p&gt;In this discussion, we will use states with object values that are more complex than primitive values. Hopefully, if you can understand this case study, you will also be able to understand states with primitive values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi8erucshi19o96uocdxu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi8erucshi19o96uocdxu.png" alt="Case study"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above explains that we have a state object with three properties: foo, bar, and baz. To update the state value we can use the set state function as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7tghtw8alnymqm2l4qg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7tghtw8alnymqm2l4qg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, if we implement it in our previous source code, we get the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2l8v7b6fkzrg2748zegd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2l8v7b6fkzrg2748zegd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we apply the code as above, then when we run it, there is nothing strange at first glance, but look at the console log developer tools in our browser (or press the F12 key and then navigate to the console window). Note that the following error will occur:&lt;/p&gt;

&lt;p&gt;"Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn5rtkbk3zsavg2i6n527.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn5rtkbk3zsavg2i6n527.png" alt="Error at concole"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The error above is caused by the rendering of repeatedly and continuously and indefinitely or an infinity loop. If left unchecked it will eat up memory and will be fatal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution Step 1
&lt;/h3&gt;

&lt;p&gt;The first solution to the error is as explained in the official react hooks effect documentation, that we need to capture the function in the first argument of the array in the second argument, here's an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3e7b1zrb1p7k16szjf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3e7b1zrb1p7k16szjf6.png" alt="Solution"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Notice the second argument, the array in the second argument is used to accommodate any variables that are monitored for changes, if the variables contained in the array change then useEffect will re-render the existing function on it.&lt;/p&gt;

&lt;p&gt;If we use an IDE such as Visual Studio Code, it will be recommended what variables need to be added to the array, by giving a warning to the array.&lt;/p&gt;

&lt;p&gt;OK, let's fix our function above:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnfflmr9fdex3em4s0h6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnfflmr9fdex3em4s0h6.png" alt="Solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way there will be no more errors like the one above. But notice again, that there is still a warning there (in the developer tools console):&lt;br&gt;
"React Hook useEffect has a missing dependency: 'state'. Either include it or remove the dependency array. You can also do a functional update 'setState(s =&amp;gt; ...)' if you only need 'state' in the 'setState' call react-hooks/exhaustive-deps"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqpyayla7puq2a800ywv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqpyayla7puq2a800ywv.png" alt="Warning at console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This warning appears because we are advised to add a state to the second argument array of useEffect, in this way, useEffect will not re-render its callback function unless there is a change in the state. let's fix our previous code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ez6ajh5ab4a2eg62v55.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ez6ajh5ab4a2eg62v55.png" alt="Solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way we have removed the warning on our console. but there will be an error again like before.&lt;br&gt;
"Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcokgrb7y0prbrju4kbfn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcokgrb7y0prbrju4kbfn.png" alt="Error at console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;p&gt;The error above is an error that occurs because changes and monitoring co-occur, or in the same context, when we use …state then we are making changes to the state, while the state is being monitored for changes by useEffect, so that due to a state change, useEffect re-rendering the callback function, once it is re-rendered, there is another change to the state in the …state section, and it will keep repeating like that, then there will be an infinity loop on useEffect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution Step 2
&lt;/h3&gt;

&lt;p&gt;To solve the above error, we can use a callback to setState in useEffect as described in React's documentation. let's fix our previous code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qqdnb5xm3ylkxvhr0qx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qqdnb5xm3ylkxvhr0qx.png" alt="Solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the callback on setState, the state will no longer be read by useEffect to be monitored for changes, so we no longer need to add state to the array in the second argument. this way there will be no repeated and unlimited rendering, and no warning will appear to add a state to the second argument array in useEffect.&lt;/p&gt;




&lt;h2&gt;
  
  
  Epilogue ☕
&lt;/h2&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwve1hh3j8ewl5aowo7r.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Debounce and Throttle</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Tue, 18 Oct 2022 01:37:26 +0000</pubDate>
      <link>https://dev.to/catur/debounce-and-throttle-3kel</link>
      <guid>https://dev.to/catur/debounce-and-throttle-3kel</guid>
      <description>&lt;p&gt;This article will briefly discuss two techniques in programming to control the execution of a function, this is often applied in an event handler. The more modern this is, the application is made more interactive with the user, for example, if we have an auto-scroll page, which can load data automatically when the user has reached the bottom of our page. Techniques like this, require a scroll detector in our application, to see if the user has arrived at our final page or not.&lt;/p&gt;

&lt;p&gt;For this purpose, we can add an event listener to the scroll event, which calls the function when scrolling occurs on our page. This method is very inefficient, just imagine if in every function in the event listener we perform a fairly heavy action, such as having to re-render the page components, or having retrieve data from the database. In fact, once scrolling on a page, it can trigger many events at once. It could be that for one request there are tens or even hundreds of requests, because of the mouse scroll event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://css-tricks.com/debouncing-throttling-explained-examples/"&gt;David Corbacho&lt;/a&gt; explained that in 2011 there was an issue on Twitter, when we scrolled through our Twitter feed, the application slowed down and was no longer responsive. &lt;a href="https://johnresig.com/blog/learning-from-twitter/"&gt;John Resig&lt;/a&gt; thinks it's not a good idea to put expensive functions into scroll events. So we need a method to solve this kind of problem, and there is a method to hold the execution of a function called debounce and throttle. These two methods are the same, but with different implementations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debounce
&lt;/h2&gt;

&lt;p&gt;Debounce is a method or technique used to hold the execution of a function for a certain period on the event listener after the event listener is no longer responding. The simple analogy is, when a bus stops at the bus stop, the bus will wait for five minutes, if there are passengers on the bus, the bus will wait another five minutes, if there are more passengers before five minutes, the bus will wait again. again for five minutes, and so on. After five minutes there are no more passengers, and the new bus will start.&lt;/p&gt;

&lt;p&gt;The illustration above, if we implement it into programming, we can see the scheme in the following figure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GJoLR2-G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iscoef4b4oruu4pyb58u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GJoLR2-G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iscoef4b4oruu4pyb58u.png" alt="Debounce Illustration" width="800" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the picture above e represents an event, and the above schematic is made to bounce for five seconds. We can note that no matter how many times the event occurs, as long as it is still under the five-second period, the function will be delayed, until the last event, and will wait until five seconds later, if there are no more events, the new function will be executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Throttle
&lt;/h2&gt;

&lt;p&gt;Throttle is a method similar to debounce, except that the throttle does not hold the function on the event listener forever, but the throttle will continue to function at certain intervals as long as the event listener is still responding to an event. If you refer to the previous illustration, when a bus stops at the bus stop and will wait for five minutes, no matter how many people will get on the bus, after five minutes, the bus will start, as well as at the next stop, and so on.&lt;/p&gt;

&lt;p&gt;The illustration above, if we implement it into programming, we can see the scheme in the following figure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aEt1fslR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktf4fg3qrbnevywjbgm4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aEt1fslR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktf4fg3qrbnevywjbgm4.png" alt="Throttle Illustration" width="800" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image above the e represents an event, and the schematic above makes the throttle for five seconds. We can note that no matter how many times the event occurs, as long as it is under a five-second period, the function will still run for five seconds, until the last event, and will wait until five seconds later, if there are no more events, then a new function will run again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Both of these methods will be very efficient if used to handle an event that occurs quickly and tightly, by carrying out the same function. Like handling a scroll event, changing events on a field, and so on.&lt;/p&gt;




&lt;h2&gt;
  
  
  Epilogue ☕
&lt;/h2&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CQvhqaK6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Tips For Using "Nested If" In Programming</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Mon, 17 Oct 2022 02:48:26 +0000</pubDate>
      <link>https://dev.to/catur/do-this-dont-do-that-nested-if-15af</link>
      <guid>https://dev.to/catur/do-this-dont-do-that-nested-if-15af</guid>
      <description>&lt;p&gt;This time we will discuss simple tips for making programs. We can implement these tips in any programming language, but this short article will demonstrate using the Javascript language. The discussion this time is about nested if in a function.&lt;/p&gt;

&lt;p&gt;Sometimes we meet cases that require us to make decisions in decisions, for example, this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8luIvlvh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/867iugwh5tn14m10i5s8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8luIvlvh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/867iugwh5tn14m10i5s8.png" alt="Figure 1 If Hell" width="800" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The example above is that we want to return the string "Hallo" if the signedIn variable is true, and the userId variable is not null. It might seem normal at first glance, but it would be terrible if there were so many condition checks, not just the two in the example above. it will look like "&lt;a href="http://callbackhell.com/"&gt;callback hell&lt;/a&gt;"&lt;/p&gt;

&lt;p&gt;the clean way to do it in the code example above is like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j5er4paq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmdvcicil6clwnoqzme3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j5er4paq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmdvcicil6clwnoqzme3.png" alt="Figure 2 If Hell" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;or in the Javascript language you can also use a short way to be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kb-Cv6Qw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlhb1jdas7fsah9r0pwr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kb-Cv6Qw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlhb1jdas7fsah9r0pwr.png" alt="Figure 3 If Hell" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;by using this method, there are no longer nested if, and avoid "callback hell". This method can also be done in other programming languages with customized modifications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Epilogue ☕
&lt;/h2&gt;

&lt;p&gt;Thank you for reading this article, I hope this article is useful for you, if you learn something new from this article, please provide feedback on this article, or give us criticism, suggestions so that it can be even better. Maybe you want to buy me a cup of coffee?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/catur.dev"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CQvhqaK6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>MeTalk: Pengalaman Dengan Docker Dan Google Cloud</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Mon, 03 Oct 2022 13:51:44 +0000</pubDate>
      <link>https://dev.to/catur/metalk-pengalaman-dengan-docker-dan-google-cloud-e6d</link>
      <guid>https://dev.to/catur/metalk-pengalaman-dengan-docker-dan-google-cloud-e6d</guid>
      <description>&lt;p&gt;Artikel ini adalah sesi berbagi pengalaman dari yang pernah saya alami sebagai developer. Apa yang saya tulis pada artikel ini merupakan opini pribadi.&lt;/p&gt;




&lt;p&gt;Dari waktu ke waktu teknologi terus berkembang, begitu juga dengan bahasa pemrograman. Tahun 2022 ini, mulai banyak tren mengembangkan program aplikasi terutama web service dengan menggunakan bahasa yang beraneka ragam. Seperti Node.Js, Python, Go, dan masih banyak yang lain. Baru-baru ini dan masih dikembangkan adalah dengan menggunakan bahasa Dart, meskipun tujuan utama dari bahasa ini pada awal diciptakan adalah untuk pengembangan UI, namun belakangan muncul library-library yang memungkinkan bahasa Dart untuk melakukan manajemen server dan database.&lt;/p&gt;

&lt;p&gt;Saya sendiri salah satu yang tertarik untuk menggunakan bahasa-bahasa tersebut untuk membuat REST API. Sebelum sekitar tahun 2019-an, untuk melakukan deployment aplikasi web dengan menggunakan bahasa-bahasa ini, sudah didukung CPanel pada shared Hosting, sehingga tidak begitu sulit, kita hanya perlu sedikit konfigurasi, dan deployment aplikasi kita berhasil.&lt;/p&gt;

&lt;p&gt;Sekitar tahun 2018, mulai tren server dengan VPS, dan layanan deployment untuk Node, Typhon, dll. juga sudah mulai dihilangkan dari layana para penyedia hosting. User yang akan melakukan deployment aplikasi-aplikasi tersebut harus menggunakan VPS, yang tentu harganya jauh lebih mahal dari shared hosting pada umumnya. Dengan menggunakan VPS tidak semudah menggunakan shared hosting kita perlu mempersiapkan berbagai hal untuk bisa deploy aplikasi kita dengan menggunakan VPS. Saya salah satu yang cukup direpotkan dengan persiapan penggunaan VPS, meskipun kelebihan yang ditawarkan memang lebih baik daripada shared hosting biasa.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tertama Kali Mengenal Docker
&lt;/h2&gt;

&lt;p&gt;Waktu pertama kali mengenal Docker, saya sedikit ragu, karena saya berpikir itu hanya menambah pekerjaan saja. Namun, pada suatu waktu ada hal yang mengharuskan saya untuk menggunakan Docker, dan mau tidak mau saya belajar cara menggunakanya. Setelah saya mencoba menggunakan Docker, saya langsung berpikir kalau Docker adalah yang saya cari selama ini.&lt;/p&gt;

&lt;p&gt;Selama saya develop aplikasi dengan menggunakan SDK Javascript seperti Node, React, atau Python, Go, dan lain sebagainya, saya selalu mengalami kesulitan dalam hal deployment (tanpa VPS), memang sudah banyak penyedia layanan instan, seperti contohnya Vercel, atau Netlyfy untuk React Js, dan Heroku untuk Node.js, namun harga yang ditawarkan sangatlah mahal, bahkan lebih mahal daripada VPS, karena hanya terbatas untuk deployment aplikasi tertentu, tidak sefleksibel menggunakan VPS.&lt;/p&gt;

&lt;p&gt;Dengan menggunakan Docker, ternyata memudahkan kita untuk melakukan deployment hampir dimana saja kita mau. Contoh, saat ini saya berlangganan Google Cloud, dengan menggunakan Google Cloud, proses deployment hanya menggunakan sedikit config dan aplikasi kita dapat ter-deploy dengan sempurna. Kita hanya perlu push image Docker kita ke registry Google Cloud dan dan selebihnya kita serahkan pada Google Cloud. Hal yang paling penting bagi saya adalah, harganya yang menurut saya jauh lebih terjangkau daripada kita menyewa VPS&lt;/p&gt;

&lt;p&gt;Untuk rivalnya, Amazon Web Service, saya belum pernah mencobanya, jadi belum bisa berkomentar banyak soal bagaimana kalau menggunakan AWS, namun saya yakin juga akan semudah ini. Mungkin suatu saat nanti saya juga akan menggunakan AWS.&lt;/p&gt;




&lt;p&gt;Terima kasih telah membaca cerita singkat ini, semoga bermanfaat, dan memberikan inspirasi baru.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>googlecloud</category>
      <category>webdev</category>
      <category>webservice</category>
    </item>
    <item>
      <title>Rest API Menggunakan Dart</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Sat, 01 Oct 2022 13:59:38 +0000</pubDate>
      <link>https://dev.to/catur/rest-api-menggunakan-dart-4l3</link>
      <guid>https://dev.to/catur/rest-api-menggunakan-dart-4l3</guid>
      <description>&lt;p&gt;Artikel ini adalah tutorial pembuatan REST API dengan menggunakan bahasa Dart. Tutorial ini akan terasa cukup panjang, dengan membahas pembuatan REST API dengan menggunakan Dart. Dari menerima http request dari client berupa test dan menerima file upload dari client ke aplikasi REST API server Dart kita.&lt;/p&gt;

&lt;h2&gt;
  
  
  Persiapan
&lt;/h2&gt;

&lt;p&gt;Sebelum lebih jauh, kita akan mempersiapkan terlebih dahulu apa saja yang kita perlukan dalam membuat REST API ini. Pada seri kali ini, kita memerlukan beberapa hal berikut ini:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Visual Studio Code (VSCode). Kita akan menggunakan VSCode untuk code editor kita. karena VSCode sudah menyediakan plug-in yang dapat mempermudah pengerjaan kita nantinya.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dart SDK. Untuk membuat REST API dengan menggunakan DART, sudah tentu kita memerlukan bahasa Dart itu sendiri terinstall pada perangkat kia.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Intalasi VS Code
&lt;/h3&gt;

&lt;p&gt;Bagi yang belum menginstall aplikasi VSCode pada perangkat masing-masing, dapat mengunjungi &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;halaman website VS Code&lt;/a&gt;. Berikut ini tampilan untuk halaman website VSCode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjj5cyho7jbhhoox224hu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjj5cyho7jbhhoox224hu.png" alt="Halaman website VS Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pada halaman awal VSCode sudah disediakan link untuk men-download installer, dan VSCode sudah dapat memprediksi operating system yang kita gunakan, sehingga link download installer sudah menyesuaikan operating system yang kita gunakan, di sini saya menggunakan Windows dan pada halaman tersebut sudah disediakan installer VS Code untuk Windows.&lt;/p&gt;

&lt;p&gt;Selanjutnya silahkan click link download tersebut, untuk men-download installer-nya. Setelah selesai download, lakukan instalasi seperti biasa.&lt;/p&gt;

&lt;h3&gt;
  
  
  Instalasi Dart SDK
&lt;/h3&gt;

&lt;p&gt;Setelah melakukan instalasi VSCode, selanjutnya yang kita perlukan adalah Dart SDK, untuk melakukan instalasi Dart SDK, sebenarnya sudah dijelaskan pada &lt;a href="https://dart.dev/get-dart" rel="noopener noreferrer"&gt;halaman website dari Dart&lt;/a&gt;, untuk operating system Windows disarankan menggunakan &lt;a href="https://chocolatey.org/" rel="noopener noreferrer"&gt;Chocolatey&lt;/a&gt;. Bagi yang belum menginstal package manager Chocolatey di perangkat masing-masing, dapat melakukan instalasi sesuai dengan halaman website dari Chocolatey pada panduan &lt;a href="https://chocolatey.org/install" rel="noopener noreferrer"&gt;instalasinya&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Setelah selesai melakukan instalasi Chocolatey, lakukan instalasi Dart dengan menggunakan perintah berikut ini:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;choco&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;install&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;dart-sdk&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Untuk update Dart SDK dapat menggunakan perintah berikut ini:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;choco&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;upgrade&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;dart-sdk&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Apabila tidak ada kesalahan maka sekarang Dart SDK sudah berhasil terinstal pada perangkat kita. untuk memeriksanya, kita dapat mengetikkan perintah berikut ini pada command prompt kita:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;dart&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Perintah di atas akan memunculkan informasi terkait cara penggunaan Dart pada terminal. Dengan demikian kita sudah mempersiapkan alat yang kita butuhkan untuk membuat REST API dengan menggunakan Dart.&lt;/p&gt;
&lt;h3&gt;
  
  
  Addons VS Code
&lt;/h3&gt;

&lt;p&gt;Penjelasan sebelumnya kenapa pada tutorial ini menggunakan VSCode adalah, karena VSCode menyediakan Addons yang dapat memudahkan pengerjaan kita dalam menggunakan Dart. Pada tutorial kali ini, kita akan menggunakan salah satu addons Dart pada VSCode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr751pex4bggf7njd93ah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr751pex4bggf7njd93ah.png" alt="Addons Dart di VS Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tambahkanlah addons tersebut pada aplikasi VSCode masing-masing.&lt;/p&gt;


&lt;h2&gt;
  
  
  Aplikasi REST API Pertama
&lt;/h2&gt;

&lt;p&gt;Setelah kita selesai melakukan persiapan, kita akan memulai membuat aplikasi REST API pertama kita. Pada VSCode kita, buka command palette dengan menekan &lt;code&gt;ctrl + shift + p&lt;/code&gt;, Selanjutnya pada kolom palette ketikan &lt;code&gt;dart&lt;/code&gt;, sehingga akan muncul pilihan &lt;code&gt;new project&lt;/code&gt;, dan pilih &lt;code&gt;new project&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdtqwcfj7mde6snlqzx7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdtqwcfj7mde6snlqzx7.png" alt="New Project Dart Dengan VS Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Setelah memilih &lt;code&gt;new project&lt;/code&gt;, akan muncul pilihan lagi dan pilih &lt;code&gt;Server App&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7knalr61ljjg863539i7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7knalr61ljjg863539i7.png" alt="Pilihan Project Baru Dengan Dart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Setelah memilih &lt;code&gt;Server App&lt;/code&gt;, akan muncul jendela di mana kita akan menempatkan project kita. Kita bebas untuk menempatkan project kita, pilih saja dimana kita akan menempatkannya. Setelah menentukan direktori untuk project kita, selanjutnya VSCode akan meminta nama project baru kita. Kita bebas menentukan nama project baru kita, pada tutorial kali ini, kita akan menggunakan nama default dari plugin, yaitu &lt;code&gt;dart_application_1&lt;/code&gt;. Setelah itu, plugin akan melakukan generate data project baru kita secara otomatis. Setelah selesai, maka aplikasi REST API pertama kita sudah siap untuk digunakan.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3w5fs0yw81a6p5epav67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3w5fs0yw81a6p5epav67.png" alt="Project Pertama Dart REST API"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Running Aplikasi REST API Pertama Kita
&lt;/h2&gt;

&lt;p&gt;Setelah kita membuat aplikasi pertama kita, kita akan mencoba untuk running aplikasi kita. Kita akan menjalankan aplikasi kita dengan menggunakan terminal command prompt atau powershell di Windows, untuk sistem operasi lain dapat menggunakan terminal pada sistem operasi masing-masing. Pertama kita perlu mengarahkan direktori terminal kita ke direktori project kita, atau cara paling mudah adalah kita membuka terminal melalui VSCode dengan menekan &lt;code&gt;ctrl + shift + c&lt;/code&gt;, atau kita juga dapat menggunakan terminal bawaan dari VSCode, untuk menggunakannya, tekan &lt;code&gt;ctrl + j&lt;/code&gt;. Untuk menjalankan aplikasi kita, kita perlu menjalankan file utama pada server Dart kita. Pada project default, file utama ada di &lt;code&gt;bin/server.dart&lt;/code&gt;. untuk menjalankanya ketikan perintah berikut ini.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;dart&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;bin/server.dart&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Apabila tidak ada masalah, maka pada terminal kita akan mencetak tulisan 'Server listening on port 8080' yang menandakan server kita telah berjalan di port 8080. Untuk memastikan kalau aplikasi kita berjalan dengan baik, kita dapat mencobanya pada browser kita dengan mengakses halaman localhost:8080. Apabila tidak ada kesalahan, maka alamat tersebut akan memunculkan kalimat 'Hello, World!'.&lt;/p&gt;
&lt;h3&gt;
  
  
  Review Kode
&lt;/h3&gt;

&lt;p&gt;Kita telah berhasil menjalankan aplikasi REST API pertama kita, selanjutnya kita perlu memahami struktur kode yang baru saja kita jalankan. Pada bundle aplikasi hasil generate dari dart pada langkah awal pada tutorial ini, kita akan banyak bekerja pada direktori &lt;code&gt;bin&lt;/code&gt; di mana kita akan menempatkan semua file sistem pada direktori ini. Apabila diperhatikan, pada direktori bin terdapat file server.dart, ini merupakan file utama pada aplikasi kita.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'dart:io'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf_io.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf_router/shelf_router.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Configure routes.&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;_router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_rootHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/echo/&amp;lt;message&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_echoHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="nf"&gt;_rootHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;req&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="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello, World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="nf"&gt;_echoHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="si"&gt;$message&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Use any available host or container IP (usually `0.0.0.0`).&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;InternetAddress&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;anyIPv4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Configure a pipeline that logs requests.&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Pipeline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;logRequests&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_router&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// For running in containers, we respect the PORT environment variable.&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Platform&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'PORT'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s"&gt;'8080'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Server listening on port &lt;/span&gt;&lt;span class="si"&gt;${server.port}&lt;/span&gt;&lt;span class="s"&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;Baris kode di atas adalah file server.dart hasil generate dari dart. Pada generated awal, kita akan diberikan dua route sampel untuk bisa kita coba, perhatikan baris kode ini:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;_router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_rootHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/echo/&amp;lt;message&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_echoHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Baris kode di atas menunjukkan bahwa dart membuatkan kita route sampel yaitu &lt;code&gt;'/'&lt;/code&gt; dan &lt;code&gt;'/echo/&amp;lt;message&amp;gt;'&lt;/code&gt;. Untuk mencobanya, kita gunakan aplikasi Postman, pastikan terlebih dahulu dart server kita berjalan dengan mengetikkan perintah di terminal &lt;code&gt;dart bin/server.dart&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Menjalankan Route '/'
&lt;/h4&gt;

&lt;p&gt;Untuk mencoba route '/', kita dapat mengetikkan url &lt;code&gt;localhost:8080&lt;/code&gt; dengan method request &lt;code&gt;GET&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwouxuftti9sw9am5atjy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwouxuftti9sw9am5atjy.png" alt="Akses Route"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maka kita akan mendapatkan hasil kembalian berupa string 'Hallo World'. Hasil kembalian ini kita dapatkan dari fungsi &lt;code&gt;_rootHandler&lt;/code&gt; yang di gunakan pada route '/'. sehingga route untuk '/' sebenarnya menjalankan fungsi &lt;code&gt;_rootHandler&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Menjalankan Route '/echo/&amp;lt;message&amp;gt;'
&lt;/h4&gt;

&lt;p&gt;Untuk mencoba route '/echo/', kita dapat mengetikkan url &lt;code&gt;localhost:8080&lt;/code&gt; dengan method request &lt;code&gt;GET&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6qi517mf5s1wb0kir6x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6qi517mf5s1wb0kir6x.png" alt="Akses Route"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maka kita akan mendapatkan hasil kembalian string 'hallo'. Hasil kembalian ini kita dapatkan dari fungsi &lt;code&gt;_echoHandler&lt;/code&gt; yang digunakan pada route '/echo/'. sehingga route untuk '/echo/' sebenarnya menjalankan fungsi &lt;code&gt;_echoHandler&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Seperti contoh di atas, pada penulisan route akan seperti ini '/echo/' namun saat pemanggilan, parameter '' kita ganti dengan string 'hallo'. Dalam REST API dengan menggunakan Dart, untuk melewatkan parameter dapat menggunakan notasi &lt;code&gt;&amp;lt;nama&amp;gt;&lt;/code&gt; yang disebut dengan notasi diamond.&lt;/p&gt;
&lt;h4&gt;
  
  
  Kesimpulan
&lt;/h4&gt;

&lt;p&gt;Konsep dasarnya adalah kita membuat sebuah fungsi dengan parameter object &lt;code&gt;Request&lt;/code&gt;, untuk handle sebuah route. untuk menambahkan route lagi, kita hanya perlu membuat fungsi lagi untuk menanganinya, kemudian kita daftarkan pada &lt;code&gt;_router&lt;/code&gt;. Kita akan mencobanya setelah ini.&lt;/p&gt;


&lt;h2&gt;
  
  
  Method Request
&lt;/h2&gt;

&lt;p&gt;Sebelumnya kita telah mencoba membuat REST API kita dengan menggunakan metode GET, selanjutnya kita kan akan mencoba membuat metode request POST dan bagaimana menangkap parameternya, kali ini kita akan mencoba membuat route &lt;code&gt;/greeting&lt;/code&gt;. Pertama kita perlu membuat sebuah fungsi untuk menangani route &lt;code&gt;/greeting&lt;/code&gt; ini.&lt;br&gt;
kita akan membuat sebuah fungsi seperti berikut ini:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_getPostJsonHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readAsString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;queryParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;query:&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;queryParameters&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queryParams&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nl"&gt;headers:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Content-type'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'application/json'&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;Fungsi di atas adalah fungsi untuk menampilkan POST body. Selanjutnya, kita perlu membuat route baru pada &lt;code&gt;_router&lt;/code&gt; dan mendaftarkan fungsi &lt;code&gt;_getPostJsonHandler&lt;/code&gt;. pada fungsi &lt;code&gt;_router&lt;/code&gt; kita rubah menjadi seperti berikut ini:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Configure routes.
final _router = Router()
  ..get('/', _rootHandler)
  ..get('/echo/&amp;lt;message&amp;gt;', _echoHandler)
  ..post('/getpostdata', _getPostJsonHandler);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Setelah menambahkan route, maka kita dapat mencoba testing route baru kita. Untuk mencobanya, &lt;strong&gt;kita perlu mematikan server kita terlebih dahulu dan memulainya kembali&lt;/strong&gt;. Berikut apabila kita coba menggunakan aplikasi Postman.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjs9zcrb74l8xfhg0z4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjs9zcrb74l8xfhg0z4x.png" alt="Contoh Response Dengan Metode POST"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pada gambar di atas memperlihatkan bahwa hasil response berupa JSON. Langkah selanjutnya, kita dapat mengkreasikan dengan menimpan data tersebut ke database atau menyesuaikan kebutuhan kita nantinya.&lt;/p&gt;


&lt;h2&gt;
  
  
  Media Upload
&lt;/h2&gt;

&lt;p&gt;Setelah kita berhasil membuat plikasi REST API dengan menerima parameter berupa string baik dengan GET, POST, atau dengan metode request yang lain, selanjutnya kita akan mencoba untuk menerima parameter berupa file. Untuk melakukan ini, kita memerlukan  dependensi dari Dart yaitu &lt;a href="https://pub.dev/packages/mime" rel="noopener noreferrer"&gt;mime&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Bagi yang masih bingung untuk instalasinya, cukup ketikkan perintah di bawah ini pada terminal yang mengarah pada project kita.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;dart&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;pub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;mime&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Fungsi Controller
&lt;/h3&gt;

&lt;p&gt;Setelah selesai install dependensi &lt;code&gt;mime&lt;/code&gt;, maka kita sudah memiliki semua yang dibutuhkan untuk menerima file. Seperti biasa, hal pertama yang perlu kita lakukan adalah dengan membuat fungsi untuk menangani route nantinya. Pada tutorial ini kita akan membuat fungsi untuk menangani upload seperti berikut ini:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_upload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'content-type'&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="n"&gt;contentType&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="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;body:&lt;/span&gt; &lt;span class="s"&gt;'content-type tidak ditemukan'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;mediaType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contentType&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="n"&gt;mediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mimeType&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;'multipart/form-data'&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="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;body:&lt;/span&gt; &lt;span class="s"&gt;'content-type tidak valid'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;boundary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'boundary'&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="n"&gt;boundary&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="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;body:&lt;/span&gt; &lt;span class="s"&gt;'boundary tidak ditemukan'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;read&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MimeMultipartTransformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;boundary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kn"&gt;part&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="kn"&gt;part&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'content-type'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'image/png'&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="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;partsIterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;StreamIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;partsIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;moveNext&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kn"&gt;part&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;partsIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;current&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'./uploads/testing.png'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// direktori file upload&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;chunksIterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;StreamIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kn"&gt;part&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;chunksIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;moveNext&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chunksIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;writeAsBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;mode:&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&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="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Upload Berhasil'&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="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&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;Setelah membuat fungsi untuk menangani file upload seperti di atas, selanjutnya kita buat route untuk mengakses fungsi tersebut, pada variabel &lt;code&gt;_router&lt;/code&gt; kita ubah menjadi seperti berikut ini:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final _router = Router()
  ..get('/', _rootHandler)
  ..get('/echo/&amp;lt;message&amp;gt;', _echoHandler)
  ..post('/getpostdata', _getPostJsonHandler)
  ..post('/upload', _upload);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Kita menambahkan router &lt;code&gt;/upload&lt;/code&gt; pada variabel &lt;code&gt;_router&lt;/code&gt; dengan mengakses fungsi &lt;code&gt;_upload&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Setelah kita mempersiapkan fungsi dan juga route-nya, selanjutnya kita persiapkan direktori untuk menampung file yang akan kita upload, pada tutorial ini kita akan menempatkan semua file upload pada filter uploads pada direktori root. Kita akan membuat folder Uploads pada direktori root kita.&lt;/p&gt;

&lt;p&gt;Setelah semua siap, selanjutnya kita dapat mulai mencoba untuk menjalankan sistem upload kita. Seperti biasa kita perlu me-restart server kita, kita matikan server kita, dan kita jalankan lagi. Setelah berhasil me-restart, kita jalankan route upload yang baru saja kita buat. Kali ini, kita akan mencoba menjalankannya dengan menggunakan aplikasi Postman.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9cepo5z0z7dudbrwpxdm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9cepo5z0z7dudbrwpxdm.png" alt="Contoh Request Upload Gambar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Gambar di atas adalah apabila kita mencoba akses route upload kita dengan menggunakan Postman. Untuk membuktikan apakah file sudah benar-benar berpindah, kita dapat memeriksa folder uploads pada direktori root kita. Sampai di sini, file tutorial dapat diambil pada repositori di bawah ini.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://gitlab.com/caturdev/demo-rest-api-mengguanakan-dart" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgitlab.com%2Fuploads%2F-%2Fsystem%2Fproject%2Favatar%2F39872723%2Flogo_baru_codencookie_thumbnail.png" height="auto" class="m-0"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://gitlab.com/caturdev/demo-rest-api-mengguanakan-dart" rel="noopener noreferrer" class="c-link"&gt;
          Catur Wicaksono / Demo REST API Mengguanakan Dart · GitLab
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          GitLab.com
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgitlab.com%2Fassets%2Ffavicon-72a2cad5025aa931d6ea56c3201d1f18e68a8cd39788c7c80d5b2b82aa5143ef.png"&gt;
        gitlab.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;






&lt;h2&gt;
  
  
  Desain Module
&lt;/h2&gt;

&lt;p&gt;Sesi terakhir pada tutorial kita kali ini adalah untuk membuat aplikasi kita setidaknya mudah untuk di-maintenance. Module aplikasi REST API yang terakhir kita buat, kita akan mencoba memisahkan antara startup file dengan module-module yang lain.&lt;/p&gt;

&lt;p&gt;Pertama kita akan memisahkan fungsi &lt;code&gt;_uplaod&lt;/code&gt; dan &lt;code&gt;_getPostJsonHandler&lt;/code&gt; ke dalam file baru pada folder yang sama, yaitu folder dengan nama file baru &lt;code&gt;user.dart&lt;/code&gt;. Selanjutnya, pada file server.dart kita juga akan menghapus fungsi &lt;code&gt;_rootHandler&lt;/code&gt; dan &lt;code&gt;_echoHandler&lt;/code&gt; beserta route dari fungsi-fungsi tersebut.&lt;/p&gt;

&lt;p&gt;Pada file modul user.dart, fungsi &lt;code&gt;_uplaod&lt;/code&gt; dan &lt;code&gt;_getPostJsonHandler&lt;/code&gt; akan kita sesuaikan supaya dapat di terima pada startup file (server.dart) kita perlu membuat fungsi yang mengembalikan objek router.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserApi&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Router&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;



    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;router&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;Pada file user.dart kita akan buat dulu class UserApi, nama kelas ini bebas, dapat disesuaikan sesuai keinginan. pada class UserApi kita membuat sebuah getter yang mengembalikan object &lt;code&gt;Router&lt;/code&gt;. Selanjutnya kita akan memindahkan fungsi &lt;code&gt;_uplaod&lt;/code&gt; dan &lt;code&gt;_getPostJsonHandler&lt;/code&gt; sebelumnya pada blok getter ini. Perhatikan block code berikut ini.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserApi&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Router&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nl"&gt;headers:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Content-type'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'application/json'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="s"&gt;'code'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="s"&gt;'data'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Catur Wicaksono'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'address'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Jakarta'&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="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/login'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readAsString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;queryParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;query:&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;queryParameters&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nl"&gt;headers:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Content-type'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'application/json'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="s"&gt;'code'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="s"&gt;'data'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;queryParams&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="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/upload/picture'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'content-type'&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="n"&gt;contentType&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="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;body:&lt;/span&gt; &lt;span class="s"&gt;'content-type tidak ditemukan'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;mediaType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contentType&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="n"&gt;mediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mimeType&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;'multipart/form-data'&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="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;body:&lt;/span&gt; &lt;span class="s"&gt;'content-type tidak valid'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;boundary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'boundary'&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="n"&gt;boundary&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="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;body:&lt;/span&gt; &lt;span class="s"&gt;'boundary tidak ditemukan'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;read&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
          &lt;span class="n"&gt;MimeMultipartTransformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;boundary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kn"&gt;part&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="kn"&gt;part&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'content-type'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'image/png'&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="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;partsIterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;StreamIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;partsIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;moveNext&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kn"&gt;part&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;partsIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;current&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'./uploads/testing.png'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// direktori file upload&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;chunksIterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;StreamIterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kn"&gt;part&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;chunksIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;moveNext&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chunksIterator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;writeAsBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;mode:&lt;/span&gt; &lt;span class="n"&gt;FileMode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&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="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Upload Berhasil'&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="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&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="n"&gt;router&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;Pada baris kode di atas, kita mengimplementasikan fungsi &lt;code&gt;_uplaod&lt;/code&gt; dan &lt;code&gt;_getPostJsonHandler&lt;/code&gt; sebelumnya, namun dengan cara yang agak berbeda. Perhatikan, nama kedua fungsi menjadi anonymous function yang langsung ditambahkan setelah inisialisasi router. fungsi &lt;code&gt;_uplaod&lt;/code&gt; kita tempatkan pada route &lt;code&gt;/upload/picture&lt;/code&gt;, dan &lt;code&gt;_getPostJsonHandler&lt;/code&gt; pada route &lt;code&gt;/login&lt;/code&gt;. Untuk referensi kita akan menambahkan satu route lagi dengam metode &lt;code&gt;get&lt;/code&gt;, dengan baris kode seperti di bawah ini.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nl"&gt;headers:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Content-type'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'application/json'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="s"&gt;'code'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;'data'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Catur Wicaksono'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'address'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Jakarta'&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Setelah selesai membuat module user, selanjutnya kita gabungkan module user ke startup file kita, server.dart. Pada file server.dart, kita tambahkan module pada variable router sebelumnya seperti berikut ini.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final _router = Router()..mount('/user', UserApi().router);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Untuk menambahkan module, kita gunakan fungsi mount seperti di atas, parameter pertama adalah user, dan parameter kedua adalah object module yang akan ditambahkan.&lt;/p&gt;

&lt;p&gt;Dengan demikian cara mengakses route agak sedikit berbeda dari sebelumnya, misal pada module user terdapat route &lt;code&gt;/login&lt;/code&gt;, karena module ini ada pada poute &lt;code&gt;/user&lt;/code&gt; pada startup file, maka cara mengaksesnya dengan menggunakan router pada startup file terlebih dahulu dan dilanjutkan dengan route module, menjadi &lt;code&gt;/user/login&lt;/code&gt;. Kita akan mencobanya dengan menggunakan Postman.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fei1vdbsbj87ecq8zaran.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fei1vdbsbj87ecq8zaran.png" alt="Akses Route Baru Dengan Postman"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Untuk source code lengkap untuk tutorial ini, dapat diakses pada repository di bawah ini.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://gitlab.com/caturdev/demo-rest-api-mengguanakan-dart" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgitlab.com%2Fuploads%2F-%2Fsystem%2Fproject%2Favatar%2F39872723%2Flogo_baru_codencookie_thumbnail.png" height="auto" class="m-0"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://gitlab.com/caturdev/demo-rest-api-mengguanakan-dart" rel="noopener noreferrer" class="c-link"&gt;
          Catur Wicaksono / Demo REST API Mengguanakan Dart · GitLab
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          GitLab.com
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgitlab.com%2Fassets%2Ffavicon-72a2cad5025aa931d6ea56c3201d1f18e68a8cd39788c7c80d5b2b82aa5143ef.png"&gt;
        gitlab.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;






&lt;h2&gt;
  
  
  Epilog
&lt;/h2&gt;

&lt;p&gt;Kita cukupkan materi tutorial ini sampai di sini. Sistem yang kita bangun pada tutorial ini masih sangat dasar, sehingga masih perlu banyak pengembangan lagi. Terima kasih telah mengikuti tutorial yang cukup panjang ini.&lt;/p&gt;

</description>
      <category>dart</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>XAMPP MySQL Shutdown Unexpectedly</title>
      <dc:creator>Catur Wicaksono</dc:creator>
      <pubDate>Thu, 29 Sep 2022 05:48:05 +0000</pubDate>
      <link>https://dev.to/catur/xampp-mysql-shutdown-unexpectedly-3953</link>
      <guid>https://dev.to/catur/xampp-mysql-shutdown-unexpectedly-3953</guid>
      <description>&lt;p&gt;Artikel kali ini, kita akan sedikit membahas mengenai penanganan error pada XAMPP MySQL dengan pesan error shutdown unexpectedly. Developer yang menggunakan XAMPP untuk melakukan running database secara local, cepat atau lambat memiliki potensi error semacam ini pada XAMPP MySQL nya. Sudah banyak artikel yang membahas mengenai permasalahan ini, namun terkadang cara yang menjadi rekomendasi belum efektif maupun efisien. Bahkan, mungkin dapat menimbulkan error lain yang kadang tidak tersadari.&lt;/p&gt;

&lt;p&gt;Pada kesempatan kali ini, kita akan mencoba melakukan penanganan error tersebut dengan cara yang cukup aman untuk dilakukan untuk  XAMPP MySQL shutdown unexpectedly, tanpa perlu khawatir akan terjadi penurunan stabilitas fungsi XAMPP MySQL itu sendiri. Selanjutnya kita akan melakukan penanganan langkah demi langkah berikut ini.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error Handling
&lt;/h2&gt;

&lt;p&gt;Error yang terjadi pada artikel ini kurang lebih memiliki tampilan seperti di bawah ini pada XAMPP:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TO22AJJo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8m4m5jormrncwiarkaec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TO22AJJo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8m4m5jormrncwiarkaec.png" alt="Database XAMPP error" width="667" height="735"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pemecahan Masalah
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Langkah pertama, kita masuk ke direktori XAMPP kita, untuk yang melakukan instalasi normal, biasa terdapat pada drive C:\xampp. Pada direktori tersebut, kita masuk ke direktori mysql.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pada direktori mysql, lakukan rename pada foler data dengan nama data_backup. Langkah ini untuk melakukan backup data untuk menghindari kesalahan yang tidak diinginkan. Kita dapat memberikan nama dengan nama yang lain, sesuai dengan keinginan kita.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Langkah selanjutnya, kita copy folder backup, dengan nama data. Langkah ini untuk membuat folder data baru dengan menggunakan data-data yang ada di folder backup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Selanjutnya, kita copy database yang ada di folder data_backup, ke dalam folder data. Database yang perlu dicopy adalah semua folder yang ada pada data_backup, selain folder mysql, performance_schema, phpmyadmin, test. Sehingga hanya database yang benar-benar kita buat saja yang kita pindahkan.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Langkah terakhir, kita copy file ibdata1 yang ada di folder data_backup, kedalam folder data, lakukan replace file.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sampai sini, seharusnya XAMPP MySQL kita sudah dapat berjalan dengan benar kembali, dan tidak terjadi error. Lakukan running kembali pada XAMPP MySQL kita, dan perhatikan bahwa error sudah tertangani dengan baik.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zFi3BF7B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1r82oiauqhae25qmmf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zFi3BF7B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1r82oiauqhae25qmmf4.png" alt="Database XAMPP berhasil diperbaiki" width="672" height="740"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apabila semua berjalan dengan benar, maka kurang lebih tampilan XAMPP kita saat running MySQL, seperti gambar di atas.&lt;/p&gt;




&lt;p&gt;Daftar Referensi:&lt;br&gt;
&lt;a href="https://stackoverflow.com/a/61859561/16632789"&gt;https://stackoverflow.com/a/61859561/16632789&lt;/a&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>errors</category>
      <category>mysql</category>
      <category>xampp</category>
    </item>
  </channel>
</rss>
