<?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: Victor Hugo</title>
    <description>The latest articles on DEV Community by Victor Hugo (@hxsggsz).</description>
    <link>https://dev.to/hxsggsz</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%2F1059128%2Faa8fb5ea-c943-4c26-a92c-936c72508faa.jpeg</url>
      <title>DEV Community: Victor Hugo</title>
      <link>https://dev.to/hxsggsz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hxsggsz"/>
    <language>en</language>
    <item>
      <title>The most useful data structure for leetcode</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Tue, 27 May 2025 00:43:51 +0000</pubDate>
      <link>https://dev.to/hxsggsz/the-most-useful-data-structure-for-leetcode-1jbo</link>
      <guid>https://dev.to/hxsggsz/the-most-useful-data-structure-for-leetcode-1jbo</guid>
      <description>&lt;p&gt;Recently I decided to enhance my programming logic so I started making some leetcode problems daily just to exercise, but as always I feel a lot of difficulty doing this logic exercises until youtube recommended &lt;a href="https://www.youtube.com/watch?v=b7Vy-uIQUrE" rel="noopener noreferrer"&gt;this video from a Brazilian tech content creator&lt;/a&gt;  where he teach how to solve one of the most common exercises from tech interviews using a map.&lt;/p&gt;

&lt;h2&gt;
  
  
  what is a map
&lt;/h2&gt;

&lt;p&gt;Map is a data structure where can store a value combined with a key, so if you need to access this data later you can use the key instead of interating an entire array to find a value (just like the filter and find methods in javascript). &lt;/p&gt;

&lt;h2&gt;
  
  
  But why should I use a map instead of array?
&lt;/h2&gt;

&lt;p&gt;You can simplify the logic with a map instead of using a lot of for loops to make the exercise, being more legible and understandable. When you iterate an array, your code  have the complexity of O(n) where n is the size of the array because you have to iterate the entire array, when using a Map, operations such as get and set have a time complexity of O(1) because the Map relies on a hash table internally. This structure allows the engine to compute the exact location of a value based on its key, so it can access or update entries directly without iterating through all keys and values. As a result, the performance of these operations remains constant regardless of the size of the Map, depending only on the key being used rather than the total number of elements stored.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>map</category>
      <category>javascript</category>
    </item>
    <item>
      <title>lexical analysis / tokenization</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Wed, 05 Mar 2025 23:36:45 +0000</pubDate>
      <link>https://dev.to/hxsggsz/lexical-analysis-tokenization-2e4k</link>
      <guid>https://dev.to/hxsggsz/lexical-analysis-tokenization-2e4k</guid>
      <description>&lt;p&gt;A long time ago some friends showed me one project, one screen split in  a half, on the left has a code editor with prisma models and on the right a diagram with nodes with the prisma model. It is a really good whey to see your models but I forgot about this project.&lt;/p&gt;

&lt;p&gt;In this year I decided to learn &lt;a href="https://go.dev/doc/" rel="noopener noreferrer"&gt;Golang&lt;/a&gt; and was thinking about projects that I could do to learn more this language, in my research I discovered a technique called Lexical analysis, also known as tokenization, when I discovered it the first thing that came into my mind was that project with prisma models and diagrams and them I decided to recreate this project using this tokenization technique.&lt;/p&gt;

&lt;h2&gt;
  
  
  what is tokenization
&lt;/h2&gt;

&lt;p&gt;Lexical analysis (also called &lt;strong&gt;tokenization&lt;/strong&gt;) is the process of converting raw source code into structured tokens, which are the smallest meaningful units of a programming language. This technique was created to create programming languages. But you can use this technique to anything you want. &lt;/p&gt;

&lt;p&gt;In that whey I created an API in Golang that reads the prisma models and tokenize it with a struct with type and value, the program uses position to know where the program is currently reading  when it gets a piece of the text and tokenize it, it advances the position in the text until it finds the next token.&lt;/p&gt;

&lt;p&gt;After the Tokenization,  it gets the models tokens and formats it with an object with id, model name, the content of the model with the columns and the relations between the models and returns to the api.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I use it
&lt;/h2&gt;

&lt;p&gt;in frontend I have a code editor that accepts prisma models and a diagram canvas. Every code you put into the code editor it sends to the api and render the nodes in the canvas with the data returned&lt;/p&gt;

&lt;p&gt;&lt;a href="https://priagram.vercel.app/" rel="noopener noreferrer"&gt;Here is the link of the project if you want to test&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%2Fhs9y38grrzfxozff5h07.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%2Fhs9y38grrzfxozff5h07.png" alt="a diagram explaning the flow of project" width="800" height="755"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>learning</category>
      <category>api</category>
    </item>
    <item>
      <title>What is Trunk Based Development</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Mon, 24 Feb 2025 17:40:40 +0000</pubDate>
      <link>https://dev.to/hxsggsz/what-is-trunk-based-development-kfm</link>
      <guid>https://dev.to/hxsggsz/what-is-trunk-based-development-kfm</guid>
      <description>&lt;p&gt;It is a source-control branching model where developers collaborate on code in a single branch called &lt;code&gt;trunk&lt;/code&gt;, They therefore avoid merge hell, with having only one source of true, the trunk branch and with smaller commits. Do not break the build, since you have to check if the build it is still working on every commit. &lt;/p&gt;

&lt;p&gt;This means that they're code will go to the &lt;code&gt;trunk (main)&lt;/code&gt; branch and the repository will &lt;strong&gt;not&lt;/strong&gt; have more than one main branch (e.g: a development branch or a release branch).&lt;/p&gt;

&lt;h2&gt;
  
  
  Who use it?
&lt;/h2&gt;

&lt;p&gt;Giants from the tech like: Google, Atlassian, Facebook (meta), Microsoft, Amazon, etc...&lt;/p&gt;

&lt;h2&gt;
  
  
  CI/CD
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Trunk-Based Development&lt;/code&gt; is a key enabler of &lt;code&gt;Continuous Integration (CI)&lt;/code&gt;and by extension &lt;code&gt;Continuous Delivery (CD)&lt;/code&gt;. When individuals on a team are committing their changes to the trunk multiple times a day it becomes easy to satisfy the core requirement of &lt;code&gt;Continuous Integration&lt;/code&gt; that all team members commit to trunk at least once every 24 hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;Every developer will create smaller but continuous commits to the trunk through Pull Requests checking the build (it must pass before sending the code to the trunk). You can create a &lt;code&gt;Continuous Integration (CI)&lt;/code&gt; with &lt;code&gt;Github Actions&lt;/code&gt; to run the build, linter and tests on every new commit in the Pull Request to check it for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous code review
&lt;/h2&gt;

&lt;p&gt;After you finish your work and the &lt;code&gt;Continuous Integration (CI)&lt;/code&gt; passed, someone should review your code, and after that merge your pull request with squash and merge, to create one commit with sub-commits were the sub-commits will be the smaller commits you have send to the pull request. &lt;/p&gt;

&lt;p&gt;Indeed, in &lt;code&gt;Trunk Based Development&lt;/code&gt; your commits will be small but continuous to guarantee the &lt;code&gt;Continuous Integration (CI)&lt;/code&gt; but that also means that you will have continuous code review, since the commits will be smaller than usual it will enable a fast and simple code review of your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous Delivery
&lt;/h2&gt;

&lt;p&gt;With small commits, easy code review and the &lt;code&gt;Continuous Integration (CI)&lt;/code&gt; checking the build, lint and tests you have a &lt;code&gt;trunk (main)&lt;/code&gt;branch always ready for deploys and tests without having the necessity to create a new release branch to test the commits and deploys, your main branch can do it for you. &lt;/p&gt;

&lt;h2&gt;
  
  
  History
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Trunk Based Development&lt;/code&gt; is not a new branching model. The word ‘trunk’ is referent to the concept of a growing tree, where the fattest and longest span is the trunk, not the branches that radiate from it and are of more limited length.&lt;/p&gt;

&lt;p&gt;It has been a solution of the merge hell that the &lt;code&gt;gitflow&lt;/code&gt; development causes with the multiple branches&lt;/p&gt;

</description>
      <category>git</category>
      <category>programming</category>
      <category>productivity</category>
      <category>development</category>
    </item>
    <item>
      <title>Creating a hashmap from scratch in Golang</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Wed, 12 Feb 2025 18:21:22 +0000</pubDate>
      <link>https://dev.to/hxsggsz/creating-a-hashmap-from-scratch-in-golang-28od</link>
      <guid>https://dev.to/hxsggsz/creating-a-hashmap-from-scratch-in-golang-28od</guid>
      <description>&lt;p&gt;Recently I was watching &lt;a href="https://www.youtube.com/watch?v=J4ELMYEGVS0" rel="noopener noreferrer"&gt;this video from a Brazilian youtuber called Augusto Galego&lt;/a&gt; about creating a hashmap from scratch, while I was watching this video something came to my mind: what if I do the same but with go? and that is what I did! &lt;a href="https://github.com/hxsggsz/hashmap-go" rel="noopener noreferrer"&gt;here is the repo&lt;/a&gt; If you want to try, in the main file there is a lot of tests with the hashmap, you will need to have installed go version 1.18 or superior because of the go generics.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; first you will need to copy the code on your machine, check if you have installed git on it:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/hxsggsz/hashmap-go.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;now run the &lt;code&gt;main.go&lt;/code&gt;file to see the tests:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is a hashmap
&lt;/h2&gt;

&lt;p&gt;A hashmap is a data structure that stores key-value pairs. It uses a hash function to compute an index where the value is stored, enabling average O(1) time complexity for insertions, deletions, and lookups, so for example, if you want to store some data in the key "age" it will hash this key, this will generate a large number, we get this large number and divide it by the factorial of the size of your data structure, in my case I use the slices from Golang, it will returns always the same number by the length of the key, this will be the index to search in the data structure. But what if I store two values with a key with the same index?&lt;/p&gt;

&lt;p&gt;For example I store the value 21 in key &lt;code&gt;age&lt;/code&gt; and 200 in key &lt;code&gt;now&lt;/code&gt;, the hash function will return the same index for both of the keys because both of them have the same size of characters, that's a conflict problem and to solve this I use the chaining solution that consists in using another slice inside the slice to store the values, so for example in the index 3 of the slice it will have another slice with key &lt;code&gt;age&lt;/code&gt; and value 21 and the key &lt;code&gt;now&lt;/code&gt; with the value 200.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods
&lt;/h2&gt;

&lt;p&gt;This hashmap have 4 public methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;set

&lt;ul&gt;
&lt;li&gt;store a key with a value.&lt;/li&gt;
&lt;li&gt;if there already have key stored, he updates the value.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;get

&lt;ul&gt;
&lt;li&gt;gets the value stored to a key.&lt;/li&gt;
&lt;li&gt;if not founds the key it throws a error.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;put

&lt;ul&gt;
&lt;li&gt;update the value stored to a key.&lt;/li&gt;
&lt;li&gt;if not founds the key it throws a error.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;delete

&lt;ul&gt;
&lt;li&gt;deletes the value and key stored to a key.&lt;/li&gt;
&lt;li&gt;if not founds the key it throws a error.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;And on private method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hash:

&lt;ul&gt;
&lt;li&gt;hashs the string passed by param and divide by factorial of the size of  the slice.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Generics
&lt;/h2&gt;

&lt;p&gt;I am a typescript developer and I love using generics in my code, because of that I saw an opportunity to learn to use generics on Golang, since it was implemented in the version &amp;gt;1.18, and that is what I did, instead of allowing only a type of data in the hashmap I use the generics to allow any type of the data in the hashmap.&lt;/p&gt;

</description>
      <category>go</category>
      <category>hashmap</category>
      <category>datastructures</category>
      <category>study</category>
    </item>
    <item>
      <title>How to install a Window Manager on Linux</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Wed, 29 Jan 2025 21:50:18 +0000</pubDate>
      <link>https://dev.to/hxsggsz/how-to-install-a-window-manager-on-linux-4g9k</link>
      <guid>https://dev.to/hxsggsz/how-to-install-a-window-manager-on-linux-4g9k</guid>
      <description>&lt;h2&gt;
  
  
  What is a Window manager
&lt;/h2&gt;

&lt;p&gt;A window manager is a system software that controls the placement and appearance of windows within a graphical user interface. It serves as the fundamental component responsible for managing how windows are displayed, manipulated, and organized on your screen. This is a fundamental software if you have only one monitor and have some trouble managing you windows.&lt;/p&gt;

&lt;p&gt;I have my own dotfiles with all the configuration for the softwares that we will use in this article, &lt;a href="https://github.com/hxsggsz/dotfiles" rel="noopener noreferrer"&gt;here is the github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I like to use i3 as a window manager and it's what I will use in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is i3
&lt;/h2&gt;

&lt;p&gt;i3 (i3wm) is a tiling window manager designed for X11 operating systems it represents a minimalist yet powerful approach to window management on Linux.&lt;/p&gt;

&lt;p&gt;Let's start! First I need to install the window manager:&lt;/p&gt;

&lt;p&gt;Let's install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;i3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, we already have the window manager installed but he by he self is a little bit raw, we need a few things to increment it!&lt;/p&gt;

&lt;p&gt;Now we need a nice font.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nerdfonts
&lt;/h2&gt;

&lt;p&gt;Nerd Fonts is an open-source project that enhances developer-focused fonts by adding numerous additional glyphs and icons, making them more functional for programming environments, we need a font from them to have the icons displayed in our i3.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first you need to go to the &lt;a href="https://www.nerdfonts.com/font-downloads" rel="noopener noreferrer"&gt;official nerdfonts site&lt;/a&gt; and choose your font&lt;/li&gt;
&lt;li&gt;click in the download button with the right mouse button and copy the link of it&lt;/li&gt;
&lt;li&gt;now you need to run this command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget &lt;span class="nt"&gt;-P&lt;/span&gt; ~/.local/share/fonts &amp;lt;url-link-you-copied&amp;gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; ~/.local/share/fonts &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; unzip &amp;lt;name-of-the-downloaded-zip&amp;gt;.zip &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &amp;lt;name-of-the-downloaded-zip&amp;gt;.zip &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; fc-cache &lt;span class="nt"&gt;-fv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is Picom
&lt;/h2&gt;

&lt;p&gt;Picom is a lightweight standalone compositor for the X Window System, specifically designed to enhance window managers that lack built-in compositing capabilities&lt;br&gt;
and it have a few enhancements by using picom:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eliminates screen tearing and stuttering during video playback&lt;/li&gt;
&lt;li&gt;Provides smooth transitions between windows&lt;/li&gt;
&lt;li&gt;Enables transparency control for both active and inactive windows&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;picom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to install polybar&lt;/p&gt;

&lt;h2&gt;
  
  
  What is polybar
&lt;/h2&gt;

&lt;p&gt;Polybar is a fast and lightweight status bar tool designed for Linux desktop environments and window managers, particularly useful for minimalist window managers like i3&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launch applications through a searchable interface
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;polybar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to install rofi&lt;/p&gt;

&lt;h2&gt;
  
  
  What is rofi
&lt;/h2&gt;

&lt;p&gt;Rofi is a customizable status bar for Linux that displays essential system information like time, battery, WiFi, and running programs. Think of it as a smart dashboard that sits at the top or bottom of your screen, showing exactly what you need at a glance. It's particularly popular with minimal window managers like i3 because it's lightweight and doesn't slow down your system. You can easily customize what information to show, where to place it, and how it looks through simple configuration files. Whether you want to monitor system resources, control music, or switch between tasks, Rofi handles it all in a clean, organized way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;rofi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my case, inside my &lt;a href="https://github.com/hxsggsz/dotfiles/blob/master/rofi/.config/rofi/setup.sh" rel="noopener noreferrer"&gt;rofi configuration&lt;/a&gt; I have this bash file to make the setup and install everything that it needs to run you will have to run this file with this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./setup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after that, we need a software to take screenshots here I will use maim for screenshot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;maim
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is feh
&lt;/h2&gt;

&lt;p&gt;feh is a software to manage your background image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;feh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is lxappearance
&lt;/h2&gt;

&lt;p&gt;LXAppearance is a lightweight and versatile GTK+ theme switcher that allows users to customize the visual appearance of their Linux system without requiring a full desktop environment. In our case we will use this to customize the theme and cursor with our i3wm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;lxappearance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if everything works fine your result will be something like this:&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%2Frv5vo1n68053hac4hxcn.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%2Frv5vo1n68053hac4hxcn.png" alt="the i3wm with a wide window on top with a terminal, above split in two have a browser and the vscode" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>i3wm</category>
      <category>linux</category>
      <category>productivity</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Como rodar um projeto Expo por um Virtual Device no WSL</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Mon, 04 Mar 2024 20:30:00 +0000</pubDate>
      <link>https://dev.to/devhat/como-rodar-um-projeto-expo-por-um-virtual-device-no-wsl-2i16</link>
      <guid>https://dev.to/devhat/como-rodar-um-projeto-expo-por-um-virtual-device-no-wsl-2i16</guid>
      <description>&lt;p&gt;Para seguir com esse artigo, você precisará concluir o ultimo &lt;a&gt;artigo ensinando a configurar o WSL para o Expo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. INSTALANDO O WSA
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Agora para rodar o Expo em um &lt;code&gt;Virtual Device&lt;/code&gt; no &lt;code&gt;WSL (Window Subsystem for Linux)&lt;/code&gt; nós precisamos instalar o &lt;code&gt;WSA (Windows Subsystem for Android)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Abra &lt;a href="https://github.com/MustardChef/WSABuilds?tab=readme-ov-file" rel="noopener noreferrer"&gt;este link&lt;/a&gt; e procure por essa tabela no &lt;code&gt;README&lt;/code&gt;, feito isso baixe a ultima versão estável do &lt;code&gt;WSA&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&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%2Fajxwtd391b96wnlwiqnu.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%2Fajxwtd391b96wnlwiqnu.png" alt="tabela mostrando para baixar a versao estavel do wsa"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mova o arquivo &lt;code&gt;ZIP&lt;/code&gt; que você acabou de baixar para a Root da pasta &lt;code&gt;C:\&lt;/code&gt; igual você fez com o script de &lt;code&gt;PowerShell&lt;/code&gt; no &lt;a href="https://dev.tolink%20do%20artigo%201"&gt;artigo 1&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Agora com o &lt;code&gt;ZIP&lt;/code&gt; no lugar certo extraia o arquivo com &lt;code&gt;winRAR&lt;/code&gt; ou &lt;code&gt;7z&lt;/code&gt; e exclua o arquivo &lt;code&gt;.ZIP&lt;/code&gt;, entre na pasta que acabou de ser extraída e veja se tem uma pasta dentro, se houver, recorte ela e cole-a no root do seu C: novamente.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Abra a pasta, clique com o botão direito no Install.ps1 e clique em &lt;code&gt;Executar com o PowerShell&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Caso &lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74tnainl10m7lk5kcwpw.png" rel="noopener noreferrer"&gt;você encontre esse erro&lt;/a&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%2F74tnainl10m7lk5kcwpw.png" alt="o arquivo C:\WSA_2311.400000.5.0_x64\Install.ps1 nao pode ser carregado porque a execucao de scripts foi desabilitada neste sistema. Para obter mais informacoes consulte about_Execution_policies em https://go.microsoft.com/fwlink/?linkID=135170"&gt;
&lt;/li&gt;
&lt;li&gt;Clique no arquivo &lt;code&gt;Run.bat&lt;/code&gt; e execute ele.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Depois que o comando rodar, vão aparecer algumas popups e umas caixas de dialogo clique em continue e deixe os processos carregarem. Não feche inicialmente nenhuma aba, apenas minimize.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Procure a aba que se chame &lt;code&gt;Windows Subsystem for Android&lt;/code&gt; com o titulo &lt;code&gt;System&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Vá no canto superior esquerdo e selecione &lt;code&gt;advanced settings&lt;/code&gt; e ative o &lt;code&gt;developer mode&lt;/code&gt; e permita as coisas que precisarem permitir em nível de admin.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. INSTALANDO O SDK DO ANDROID
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Nós precisamos desse &lt;code&gt;sdk&lt;/code&gt; para termos acesso as &lt;code&gt;API's Nativas do android&lt;/code&gt; pelo &lt;code&gt;javascript&lt;/code&gt;, sem isso nós não conseguiremos criar os nossos aplicativos de forma adequada. Além disso precisamos do &lt;code&gt;sdk&lt;/code&gt; para prosseguir pro próximo passo &lt;/li&gt;
&lt;li&gt;Siga o passo de instalação desse GIST &lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Rode os comandos um por um pra evitar qualquer problema.&lt;/li&gt;
&lt;li&gt;Lembre-se de trocar o path de &lt;code&gt;Sdk&lt;/code&gt; para &lt;code&gt;sdk&lt;/code&gt; pois o padrão do expo é &lt;code&gt;sdk&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;NÃO&lt;/code&gt;&lt;/strong&gt; rode o ultimo comando do &lt;code&gt;gradle&lt;/code&gt;.
## 3. INSTALANDO O ADB&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;O &lt;code&gt;ADB (Android Debug Bridge)&lt;/code&gt; é uma ferramenta de linha de comando que faz parte do &lt;code&gt;SDK&lt;/code&gt; do &lt;code&gt;Android&lt;/code&gt;. Ele cria a conexão do nosso &lt;code&gt;WSL&lt;/code&gt; ao &lt;code&gt;WSA&lt;/code&gt;, sem ele nós não conseguiremos fazer essa conexão e por consequência não conseguiremos usar o &lt;code&gt;WSA&lt;/code&gt; para rodar o nosso projeto em um &lt;code&gt;Virtual Device&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;Acesse &lt;a href="https://github.com/awake558/adb-win/tree/master/SDK_Platform-Tools_for_Linux" rel="noopener noreferrer"&gt;esse link&lt;/a&gt; e baixe a versão mais recente encontrada nesse repositório.&lt;/li&gt;

&lt;li&gt;Pegue o &lt;code&gt;ZIP&lt;/code&gt; que você acabou de baixar e coloque na &lt;code&gt;HOME&lt;/code&gt; do seu WSL&lt;/li&gt;

&lt;li&gt;Depois de jogar o seu &lt;code&gt;ZIP&lt;/code&gt; lá, extraia ele pelo terminal usando o seguinte comando:
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;unzip &amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;div &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;pre &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight plaintext"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;code&amp;gt;- Se tudo der certo, você terá a pasta &lt;span class="sb"&gt;`&lt;/span&gt;platform-tools&lt;span class="sb"&gt;`&lt;/span&gt; na home &lt;span class="k"&gt;do &lt;/span&gt;seu terminal

&lt;span class="o"&gt;![&lt;/span&gt;mostrando a pasta na home &lt;span class="k"&gt;do &lt;/span&gt;terminal]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecvf6gciwr6dthpdqbmw.png&lt;span class="o"&gt;)&lt;/span&gt;
- Acesse essa pasta e rode o seguinte comando:
&lt;span class="sb"&gt;```&lt;/span&gt;bash


./adb connect &amp;amp;lt&lt;span class="p"&gt;;&lt;/span&gt;ip-do-wsl&amp;amp;gt&lt;span class="p"&gt;;&lt;/span&gt;:&amp;amp;lt&lt;span class="p"&gt;;&lt;/span&gt;porta-do-WSA&amp;amp;gt&lt;span class="p"&gt;;&lt;/span&gt;


&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;h3&amp;gt;
  &amp;lt;a &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"como-descobrir-o-ip-do-wsl"&lt;/span&gt; &lt;span class="nv"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"#como-descobrir-o-ip-do-wsl"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &amp;lt;/a&amp;gt;
  COMO DESCOBRIR O IP DO WSL
&amp;lt;/h3&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;No seu terminal &lt;span class="k"&gt;do &lt;/span&gt;WSL, execute o mesmo comando:
&lt;span class="sb"&gt;```&lt;/span&gt;bash
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;netsh.exe interface ip show address&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;div &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;pre &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight plaintext"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;code&amp;gt;- Procure pelo &lt;span class="sb"&gt;`&lt;/span&gt;vEthernet &lt;span class="o"&gt;(&lt;/span&gt;WSL&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;

&lt;span class="o"&gt;![&lt;/span&gt;mostrando o resultado &lt;span class="k"&gt;do &lt;/span&gt;comando no terminal]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32j40187atghmdy2hn12.png&lt;span class="o"&gt;)&lt;/span&gt;

- No &lt;span class="sb"&gt;`&lt;/span&gt;Endereço IP&lt;span class="sb"&gt;`&lt;/span&gt;terá o IP &lt;span class="k"&gt;do &lt;/span&gt;WSL que precisamos usar no comando acima.
&lt;span class="c"&gt;### COMO DESCOBRIR A PORTA DO WSA&lt;/span&gt;
- Abra o WSA &lt;span class="o"&gt;(&lt;/span&gt;clique na barra de pesquisa &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;windows&lt;span class="sb"&gt;`&lt;/span&gt; e escreva &lt;span class="sb"&gt;`&lt;/span&gt;android&lt;span class="sb"&gt;`&lt;/span&gt; nele&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;![&lt;/span&gt;WSA na barra de pesquisa &lt;span class="k"&gt;do &lt;/span&gt;windows]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ho32xejy91me2tynpss.png&lt;span class="o"&gt;)&lt;/span&gt;

- Acesse a aba de Advanced settings

&lt;span class="o"&gt;![&lt;/span&gt;mostrando a aba advanced settings no wsa]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afmclgf4m0o0qecoz1hl.png&lt;span class="o"&gt;)&lt;/span&gt;

- Esses números depois &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;:&lt;span class="sb"&gt;`&lt;/span&gt; são a porta &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;WSA&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="o"&gt;![&lt;/span&gt;porta &lt;span class="k"&gt;do &lt;/span&gt;WSA]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztnyo4n46u1fsk6vtv2z.png&lt;span class="o"&gt;)&lt;/span&gt;

- caso você não encontre a porta &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;WSA&lt;span class="sb"&gt;`&lt;/span&gt; aqui, você terá que baixar a versão de &lt;span class="sb"&gt;`&lt;/span&gt;pre release&lt;span class="sb"&gt;`&lt;/span&gt; &lt;span class="k"&gt;do &lt;/span&gt;WSA.
- O padrão é &lt;span class="sb"&gt;`&lt;/span&gt;58526&lt;span class="sb"&gt;`&lt;/span&gt;, mas no seu caso pode mudar, é bom verificar.
- Pronto, agora você sabe  o IP &lt;span class="k"&gt;do &lt;/span&gt;seu WSL e a porta &lt;span class="k"&gt;do &lt;/span&gt;WSA, agora você pode rodar  o comando.
- Depois de fazer isso, você pode rodar o projeto &lt;span class="sb"&gt;`&lt;/span&gt;Expo&lt;span class="sb"&gt;`&lt;/span&gt; de novo e aperte  o botão &lt;span class="sb"&gt;`&lt;/span&gt;a&lt;span class="sb"&gt;`&lt;/span&gt; para abrir o projeto com o &lt;span class="sb"&gt;`&lt;/span&gt;WSA&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="o"&gt;![&lt;/span&gt;mostrando o expo rodando corretamente com o virtual device]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqq3pyy5uqlgy8lqtnd3.png&lt;span class="o"&gt;)&lt;/span&gt;

Finalizamos, caso esse artigo tenha sido útil para você, de uma olhada na nossa comunidade, a &lt;span class="o"&gt;[&lt;/span&gt;devhat]&lt;span class="o"&gt;(&lt;/span&gt;https://discord.gg/3gsMAEumEd&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; É aqui, que encontramos apoio, compartilhamos ideias e nos inspiramos mutuamente. A comunidade DevHatt não é apenas um conjunto de desenvolvedores, mas uma família unida pela paixão pela tecnologia e inovação.

Se você ainda não faz parte desta comunidade dinâmica, convidamos você a se juntar a nós no &lt;span class="o"&gt;[&lt;/span&gt;Discord DevHatt]&lt;span class="o"&gt;(&lt;/span&gt;https://discord.gg/3gsMAEumEd&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>wsl</category>
      <category>reactnative</category>
      <category>javascript</category>
      <category>expo</category>
    </item>
    <item>
      <title>Como Configurar o Expo no WSL</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Mon, 04 Mar 2024 20:30:00 +0000</pubDate>
      <link>https://dev.to/devhat/como-configurar-o-expo-no-wsl-bo3</link>
      <guid>https://dev.to/devhat/como-configurar-o-expo-no-wsl-bo3</guid>
      <description>&lt;h2&gt;
  
  
  pré-requisitos:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Windows 11/10&lt;/code&gt;, &lt;code&gt;Direitos de administrador no WSL2&lt;/code&gt;, &lt;code&gt;dotenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;O &lt;code&gt;Expo&lt;/code&gt; é uma ferramenta &lt;code&gt;open source&lt;/code&gt; para facilitar a sua vida na hora de criar aplicativos em &lt;code&gt;React Native&lt;/code&gt;. É possível usa-lo em qualquer sistema operacional que você quiser: &lt;code&gt;Windows&lt;/code&gt;, &lt;code&gt;Linux&lt;/code&gt; e &lt;code&gt;MacOs&lt;/code&gt; mas e se eu quiser usa-lo no meu &lt;code&gt;WSL (Windows Subsystem of Linux)&lt;/code&gt;? Bem... até a data de criação desse artigo isso seria uma tarefa que &lt;strong&gt;beiraria&lt;/strong&gt; o impossível, mas nós da &lt;a href="https://discord.gg/3gsMAEumEd" rel="noopener noreferrer"&gt;Devhat&lt;/a&gt; nos juntamos para te ajudar nessa missão!&lt;/p&gt;

&lt;h3&gt;
  
  
  observação
&lt;/h3&gt;

&lt;p&gt;Esse artigo será separado em duas partes: a primeira parte ensinará a configurar e conectar o &lt;code&gt;Expo&lt;/code&gt; ao seu celular pelo &lt;code&gt;QR code&lt;/code&gt;, a segunda parte ensinará a conectar o &lt;code&gt;Expo&lt;/code&gt; a um &lt;code&gt;Virtual Device&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1: ABRIR AS PORTAS DE IP DO SEU COMPUTADOR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dentro deste GIST  tem um arquivo &lt;code&gt;Powershell&lt;/code&gt;, você precisará baixar ou copiar esse arquivo (garanta que é um arquivo &lt;code&gt;.ps1&lt;/code&gt; e não um arquivo &lt;code&gt;.txt&lt;/code&gt;) .
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Caso  você precise trocar a porta, edite a variável na 6ª linha, entre parênteses.
```bash
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;$ports = @(8081, 8082);&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Pegue esse arquivo e jogue ele na Root da pasta C  `C:\`.

![foto mostrando o script powershell na root da pasta C](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mkhbk3bfc9hf43mf41b.png)
- Agora abra o `PowerShell` como administrador (precisa ser como administrador).

![mostrando como abrir o powershell como adminstrador](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obiejwpxsxwv5ysou2rv.png)

- Agora com o seu `PowerShell` como `administrador` aberto, você irá fazer algumas coisas:
    - feche todas as abas do `WSL` abertas no seu computador e execute o seguinte comando.
```bash


wsl --shutdown  


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;essa mensagem de erro aparecerá caso você pule essa etapa.
&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%2F5pqvi2lnssz4kl79mjmz.png" alt="Nenhum objeto MSFT_NetFIrewallRule encontrado com a propriedade 'DisplayName' igual a 'WSL2 Forwarded Prots'. Verifique o valor da propriedae e tente de novo."&gt;
&lt;/li&gt;
&lt;li&gt;Execute o seguinte comando.
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;powershell.exe -ExecutionPolicy Bypass -f C:\forward_wsl2_ports.ps1&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- se tudo der certo, esse será o resultado no seu `terminal`.

![sucesso executando o script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6z8wklpl8xi3r061s7rx.png)

Se tudo deu certo até aqui significa que você conseguiu configurar o `WSL` para rodar o `Expo`! Agora precisamos configurar o `Expo` para rodar o `WSL`.

## 2. CRIANDO E CONFIGURANDO UM PROJETO EXPO

- Abra o seu terminal com `WSL` e rode o seguinte comando para criar um projeto `Expo`.
```bash


npx create-expo-app


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Após criar o projeto você irá criar um script node chamado &lt;code&gt;get_network_local_ip.js&lt;/code&gt; na Root do seu projeto.&lt;/li&gt;
&lt;li&gt;Dentro desse arquivo, &lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;/li&gt;

&lt;li&gt;Nesse &lt;code&gt;script&lt;/code&gt; nós usamos duas &lt;code&gt;variáveis de ambiente&lt;/code&gt; chamadas: &lt;code&gt;LOCAL_CONNECTION_INTERFACE_NAME&lt;/code&gt; e &lt;code&gt;LOCAL_CONNECTION_IPV4_SEARCH&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;Nós precisamos dessas duas &lt;code&gt;envs&lt;/code&gt; para poder rodar o projeto no WSL, vamos aprender a achar os valores necessários pras essas &lt;code&gt;envs&lt;/code&gt;, para isso vamos rodar o seguinte comando 
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;netsh.exe interface ip show address&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;div &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;pre &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight plaintext"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;code&amp;gt;- Procure pela &lt;span class="sb"&gt;`&lt;/span&gt;Ethernet&lt;span class="sb"&gt;`&lt;/span&gt; que tenha o &lt;span class="sb"&gt;`&lt;/span&gt;DHCP habilitado&lt;span class="sb"&gt;`&lt;/span&gt; como sim, no meu caso é apenas &lt;span class="sb"&gt;`&lt;/span&gt;Ethernet&lt;span class="sb"&gt;`&lt;/span&gt;, mas no seu caso pode ser &lt;span class="sb"&gt;`&lt;/span&gt;Ethernet 1&lt;span class="sb"&gt;`&lt;/span&gt; ou &lt;span class="sb"&gt;`&lt;/span&gt;Ethernet 2&lt;span class="sb"&gt;`&lt;/span&gt; ou &lt;span class="sb"&gt;`&lt;/span&gt;WiFi&lt;span class="sb"&gt;`&lt;/span&gt;, etc. 

&lt;span class="o"&gt;![&lt;/span&gt;imagem mostrando os dados que precisamos pro tutorial]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8aqkv2ttx2hyul8ed7q.png&lt;span class="o"&gt;)&lt;/span&gt;

- Dessas informações nós queremos o &lt;span class="sb"&gt;`&lt;/span&gt;Ethernet&lt;span class="sb"&gt;`&lt;/span&gt; e o &lt;span class="sb"&gt;`&lt;/span&gt;endereço IP&lt;span class="sb"&gt;`&lt;/span&gt; pro nosso &lt;span class="sb"&gt;`&lt;/span&gt;script&lt;span class="sb"&gt;`&lt;/span&gt; e esses serão os valores das nossas &lt;span class="sb"&gt;`&lt;/span&gt;envs&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
- Agora vamos criar o arquivo de &lt;span class="sb"&gt;`&lt;/span&gt;envs&lt;span class="sb"&gt;`&lt;/span&gt; e preenche-las para o nosso script
- Na Root &lt;span class="k"&gt;do &lt;/span&gt;seu projeto crie um arquivo chamado &lt;span class="sb"&gt;`&lt;/span&gt;.env.local&lt;span class="sb"&gt;`&lt;/span&gt; e coloque as seguintes &lt;span class="sb"&gt;`&lt;/span&gt;envs&lt;span class="sb"&gt;`&lt;/span&gt; lá:
&lt;span class="sb"&gt;```&lt;/span&gt;bash


&lt;span class="nv"&gt;LOCAL_CONNECTION_INTERFACE_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Ethernet"&lt;/span&gt;
&lt;span class="nv"&gt;LOCAL_CONNECTION_IPV4_SEARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Endere"&lt;/span&gt;


&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;h3&amp;gt;
  &amp;lt;a &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"observa%C3%A7%C3%B5es-importantes"&lt;/span&gt; &lt;span class="nv"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"#observa%C3%A7%C3%B5es-importantes"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &amp;lt;/a&amp;gt;
  OBSERVAÇÕES IMPORTANTES!!!!!
&amp;lt;/h3&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;Por que a segunda &amp;lt;code&amp;gt;env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;LOCAL_CONNECTION_IPV4_SEARCH&amp;lt;/code&amp;gt; tem como valor &amp;lt;code&amp;gt;Endere&amp;lt;/code&amp;gt; e não &amp;lt;code&amp;gt;Endereço IP&amp;lt;/code&amp;gt;?

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt; Alguns terminais podem ter um erro de &amp;lt;code&amp;gt;utf-8&amp;lt;/code&amp;gt; e podem não reconhecer a letra &amp;lt;code&amp;gt;ç&amp;lt;/code&amp;gt;, nesse caso ele não vai achar o valor correto na hora de executar o script e irá dar &amp;lt;strong&amp;gt;&amp;lt;code&amp;gt;erro&amp;lt;/code&amp;gt;&amp;lt;/strong&amp;gt;! Por isso colocaremos apenas &amp;lt;code&amp;gt;Endere&amp;lt;/code&amp;gt; na &amp;lt;code&amp;gt;env&amp;lt;/code&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Se a linguagem &lt;span class="k"&gt;do &lt;/span&gt;seu sistema estiver em inglês, o &amp;lt;code&amp;gt;windows&amp;lt;/code&amp;gt; retornará o IP em &amp;lt;code&amp;gt;IP Adress&amp;lt;/code&amp;gt; em vez de &amp;lt;code&amp;gt;Endereço IP&amp;lt;/code&amp;gt;, caso essa seja a sua situação, troque no &amp;lt;code&amp;gt;.env.local&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;Endere&amp;lt;/code&amp;gt; por &amp;lt;code&amp;gt;IP Adress&amp;lt;/code&amp;gt; senão, você não conseguirá rodar o projeto.&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;h2&amp;gt;
  &amp;lt;a &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"3-comando-para-rodar-o-expo-no-wsl"&lt;/span&gt; &lt;span class="nv"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"#3-comando-para-rodar-o-expo-no-wsl"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &amp;lt;/a&amp;gt;
  3. COMANDO PARA RODAR O EXPO NO WSL
&amp;lt;/h2&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;
&amp;lt;p&amp;gt;Após essas configurações nós precisaremos apenas criar o comando no &amp;lt;code&amp;gt;package.json&amp;lt;/code&amp;gt; para rodar o projeto. Faremos da seguinte forma:&amp;lt;/p&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt; Abriremos o arquivo &amp;lt;code&amp;gt;package.json&amp;lt;/code&amp;gt; &lt;span class="k"&gt;do &lt;/span&gt;nosso projeto&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Colocaremos o seguinte comando na parte de &amp;lt;code&amp;gt;script&amp;lt;/code&amp;gt; 
&lt;span class="sb"&gt;```&lt;/span&gt;bash
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;&lt;span class="s2"&gt;"start:wsl"&lt;/span&gt;: &lt;span class="s2"&gt;"REACT_NATIVE_PACKAGER_HOSTNAME=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;node get_network_local_ip.js&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; expo start"&lt;/span&gt;,&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;div &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;pre &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"highlight plaintext"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;lt;code&amp;gt;
- Ficando dessa forma

&lt;span class="o"&gt;![&lt;/span&gt;scripts no package.json]&lt;span class="o"&gt;(&lt;/span&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6kcdpr1dqn1gl05zs7g.png&lt;span class="o"&gt;)&lt;/span&gt;

- Agora rode o comando que acabamos de criar no terminal com &lt;span class="sb"&gt;`&lt;/span&gt;npm run start:wsl&lt;span class="sb"&gt;`&lt;/span&gt;
- Se tudo der certo, ele irá mostrar um &lt;span class="sb"&gt;`&lt;/span&gt;QR code&lt;span class="sb"&gt;`&lt;/span&gt; na tela, baixe o aplicativo &lt;span class="sb"&gt;`&lt;/span&gt;Expo GO&lt;span class="sb"&gt;`&lt;/span&gt; e escaneie esse &lt;span class="sb"&gt;`&lt;/span&gt;QR code&lt;span class="sb"&gt;`&lt;/span&gt; e pronto, você tá rodando o Expo pelo &lt;span class="sb"&gt;`&lt;/span&gt;WSL&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

Caso você goste dessa opção de rodar o seu projeto no seu celular você pode parar por aqui, mas caso você queira rodar o seu projeto no seu computador e  o melhor de tudo, &lt;span class="k"&gt;**&lt;/span&gt;SEM PRECISAR DE ANDROID STUDIO PARA ISSO&lt;span class="k"&gt;**&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;acesse a parte 2 desse artigo e entenda como.]&lt;span class="o"&gt;(&lt;/span&gt;https://dev.to/devhat/como-rodar-um-projeto-expo-por-um-virtual-device-no-wsl-2i16&lt;span class="o"&gt;)&lt;/span&gt;

Caso esse artigo tenha sido útil para você, de uma olhada na nossa comunidade, a &lt;span class="o"&gt;[&lt;/span&gt;devhat]&lt;span class="o"&gt;(&lt;/span&gt;https://discord.gg/3gsMAEumEd&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; É aqui, que encontramos apoio, compartilhamos ideias e nos inspiramos mutuamente. A comunidade DevHatt não é apenas um conjunto de desenvolvedores, mas uma família unida pela paixão pela tecnologia e inovação.

Se você ainda não faz parte desta comunidade, convidamos você a se juntar a nós no &lt;span class="o"&gt;[&lt;/span&gt;Discord DevHatt]&lt;span class="o"&gt;(&lt;/span&gt;https://discord.gg/3gsMAEumEd&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>expo</category>
      <category>reactnative</category>
      <category>wsl</category>
      <category>javascript</category>
    </item>
    <item>
      <title>TIL - Today I Learn 12-11 18-11</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Fri, 08 Dec 2023 20:30:00 +0000</pubDate>
      <link>https://dev.to/devhat/til-today-i-learn-12-11-18-11-parte-2-45n4</link>
      <guid>https://dev.to/devhat/til-today-i-learn-12-11-18-11-parte-2-45n4</guid>
      <description>&lt;h1&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/piluvitu"&gt;@piluvitu&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;ㅤ&lt;/p&gt;

&lt;h3&gt;
  
  
  .env
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Para quem não sabe o seu sistema por padrão tem variáveis de ambiente, que servem para armazenar alguma informação e padronizar o acesso dela pela mesma.

&lt;ul&gt;
&lt;li&gt;É possível inserir váriáveis no próprio sistema sem precisar de nem uma abstração, mas ela não são inserções persistentes, por isso a fim de facilitar a definição dessas variáveis, existe o módulo do NPM &lt;code&gt;.env&lt;/code&gt;, que foi criado a fim de facilitar isso.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Para usa-lo tudo que é necessário fazer é criar um arquivo &lt;code&gt;.env&lt;/code&gt; na raiz do projeto e inserir as variáveis com a seguinte sintaxe:&lt;/li&gt;
&lt;li&gt;O modelo é chave - valor, com a chave sendo por padrão MAIUSCULA&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;OPENIA_KEY = informação sensivel URL = localhost PORT = 3000&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Muito se é discutido em upar ou não o &lt;code&gt;.env&lt;/code&gt; então devemos explicar o comportamento dele no sistema:&lt;/li&gt;
&lt;li&gt; &lt;code&gt;.env.development&lt;/code&gt; - serve para um ambiente de desenvolvimento&lt;/li&gt;
&lt;li&gt; &lt;code&gt;.env.example&lt;/code&gt; - serve como um arquivo de exemplo&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.env&lt;/code&gt; - é o arquivo padrão, que em teoria seria usado para inserir as variáveis de produção.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Muitos dos provedores onde podemos subir nossas aplicações de maneira facilitada, disponibilizam uma parte para inserirmos as variáveis de ambiente que serão utilizadas na nossa aplicação e por ordem de importância essas variáveis inseridas antes do deploy, são as que vão realmente sobrepor as que ficam no &lt;code&gt;.env&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ㅤ&lt;/p&gt;

&lt;h3&gt;
  
  
  GIT
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Pensando nisso o GitHub fez um &lt;a href="https://docs.github.com/pt/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository"&gt;artigo&lt;/a&gt;  que sugere usar uma ferramenta do próprio git ou uma ferramenta open-source

&lt;ul&gt;
&lt;li&gt;Falando em git, descobri que tem como renomear arquivos usando o próprio git usando o comando &lt;code&gt;git mv&lt;/code&gt;:
&lt;code&gt;git mv &amp;lt;nome do arquivo atual&amp;gt; &amp;lt;novo nome do arquivo&amp;gt;&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ㅤ&lt;/p&gt;

&lt;h3&gt;
  
  
  Absolute PATH
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Eu nunca tinha parado para pensar que, quando queremos acessar algum arquivo que está fora da pasta atual:
1 - Navegamos para fora da pasta atual
2 - Entramos na pasta do arquivo
3 - Por fim referenciamos o mesmo.&lt;/li&gt;
&lt;li&gt;O nome disso é relative PATH e percebemos que priemeiro voltamos para depois seguir em frente, que em um projeto com muitos arquivos gera um PATH longo e nada didático

&lt;ul&gt;
&lt;li&gt;Já o absolute PATH é o caminho absoluto, que toma como padrão sempre sair da raiz do projeto e ir para o arquivo desejado, encurtando o PATH e deixando didático&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Fica a dúvida de como eles fazem para referenciar o diretório raiz do projeto ?&lt;/li&gt;
&lt;li&gt;Existem várias soluções no mercado, mas a microsoft desenvolveu os arquivos &lt;code&gt;jsconfig.json&lt;/code&gt; e &lt;code&gt;tsconfig.json&lt;/code&gt; que servem para indicar ao editor que ali é a raiz do projeto.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ㅤ&lt;/p&gt;

&lt;h3&gt;
  
  
  TypeScript
&lt;/h3&gt;

&lt;p&gt;Por fim o typescript, meu primeiro contato sem auxilio de curso tem sido aqui na devhat, no projeto octopost, mal conheço e já considero pacas esse TS, estou fazendo minha introdução pelo curso da origamid.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Estudei conceitos básicos de annotation, inferface e inferência.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Annotation&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;produt0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Livro&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;prec0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;856&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;No TypeScript podemos tipar, e isso é interessante pois evitamos erros na hora de lidar com os dados&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Interface
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Interface&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;carr0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;marca&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="nx"&gt;portas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="nx"&gt;motor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;marca&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;audi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;portas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;motor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;carro&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;marca&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;245&lt;/span&gt;
&lt;span class="nx"&gt;carro&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;portas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;carro&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;motor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aspirado&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;alumínio&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Inferência
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Mesmo sem declarar a tipagem, alguns tipos de variáveis o TS, 
já identifica a tipagem correta */&lt;/span&gt;

&lt;span class="c1"&gt;// Annotation&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;produtos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Livro&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;preco&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;856&lt;/span&gt;

&lt;span class="nx"&gt;preco&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jajaja&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Interface&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;carro&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;marca&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;audi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;portas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;motor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;carro&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;marca&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;245&lt;/span&gt;
&lt;span class="nx"&gt;carro&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;portas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;carro&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;motor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aspirado&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;alumínio&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Como pode-se ver no exemplo acima, podemos deixar de declarar a tipagem de alguns dados que o proprio TypeScript já reconhece.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;somar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;soma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;soma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;No exemplo acima é possivel ver um otimo motivo para tipar, pois saberemos o que é para chegar no parâmetro e o que será retornado&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  FunFact
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;O vsCode tem intelisense de JS graças ao TS nativamente no editor, caso queira ter um pouco da experiência de usar TS no JS é só adicionar &lt;code&gt;//@ts-check&lt;/code&gt; na primeira linha do seu arquivo JS e o editor vai passar a indicar alguns erros que o TS pegaria no seu código&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/hxsggsz"&gt;@hxsggsz&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;ㅤ&lt;/p&gt;

&lt;h3&gt;
  
  
  Typescript
&lt;/h3&gt;

&lt;p&gt;Essa semana eu estudei bastante &lt;code&gt;Typescript&lt;/code&gt; pra tentar resolver um problema. Durante esse estudo eu descobri alguns &lt;code&gt;Utilities Types&lt;/code&gt; criados pelo próprio time do &lt;code&gt;Typescript&lt;/code&gt; pra facilitar a nossa vida difícil de bugs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Como esses "Utilities Types" funcionam
&lt;/h4&gt;

&lt;p&gt;Todos eles se  beneficiam dos "Generics", algo muito útil na hora de tipagem e que é presente em diversas linguagens estaticamente tipadas.&lt;/p&gt;

&lt;h4&gt;
  
  
  Partial
&lt;/h4&gt;

&lt;p&gt;O que ele faz? Ele transforma tudo em  um objeto opcional. Olha esse exemplo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
    &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
    &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Agora se a gente taca essa &lt;code&gt;interface&lt;/code&gt; no &lt;code&gt;Partial&lt;/code&gt; assim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;OptionalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;É como se a gente fizesse isso&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
    &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
    &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Required
&lt;/h4&gt;

&lt;p&gt;Existe o oposto do &lt;code&gt;Partial&lt;/code&gt; que é o &lt;code&gt;Required&lt;/code&gt;, e como o nome já diz, ele transforma todos os &lt;code&gt;tipos&lt;/code&gt; que ele receber para &lt;code&gt;necessário fornecer&lt;/code&gt;, até os que originalmente são &lt;code&gt;opicionais&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;OptionalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;RequiredUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;OptionalUser&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;E agora o tipo &lt;code&gt;RequiredUser&lt;/code&gt; fica assim&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
    &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
    &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Omit
&lt;/h4&gt;

&lt;p&gt;Agora imagina que a gente quer omitir o nome da interface &lt;code&gt;User&lt;/code&gt; sem alterar a interface original, essa é a função do &lt;code&gt;Omit&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserWithouName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pick
&lt;/h4&gt;

&lt;p&gt;E tem o contrario também, imagina que a gente quer pegar apenas a propriedade nome da interface mas sem alterar a interface original, o &lt;code&gt;Pick&lt;/code&gt; serve pra isso.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Se você quiser pegar ou excluir mais de uma propriedade, basta usar o pipeline&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// ou &lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;É possível combinar todos esses &lt;code&gt;Utilities Types&lt;/code&gt; também&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserNameOptional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  ReturnType
&lt;/h4&gt;

&lt;p&gt;Agora imagina que a gente tem esse tipagem de função aqui:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;HandleSomething&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;E você precisa ter a tipagem do retorno dessa função, pra isso existe o &lt;code&gt;ReturnType&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ReturnOfHandleSomething&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HandleSomething&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Awaited
&lt;/h4&gt;

&lt;p&gt;Imagina que você tem uma tipagem que é uma promise&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;AwaitSomething&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;E você quer a tipagem que essa &lt;code&gt;promise&lt;/code&gt; vai retornar, pra isso serve o &lt;code&gt;Awaited&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Awaited&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AwaitSomething&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Agora um caso um pouco mais real, você tipou uma função que retorna uma &lt;code&gt;promise&lt;/code&gt; e você quer separar um &lt;code&gt;type&lt;/code&gt; diferente o retorno dessa função, simples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserNameOptional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;



&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Awaited&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserNameOptional&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Um exemplo com uma função real&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;awaitSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;demorei mas cheguei&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// string&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Awaited&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;awaitSomething&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>git</category>
      <category>env</category>
      <category>typescript</category>
    </item>
    <item>
      <title>TIL - Today I Learn 12-11 18-11</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Fri, 01 Dec 2023 20:30:00 +0000</pubDate>
      <link>https://dev.to/hxsggsz/til-today-i-learn-12-11-18-11-k4l</link>
      <guid>https://dev.to/hxsggsz/til-today-i-learn-12-11-18-11-k4l</guid>
      <description>&lt;h2&gt;
  
  
  Alvaro
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Make System Under Test
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;O nome vem de Make System Under Test` (fazer sistema sob teste).&lt;/li&gt;
&lt;li&gt;Isso é usado em &lt;code&gt;Testes Unitários&lt;/code&gt; para criar uma função que retorna uma &lt;code&gt;Instância da Classe ou Componente&lt;/code&gt; que está sendo testado. &lt;/li&gt;
&lt;li&gt;Vocês devem encontrar exemplos disso com &lt;code&gt;Classes e Testes em Java&lt;/code&gt;, eles preparam um &lt;code&gt;setup/Factory&lt;/code&gt; para fazer &lt;code&gt;testes unitários&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A função &lt;code&gt;MakeSut&lt;/code&gt; recebe &lt;code&gt;parâmetros&lt;/code&gt; que podem ser usados para personalizar o comportamento ou a aparência do componente, e dentro dessa função você cria a lógica de renderização do componente no contexto dos testes, reutilizando a função entre vários testes para não precisar reescrever o código de setup em cada teste.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`javascript&lt;/p&gt;

&lt;p&gt;// Uma função que vai retornar a renderização do seu teste toda vez que for chamada&lt;br&gt;
const makeSut = ({&lt;br&gt;
  prop = false, // Aqui você seta algum parametro padrão para todos os testes. É útil quando alguma prop é obrigatória&lt;br&gt;
  ...props&lt;br&gt;
}:  Partial)  =&amp;gt;  {&lt;br&gt;
// Aqui fica a lógica de renderização do componente. Pode ser qualquer uma.&lt;br&gt;
  return render();&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;describe('Component',  ()  =&amp;gt;  {&lt;br&gt;
  describe('when',  ()  =&amp;gt;  {&lt;br&gt;
    it('do',  ()  =&amp;gt;  {&lt;br&gt;
      // Você pode modificar os parâmetros para adaptar a renderização do componente&lt;br&gt;
      makeSut({ prop:  false  });&lt;br&gt;
    });&lt;br&gt;
});&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Nicoletti
&lt;/h2&gt;

&lt;h2&gt;
  
  
  ThreeJs
&lt;/h2&gt;

&lt;p&gt;Primeiramente essa semana dei uma pegada no &lt;strong&gt;ThreeJs&lt;/strong&gt;. A parte interessante aqui é que ele também está sendo muito usado no &lt;strong&gt;Electron&lt;/strong&gt; &lt;a href="[https://medium.com/@sharadghimire5551/getting-started-with-three-js-and-electron-js-9ae49b1d3f59](https://medium.com/@sharadghimire5551/getting-started-with-three-js-and-electron-js-9ae49b1d3f59"&gt;Aqui tem um Get Started&lt;/a&gt; ) que eu tava dando uma lida.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Também dei uma avançada no de &lt;strong&gt;Git&lt;/strong&gt; Essa semana dei uma olhada pra alguns &lt;em&gt;Rewriting history&lt;/em&gt; que são bem uteis, principalmente pra mim que envio commit errado toda hora. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Changing the Last Commit:&lt;/strong&gt; &lt;code&gt;git commit --amend&lt;/code&gt; O amend pode ser usado para "Editar" seu último commit enviado sem precisar enviar um novo. Ele também serve caso queira apenas trocar o comentário do commit. Mas vale uma atenção especial já que o &lt;code&gt;--amend&lt;/code&gt; não apenas "editará" o commit antigo, e sim substituirá o antigo por um novo, trocando assim suas hashs já que o git tem a ideia de ser imutável. &lt;code&gt;git commit --amend -m "an updated commit message"&lt;/code&gt; &lt;a href="https://www.atlassian.com/git/tutorials/rewriting-history](https://www.atlassian.com/git/tutorials/rewriting-history"&gt;Aqui deixo um dos materiais de referencia&lt;/a&gt; ) caso queiram entender mais afundo.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Andre Almeida
&lt;/h2&gt;

&lt;h2&gt;
  
  
  variaveis scss
&lt;/h2&gt;

&lt;p&gt;Se importar o arquivo que possui as variáveis scss dessa forma &lt;code&gt;@use '../../styles/global' as *;&lt;/code&gt; Voce nao precisa usar o prefixo &lt;code&gt;global.$varname&lt;/code&gt;, é só chamar &lt;code&gt;$varname&lt;/code&gt; evitando a repetição desnecessária.&lt;/p&gt;

&lt;h2&gt;
  
  
  Zoldyck
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Error Boundary
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Error boundary&lt;/code&gt; é um componente especial, utilizado na captura de erros gerados durante a renderização da sua aplicação.&lt;/p&gt;

&lt;p&gt;Com ele você consegue por exemplo renderizar uma UI específica para quando houver um erro na aplicação, também pode capturar esses erros para um sistema de log.&lt;/p&gt;

&lt;p&gt;Uma curiosidade sobre o componente é que apesar do &lt;code&gt;React&lt;/code&gt; atualmente usar &lt;code&gt;Function Component&lt;/code&gt; por padrão ele só pode ser escrito como um &lt;code&gt;Class Component&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Exemplo de um &lt;code&gt;Error Boundary Component&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`ts&lt;/p&gt;

&lt;p&gt;class ErrorBoundary extends React.Component {&lt;br&gt;&lt;br&gt;
  constructor(props) {&lt;br&gt;&lt;br&gt;
    super(props);&lt;br&gt;&lt;br&gt;
    this.state = { hasError: false };&lt;br&gt;&lt;br&gt;
  }&lt;br&gt;&lt;br&gt;
  static getDerivedStateFromError(error) {&lt;br&gt;&lt;br&gt;
    return { hasError: true };&lt;br&gt;&lt;br&gt;
  }&lt;br&gt;&lt;br&gt;
  componentDidCatch(error, info) {&lt;br&gt;&lt;br&gt;
    logErrorToMyService(error, info.componentStack);&lt;br&gt;&lt;br&gt;
  }&lt;br&gt;&lt;br&gt;
  render() {&lt;br&gt;&lt;br&gt;
    if (this.state.hasError) {&lt;br&gt;&lt;br&gt;
    return this.props.fallback;&lt;br&gt;&lt;br&gt;
    }&lt;br&gt;&lt;br&gt;
    return this.props.children;&lt;br&gt;&lt;br&gt;
  }&lt;br&gt;&lt;br&gt;
}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>typescript</category>
      <category>react</category>
      <category>git</category>
    </item>
    <item>
      <title>TIL - Today I Learn 12-11 18-11</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Fri, 01 Dec 2023 20:30:00 +0000</pubDate>
      <link>https://dev.to/devhat/til-today-i-learn-12-11-18-11-1mdf</link>
      <guid>https://dev.to/devhat/til-today-i-learn-12-11-18-11-1mdf</guid>
      <description>&lt;h2&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/alvarogfn"&gt;@alvarogfn&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Make System Under Test
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;O nome vem de &lt;code&gt;Make System Under Test&lt;/code&gt; (fazer sistema sob teste).&lt;/li&gt;
&lt;li&gt;Isso é usado em &lt;code&gt;Testes Unitários&lt;/code&gt; para criar uma função que retorna uma &lt;code&gt;Instância da Classe ou Componente&lt;/code&gt; que está sendo testado. &lt;/li&gt;
&lt;li&gt;Vocês devem encontrar exemplos disso com &lt;code&gt;Classes e Testes em Java&lt;/code&gt;, eles preparam um &lt;code&gt;setup/Factory&lt;/code&gt; para fazer &lt;code&gt;testes unitários&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A função &lt;code&gt;MakeSut&lt;/code&gt; recebe &lt;code&gt;parâmetros&lt;/code&gt; que podem ser usados para personalizar o comportamento ou a aparência do componente, e dentro dessa função você cria a lógica de renderização do componente no contexto dos testes, reutilizando a função entre vários testes para não precisar reescrever o código de setup em cada teste.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Uma função que vai retornar a renderização do seu teste toda vez que for chamada&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;makeSut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Aqui você seta algum parametro padrão para todos os testes. É útil quando alguma prop é obrigatória&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
&lt;span class="p"&gt;}:&lt;/span&gt;  &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// Aqui fica a lógica de renderização do componente. Pode ser qualquer uma.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;when&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;do&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Você pode modificar os parâmetros para adaptar a renderização do componente&lt;/span&gt;
      &lt;span class="nf"&gt;makeSut&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="kc"&gt;false&lt;/span&gt;  &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/nicolettifps"&gt;@nicolettifps&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  ThreeJs
&lt;/h2&gt;

&lt;p&gt;Primeiramente essa semana dei uma pegada no &lt;strong&gt;ThreeJs&lt;/strong&gt;. A parte interessante aqui é que ele também está sendo muito usado no &lt;strong&gt;Electron&lt;/strong&gt; &lt;a href="https://medium.com/@sharadghimire5551/getting-started-with-three-js-and-electron-js-9ae49b1d3f59"&gt;Aqui tem um Get Started&lt;/a&gt; que eu tava dando uma lida.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Também dei uma avançada no de &lt;strong&gt;Git&lt;/strong&gt; Essa semana dei uma olhada pra alguns &lt;em&gt;Rewriting history&lt;/em&gt; que são bem uteis, principalmente pra mim que envio commit errado toda hora. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Changing the Last Commit:&lt;/strong&gt; &lt;code&gt;git commit --amend&lt;/code&gt; O amend pode ser usado para "Editar" seu último commit enviado sem precisar enviar um novo. Ele também serve caso queira apenas trocar o comentário do commit. Mas vale uma atenção especial já que o &lt;code&gt;--amend&lt;/code&gt; não apenas "editará" o commit antigo, e sim substituirá o antigo por um novo, trocando assim suas hashs já que o git tem a ideia de ser imutável. &lt;code&gt;git commit --amend -m "an updated commit message"&lt;/code&gt; &lt;a href="https://www.atlassian.com/git/tutorials/rewriting-history](https://www.atlassian.com/git/tutorials/rewriting-history"&gt;Aqui deixo um dos materiais de referencia&lt;/a&gt; ) caso queiram entender mais afundo.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/aalmeida00"&gt;@aalmeida00&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  variaveis scss
&lt;/h2&gt;

&lt;p&gt;Se importar o arquivo que possui as variáveis scss dessa forma &lt;code&gt;@use '../../styles/global' as *;&lt;/code&gt; Voce nao precisa usar o prefixo &lt;code&gt;global.$varname&lt;/code&gt;, é só chamar &lt;code&gt;$varname&lt;/code&gt; evitando a repetição desnecessária.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/zoldyzdk"&gt;@zoldyzdk&lt;/a&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Error Boundary
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Error boundary&lt;/code&gt; é um componente especial, utilizado na captura de erros gerados durante a renderização da sua aplicação.&lt;/p&gt;

&lt;p&gt;Com ele você consegue por exemplo renderizar uma UI específica para quando houver um erro na aplicação, também pode capturar esses erros para um sistema de log.&lt;/p&gt;

&lt;p&gt;Uma curiosidade sobre o componente é que apesar do &lt;code&gt;React&lt;/code&gt; atualmente usar &lt;code&gt;Function Component&lt;/code&gt; por padrão ele só pode ser escrito como um &lt;code&gt;Class Component&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Exemplo de um &lt;code&gt;Error Boundary Component&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;getDerivedStateFromError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  
  &lt;span class="nf"&gt;componentDidCatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="nf"&gt;logErrorToMyService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;componentStack&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasError&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fallback&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>todayilearned</category>
      <category>react</category>
      <category>typescript</category>
      <category>github</category>
    </item>
    <item>
      <title>useLongPress Custom Hook</title>
      <dc:creator>Victor Hugo</dc:creator>
      <pubDate>Mon, 10 Jul 2023 01:53:32 +0000</pubDate>
      <link>https://dev.to/hxsggsz/uselongpress-custom-hook-521n</link>
      <guid>https://dev.to/hxsggsz/uselongpress-custom-hook-521n</guid>
      <description>&lt;p&gt;Hello! My name is Victor Hugo and today i will show you why and how to use the useLongPress custom hook!&lt;/p&gt;

&lt;p&gt;Events on frontend is the basic when you start to learn javascript, manipulating the DOM with clicks and forms is the basic, but sometimes we need something a litle bit more complex, a example is the double click that we can use the onDoubleClick event but what if we need to handle a long press, when the user click and hold the button, how do we handle this? This is the reason that we use the useLongPress hook! &lt;/p&gt;

&lt;p&gt;First of all, let's start a new blank project, fell free to use any React framework that you want. I will use Vite with Typescript, if you're using Javascript just ignore the types, it will work in the same way.&lt;/p&gt;

&lt;p&gt;Now that we already have create the project, let's create a folder named "hooks", in there we will store all custom hooks that we will create, in this example will be only one. We will create a new file with the name of the hook dot ts (useLongPress.ts), if you're using only javascript, the extension of the file need to be dot js (useLongPress.js).&lt;/p&gt;

&lt;p&gt;Note that how it is a custom hook, it will not have any HTML and CSS inside only logic with typescript, that's why we're using the .ts extension and  not .tsx.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PhxqnCqq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ig9dw0xwhq96pj6w4c.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PhxqnCqq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ig9dw0xwhq96pj6w4c.PNG" alt="folder named hook with one file inside" width="226" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, we will create and export the hook with the state and refs that we will use in this hook, this is how it will looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useLongPress&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isLongPress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timerRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have setup everything that we will need to create this hook, let's make the first function of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;startPressTimer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;isLongPress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; 
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;timerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;isLongPress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;// this need to be here&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;longpress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function will see the user click and will tell us if is a longpress or just a normal click.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do he do that?
&lt;/h2&gt;

&lt;p&gt;On every click he wil see if the user stop the click after 500 ms or not, if yes he will set the isLongPress ref as true, else it will be just a normal click and the ref will be false. &lt;/p&gt;

&lt;h2&gt;
  
  
  But how do he will know when the user will click?
&lt;/h2&gt;

&lt;p&gt;We will create the functions that will do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;// click actions here&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLongPress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;// longpress actions here&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;longpress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnMouseDown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;startPressTimer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnMouseUp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnTouchStart&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;startPressTimer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnTouchEnd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will pass this functions to the onClick, onMouseDown, onMouseUp, onTouchStart and onTouchEnd events. Everytime the user touch or mouse down the button, it will trigger the startPressTimer function. The handleOnClick function is where we set the onClick event or longpress event, you can put specific logic inside and outside the if or just set the event state there like in this example.&lt;/p&gt;

&lt;p&gt;The final step, return all this functions from the hook to we use whenever we want, it need to be &lt;strong&gt;exactly&lt;/strong&gt; in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;handlers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onMouseDown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnMouseDown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onMouseUp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnMouseUp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onTouchStart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnTouchStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onTouchEnd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnTouchEnd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way will be more easy to pass all the functions to the respectives events.&lt;/p&gt;

&lt;h2&gt;
  
  
  The final result will be like this:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useLongPress&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isLongPress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timerRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;startPressTimer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;isLongPress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;timerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;isLongPress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;// this need to be here&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;longpress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;// click actions here&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLongPress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;// longpress actions here&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;setEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;longpress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnMouseDown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;startPressTimer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnMouseUp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnTouchStart&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;startPressTimer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleOnTouchEnd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;handlers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onMouseDown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnMouseDown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onMouseUp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnMouseUp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onTouchStart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnTouchStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;onTouchEnd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleOnTouchEnd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="err"&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;h2&gt;
  
  
  Now, how do we use this hook in any component?
&lt;/h2&gt;

&lt;p&gt;I will show you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handlers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useLongPress&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* in this way we can pass all the functions to the respectives events in this button */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;handlers&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
 &lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Here is the example that i made:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t5e741hL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oj2m48yns1uetb726qyd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t5e741hL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oj2m48yns1uetb726qyd.gif" alt="gif with my example using the hook" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Usefull links:
&lt;/h2&gt;

&lt;p&gt;Video where i find this hook &lt;a href="https://www.youtube.com/watch?v=mfaUfH6mo3U&amp;amp;t=976s&amp;amp;ab_channel=ColbyFayock"&gt;here&lt;/a&gt;: &lt;br&gt;
Repository of this hook on &lt;a href="https://github.com/hxsggsz/useLongPress-article"&gt;Github&lt;/a&gt;: &lt;/p&gt;

</description>
      <category>react</category>
      <category>hook</category>
      <category>longpress</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
