<?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: Marin Virdol</title>
    <description>The latest articles on DEV Community by Marin Virdol (@marinvirdol).</description>
    <link>https://dev.to/marinvirdol</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%2F187524%2F7cfb13e8-215f-43ff-a8ac-f46bfab66781.jpg</url>
      <title>DEV Community: Marin Virdol</title>
      <link>https://dev.to/marinvirdol</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marinvirdol"/>
    <language>en</language>
    <item>
      <title>Things I learned while using React Query - Part 2</title>
      <dc:creator>Marin Virdol</dc:creator>
      <pubDate>Mon, 08 Mar 2021 08:14:15 +0000</pubDate>
      <link>https://dev.to/marinvirdol/things-i-learned-while-using-react-query-part-2-4cak</link>
      <guid>https://dev.to/marinvirdol/things-i-learned-while-using-react-query-part-2-4cak</guid>
      <description>&lt;p&gt;This blog post is the second in a series:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://codemachine.dev/things-i-learned-while-using-react-query-part-1"&gt;Things I learned while using React Query - Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Things I learned while using React Query - Part 2 (this post)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Disable some of the defaults while in development
&lt;/h3&gt;

&lt;p&gt;React Query comes with some aggressive defaults that are useful in production&lt;br&gt;
but not that much while developing.&lt;/p&gt;

&lt;p&gt;For example, by default, a refetch happens in the background on window focus to&lt;br&gt;
keep the user as up to date as possible with the server. In development you&lt;br&gt;
really don't need to sync with the server so often.&lt;/p&gt;

&lt;p&gt;The same goes for the automatic retry behaviour when the query fails. Having no&lt;br&gt;
retry for queries that fail is perfectly acceptable in development and it will&lt;br&gt;
improve your development speed.&lt;/p&gt;

&lt;p&gt;I recommend that you disable these two defaults at the level of the query&lt;br&gt;
client. The advantage of doing it here is that you do it in only one place and&lt;br&gt;
you don't need to worry about the other queries in your app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;QueryClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;defaultOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;queries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;refetchOnWindowFocus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;production&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="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure &lt;code&gt;staleTime&lt;/code&gt; based on your needs
&lt;/h3&gt;

&lt;p&gt;If you know that a certain query doesn't change often, probably you should&lt;br&gt;
change the &lt;code&gt;staleTime&lt;/code&gt; from the default value of zero, to a value that best fits&lt;br&gt;
your needs for that specific query.&lt;/p&gt;
&lt;h3&gt;
  
  
  Use the &lt;code&gt;enabled&lt;/code&gt; option to create dependent queries or to disable/enable a query
&lt;/h3&gt;

&lt;p&gt;I've seen many people having a hard time running a query conditionally. Since&lt;br&gt;
hooks don't work with if statements, React Query provides the &lt;code&gt;enabled&lt;/code&gt;&lt;br&gt;
configuration option exactly for this. You can disable/enable a specific query&lt;br&gt;
by providing &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; to the &lt;code&gt;enabled&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;Another useful features that comes with the enabled option is the ability to&lt;br&gt;
create dependent queries. You fetch data in one query and the second query runs&lt;br&gt;
only after the first one successfully completes.&lt;/p&gt;
&lt;h3&gt;
  
  
  Treat query keys like a dependency array, &lt;code&gt;useEffect&lt;/code&gt; style
&lt;/h3&gt;

&lt;p&gt;React Query does the cache management based on your query keys which means that&lt;br&gt;
your query keys uniquely describe a piece of data in your application. These&lt;br&gt;
query keys can be simple string values, complex nested objects or array of&lt;br&gt;
strings and complex nested objects.&lt;/p&gt;

&lt;p&gt;Many of your fetching functions will have dynamic route parameters or query&lt;br&gt;
parameters. Think of the situations when you want to fetch a resource based on&lt;br&gt;
its id or when you are doing server side pagination or filtering.&lt;/p&gt;

&lt;p&gt;Knowing this, it's a good idea when designing your query keys, to treat them&lt;br&gt;
like a dependency array, just like you do with your &lt;code&gt;useEffect&lt;/code&gt; hooks. The rule&lt;br&gt;
of thumb is to add to the query key any variable that your fetch function&lt;br&gt;
depends on.&lt;/p&gt;

&lt;p&gt;The benefit of doing this is that React Query will automatically trigger a&lt;br&gt;
refetch whenever the query key changes and the synchronisation with the server&lt;br&gt;
just happens.&lt;/p&gt;
&lt;h3&gt;
  
  
  Create custom hooks
&lt;/h3&gt;

&lt;p&gt;A good practice is to wrap your &lt;code&gt;useQuery&lt;/code&gt; hook calls in your own custom hook.&lt;br&gt;
Functionality wise there is no added benefit, but there are a few developer&lt;br&gt;
benefits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we separate our data fetching from the UI&lt;/li&gt;
&lt;li&gt;Second, we can be sure that we are NOT using different query keys for the same
data&lt;/li&gt;
&lt;li&gt;Lastly, if we need to tweak some settings for a specific query, or add some
data transformation, we do that in only one place&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Don't be afraid to use your hook in every component you need
&lt;/h3&gt;

&lt;p&gt;If you need the same piece of data across your application in multiple&lt;br&gt;
components, don't be afraid to use your custom hook (or the &lt;code&gt;useQuery&lt;/code&gt; hook with&lt;br&gt;
the same query key) in multiple components.&lt;/p&gt;

&lt;p&gt;React Query automatically de-duplicates queries based on the query key,&lt;br&gt;
therefore you can be sure there will not be more than one request per query key.&lt;/p&gt;
&lt;h3&gt;
  
  
  Use a default query function
&lt;/h3&gt;

&lt;p&gt;To make things even more simpler, you can share the same fetch functionality for&lt;br&gt;
queries throughout your application by creating a default query function.&lt;/p&gt;

&lt;p&gt;As I told you before, many of your fetching functions will have dynamic route&lt;br&gt;
parameters or query parameters. This means that we can create a default query&lt;br&gt;
function that we can use for all of our queries.&lt;/p&gt;

&lt;p&gt;There are two steps we need to do: create the general fetch function and specify&lt;br&gt;
to React Query that we are going to use a default query function and which is&lt;br&gt;
the function we want to use.&lt;/p&gt;

&lt;p&gt;First, let's create the general function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;createQueryFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;QueryFunction&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;async&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;queryKey&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;queryKey&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;baseUrl&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;path&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And second, we need to tell React Query about this function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;QueryClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;defaultOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;queries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;createQueryFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api_base_url&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="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;Once this is done, you can use the &lt;code&gt;useQuery&lt;/code&gt; hook in the following ways:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// query with simple query key as string&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;useMovies&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// a GET request will be fired to https://api_base_url/api/movies&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/movies&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// OR&lt;/span&gt;
&lt;span class="c1"&gt;// query with complex query key as object&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;useMovies&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pageSize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;searchTerm&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// a GET request will be fired to&lt;/span&gt;
  &lt;span class="c1"&gt;// https://api_base_url/api/movies?page=0&amp;amp;pageSize=10&amp;amp;searchTerm=matrix&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/movies&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="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pageSize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;searchTerm&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>react</category>
      <category>reactquery</category>
      <category>state</category>
    </item>
    <item>
      <title>Things I learned while using React Query - Part 1</title>
      <dc:creator>Marin Virdol</dc:creator>
      <pubDate>Fri, 26 Feb 2021 09:23:13 +0000</pubDate>
      <link>https://dev.to/marinvirdol/things-i-learned-while-using-react-query-part-1-26h1</link>
      <guid>https://dev.to/marinvirdol/things-i-learned-while-using-react-query-part-1-26h1</guid>
      <description>&lt;p&gt;I've been using &lt;a href="https://react-query.tanstack.com/" rel="noopener noreferrer"&gt;React Query&lt;/a&gt; in real world applications for over 8 months now, and I want to share with you some of the things I found useful to know when using it. This is the first part of a blog series.&lt;/p&gt;

&lt;h3&gt;
  
  
  React Query is a light caching layer
&lt;/h3&gt;

&lt;p&gt;React Query is a caching layer that improves the developer experience and the user experience. The cache lives in memory, within your application, which means there is NO server or browser caching involved.&lt;/p&gt;

&lt;p&gt;One of the most common mistakes when starting with React Query is to treat it like a traditional cache. Many developers are taken by surprise when they see the background refetch of the data. They expect to have only the initial network request and then to have the data served (only) from the cache.&lt;/p&gt;

&lt;p&gt;But this is not the case.&lt;/p&gt;

&lt;p&gt;React Query uses the &lt;a href="https://tools.ietf.org/html/rfc5861#section-3" rel="noopener noreferrer"&gt;stale-while-revalidate caching strategy&lt;/a&gt; in the attempt of keeping the user as up to date as possible with the server data without affecting the user experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understand the different states of a query and the differences between them
&lt;/h3&gt;

&lt;p&gt;In the documentation or in any other resource on React Query you will see many references to the different states in which a query can be.&lt;/p&gt;

&lt;p&gt;These are: fresh, fetching, stale and inactive. Understanding why a query is in a certain state and when it will transition to a new state is crucial if you want to be proficient with React Query. They are the backbone of this library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4upms7qtsr003wk4itg1.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%2F4upms7qtsr003wk4itg1.png" alt="React Query States Diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the above diagram, the staleTime and cacheTime play a crucial role in deciding the state of the query. Make sure you understand when to use staleTime and when to use cacheTime. I like how &lt;a href="https://twitter.com/TkDodo" rel="noopener noreferrer"&gt;@TkDodo&lt;/a&gt; describes the differences between the two.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;staleTime = the duration until a query transitions from fresh to stale&lt;/p&gt;

&lt;p&gt;cacheTime = the duration until inactive queries will be removed from the cache&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Use the built-in dev tools to visualize the states of your queries
&lt;/h3&gt;

&lt;p&gt;React Query ships with built-in dev tools. They can be extremely useful when learning the library.&lt;/p&gt;

&lt;p&gt;Having a graphical representation of the different states in which a certain query is in will make it easier to understand them&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnykkmlz04cp2lggxi9h1.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%2Fnykkmlz04cp2lggxi9h1.png" alt="React Query Dev Tools"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Know the important defaults
&lt;/h3&gt;

&lt;p&gt;Be aware of the &lt;a href="https://react-query.tanstack.com/guides/important-defaults" rel="noopener noreferrer"&gt;important defaults&lt;/a&gt; and throughly understand them to make learning and debugging easier.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactquery</category>
    </item>
    <item>
      <title>Why I stopped using Postman?</title>
      <dc:creator>Marin Virdol</dc:creator>
      <pubDate>Fri, 26 Feb 2021 09:20:29 +0000</pubDate>
      <link>https://dev.to/marinvirdol/why-i-stopped-using-postman-282j</link>
      <guid>https://dev.to/marinvirdol/why-i-stopped-using-postman-282j</guid>
      <description>&lt;p&gt;I've been using Postman for about 7 years now and I must say, it was an amazing journey. When I started as a software developer there wasn't any good tool to create/test/share APIs but then Postman appeared in my life and made everything much, much better. 7 years later, Postman is the industry standard. Very well done, Postman 👏!&lt;/p&gt;

&lt;p&gt;During all these years of using Postman I've also found many disadvantages. It turns out, sharing your requests is not that simple, it has so many features I don't need and that makes it difficult to find what you do need. The application is slow to start and eats a lot of memory. Still, with all these disadvantages I was using it everyday without even thinking to search for a replacement. Until one day.&lt;/p&gt;

&lt;p&gt;I've just switched jobs and I was discussing with my new teammate, Csaba, about designing the API for a new set of features we were to  develop. I proposed to use Postman for the API design and then start the implementation based on the contract we define between the frontend and the backend. He told me that he knows a better tool for this task: &lt;a href="https://marketplace.visualstudio.com/items?itemName=humao.rest-client"&gt;REST Client extension for VS Code&lt;/a&gt;. I was skeptic 🤨about this, after all, Postman is the industry standard, use by so many developers. Even so, there was one thing that I loved from the beginning: I was able to /create/test/share the api from within the comfort of my editor 🤟. I mean, how awesome is that? And there is more, I can design my APIs by writing code, it has auto-complete, variables and no unnecessary functionality. We created a file .http file in our repository and off we went on coding our APIs. There are so many advantages to this like: it's easy to do versioning, track changes and share the file with other developers.&lt;/p&gt;

&lt;p&gt;I removed Postman from my laptop and I've been enjoying this extension ever since. Go have a look at this extension and let me know how it helped you.&lt;/p&gt;

&lt;p&gt;Thanks Csaba for introducing me to this extension.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How Can Declarative Programming  Simplify your UIs</title>
      <dc:creator>Marin Virdol</dc:creator>
      <pubDate>Tue, 02 Jun 2020 05:34:37 +0000</pubDate>
      <link>https://dev.to/marinvirdol/how-can-declarative-programming-simplify-your-uis-5614</link>
      <guid>https://dev.to/marinvirdol/how-can-declarative-programming-simplify-your-uis-5614</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Any fool can write code that a computer can understand. Good programmers write code that humans can understand. - Martin Flower&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Many UIs are lousy. I'm not referring to the fact that the User Experience is bad or that the website doesn't look great, I'm referring to the fact that they crash, lock up, mislead the user and are a nightmare for the programmers maintaining them.&lt;/p&gt;

&lt;p&gt;I strongly believe that this happens because many developers are using an imperative, event-driven programming approach which results in code riddled with huge number of convoluted conditional logic. If one could somehow reduce the number of conditional behaviours, the code can get easier to grasp, test and maintain.&lt;/p&gt;

&lt;p&gt;Techniques based on declarative example can help achieving this.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the problems with imperative, event-driven programming?
&lt;/h2&gt;

&lt;p&gt;In event-driven programming you have an UI component that generates an event, for example input onchange, which triggers an action that is directly attached to the component. The component implementation decides how to react to that action, making the action tightly coupled to the event. Basically, such a component is waiting for the occurrence of some external or internal event, such as button pressed or data arrived, and they react by performing the appropriate computation.&lt;/p&gt;

&lt;p&gt;The problem is rooted in the difficulty of describing this behaviour in ways that are clear, easy to understand, enhance and test.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is the problem with this approach?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;First, such a system neglects the context or current state in which the event takes places and because of this it generate a huge number of convoluted conditional logic, making it hard to handle all the states our application can be in.&lt;/p&gt;

&lt;p&gt;Second, the actions actually executed are determined by events which largely are in an unpredictable timing and order making the path through the code to differ each and every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is declarative programming?
&lt;/h2&gt;

&lt;p&gt;I think the easier way of describing the difference between declarative programming and imperative programming is through a real life analogy. &lt;/p&gt;

&lt;p&gt;Suppose you have to go to the headquarters of a company for an interview. Let's describe how you are going to reach their office through each of these techniques.&lt;/p&gt;

&lt;p&gt;The imperative approach of going to the interview is: Exist your house, turn right, at the forth traffic light make left and then straight ahead until you reach the metro station. Take metro number 1 and get down at the station A, exit the metro station through exit 2a. The office is right across the street.&lt;/p&gt;

&lt;p&gt;The declarative approach is: The address of the office is 23rd Huston Avenue, Boston, MA. &lt;/p&gt;

&lt;p&gt;The key difference here is that with declarative programming the steps are abstracted away from you. In order to reach your destination you must have a GPS or another method that knows how to get you to the address you provide.&lt;/p&gt;

&lt;p&gt;To sum up, the difference between the two programming paradigms is that with declarative programming you describe the desired result without explicitly listing all the steps that must be performed while in imperative programming you must describe exactly each step that will be executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can declarative programming simplify your UI?
&lt;/h2&gt;

&lt;p&gt;There are several declarative programming paradigms and libraries you can use but the most used nowadays are &lt;a href="https://reactjs.org/"&gt;react&lt;/a&gt;, reactive programming (&lt;a href="https://rxjs.dev/"&gt;rxjs&lt;/a&gt;) and finite state machines with state-charts (&lt;a href="https://xstate.js.org/"&gt;xstate&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  React
&lt;/h3&gt;

&lt;p&gt;React is probably the most known declarative library out there. In a React application you are simply stating that the view should look in a certain way given a certain state, there are few things that can go wrong with this approach. &lt;/p&gt;

&lt;p&gt;Moreover, React allows us to do declarative programming without the drawbacks that are usually associated with this paradigm: performance. In React, the DOM manipulation is abstracted away from us through the Virtual DOM which handles the performance issue of making changes to the DOM often.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reactive Programming Using A Library Like RxJs
&lt;/h3&gt;

&lt;p&gt;I am a huge fan of reactive programming which is a declarative style of programming. It makes it very easy to express static or dynamic data streams and moreover it is easy to express an inferred dependency within the associate execution model that facilitates automatic propagation of the changed data.&lt;/p&gt;

&lt;p&gt;I first learned about reactive programming and &lt;a href="https://www.notion.so/How-Can-Declarative-Programming-Simplify-your-UIs-2c0e111dbc7b4acdb90cdac29514d6da#c0fb36fc1e0a442ab1b9f816baa6a970"&gt;rxjs&lt;/a&gt; when I started with the new version of angular a few years ago. I must admit, at the beginning it was a little bit difficult to understand how everything is tight together (because I was used programming in an imperative way) but as time passed by and I got more experience and knowledgeable I started to really see the benefits. &lt;/p&gt;

&lt;p&gt;I mean, how awesome it is to create streams of data from different sources like search input, server data, routing data,  etc and then combine these streams in other streams that you finally use to create the state of the UI at any given moment? I tell you, it's amazing!!! And makes your code really, really simple to reason about.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finite State Machines Using A Library Like XState
&lt;/h3&gt;

&lt;p&gt;State machines are one of the most effective methods for developing robust UI.  Our apps have lots of states that they can be in. We usually handle states like, loading, success and maybe failure. What about other states our app can be in? Imagine the number of combinations you can have if you have five booleans in your state, around 120. Crazy, right? There is more to this, our app should not even be in many of those 120 possible states.&lt;/p&gt;

&lt;p&gt;All these state problems can be tackled by using a state machine.  A state machine encourages you to plan your states, to declare all your possible states and all possible transitions removing the impossible states and reducing the number of total states.&lt;/p&gt;

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

&lt;p&gt;Declarative programming makes it more easy to reason about a piece of code because of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is less code you have to worry about. Therefore, fewer things can go wrong.&lt;/li&gt;
&lt;li&gt;You specify what should happen not how it should happen.&lt;/li&gt;
&lt;li&gt;Easier to test programmatically.&lt;/li&gt;
&lt;li&gt;It forces you to make a little bit of planning before you jump straight into coding.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>declarativeprogramming</category>
      <category>rxjs</category>
      <category>xstate</category>
      <category>react</category>
    </item>
    <item>
      <title>3 Tips That Will Simplify Your Application State Management</title>
      <dc:creator>Marin Virdol</dc:creator>
      <pubDate>Thu, 21 May 2020 18:10:54 +0000</pubDate>
      <link>https://dev.to/marinvirdol/3-tips-that-will-simplify-your-application-state-management-21e9</link>
      <guid>https://dev.to/marinvirdol/3-tips-that-will-simplify-your-application-state-management-21e9</guid>
      <description>&lt;h2&gt;
  
  
  The Introduction
&lt;/h2&gt;

&lt;p&gt;The goal of this blog post is to introduce you to some of the most common pitfalls that make state management unnecessarily difficult and to outline 3 simple strategies that will help you avoid them. &lt;/p&gt;

&lt;p&gt;Managing the state of an application if extremely hard and I think is the main reason why applications tend to become so complex. For some reason, we try to manage the complexity by over-engineering our solution to the problem. There are so many ways of managing the state of your application (&lt;a href="https://redux.js.org/"&gt;redux&lt;/a&gt;, &lt;a href="https://facebook.github.io/flux/"&gt;flux&lt;/a&gt;,  &lt;a href="https://en.wikipedia.org/wiki/Finite-state_machine"&gt;finite state machines&lt;/a&gt;) and countless abstraction libraries on top of these. &lt;/p&gt;

&lt;p&gt;But first, let's quickly define what I call the state of an application so we are on the same page.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Definition Of State
&lt;/h2&gt;

&lt;p&gt;The state of an application is a representation of a system at a given time. The UI of a front-end application is a representation of the state. If a user changes the state by interacting with your application, the UI may look completely different afterward because it is represented by this new state. &lt;/p&gt;

&lt;p&gt;The state is able to keep the data of different components in sync (or not) because each update will re-render all relevant components. Moreover, you can use the state to communicate between different components.&lt;/p&gt;

&lt;p&gt;I would suggest that there are the following types of state:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Persistent State&lt;/strong&gt; - this one lives on the server and it is accessible, for example, via a REST endpoint&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Navigation State&lt;/strong&gt; - where is the user in the application? Is he on the sign-in page, the sign-up page or the profile page?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Local UI State&lt;/strong&gt; - what is the colour of the button? Is the panel expanded or not? Is the modal open&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client State&lt;/strong&gt; - you can think of the filters selected by the user when dealing with a large list of items, the current page in the list of items&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see, there are many aspects to be taken into consideration when thinking about the state of our application. The &lt;strong&gt;biggest problem&lt;/strong&gt; in state management arises when we try to &lt;strong&gt;coordinate all these state sources&lt;/strong&gt; (server data, web workers, UI components, navigation etc) all of which &lt;strong&gt;update the state concurrently.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How we decide to sync all this data is one of the most important decision when designing the state management of our application.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The 3 Simple Strategies That Will Simplify Your Application State Management
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Identify the type of state you are dealing with
&lt;/h3&gt;

&lt;p&gt;Make this your first step in your state management decision tree. Developers seriously underestimate the value of doing this (including myself) but it is extremely important in order to keep it simple.&lt;/p&gt;

&lt;p&gt;To help you identify it, you should think if this particular state of the application should be saved for later use or if you want the user to share the exact same state of the application with another user. If this is the case you should consider to reflect the state of the application in the URL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is important?&lt;/strong&gt; Except for the persistent state, the rest of the state is not stored on the server which means that there is no way of saving it for later use or for sharing it with other users. Also, if the user refreshes the page he will land on the same state of the application. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example.&lt;/strong&gt; Think about when you want to share a Youtube video with a friend from a specific moment. The link you are going to share will have all the information needed to play the exact same video from that exact specific moment.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Keep your state data as close as possible
&lt;/h3&gt;

&lt;p&gt;The next step in simplifying the state of your application is to keep your state data as close as possible to where it is needed. This comes with the following two benefits: &lt;strong&gt;maintainability&lt;/strong&gt; and &lt;strong&gt;performance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you have a hierarchy of components and you keep your state in the root component. Each time the state changes the entire tree is re-rendered and that is perfectly fine if all the components in the tree depend on that state. But what if that change only affects a component somewhere at the bottom of the hierarchy? You want to keep that part of the state as close as possible to the component that needs it. This way the component tree will re-render a lot fewer components than a state change at the top of the tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maintainability&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;From the maintainability point of view always start a new component by managing it’s internal state inside of that component and if a sibling or the parents need access to that state just lift that part of the state up to the parent. This goes both ways. If you have a piece of state in a parent component that only one child needs, then move that piece to the child level.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Don't keep your entire state globally
&lt;/h3&gt;

&lt;p&gt;I've notice this pattern especially in applications that use redux. There are developers that like to keep their entire state into redux. If you are using redux you should aim on keeping only global state into redux and the local state as close as possible to the components that need that data. &lt;/p&gt;

&lt;p&gt;The problem with using redux for every piece of state is &lt;strong&gt;increased complexity&lt;/strong&gt;. Redux adds a lot of complexity to your application because maintaining the state involves working with reducers, actions, dispatching calls which results in having your state management all over the place. It get really &lt;strong&gt;hard to maintain.&lt;/strong&gt; Ben Lesh puts it really well in one of his tweets:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/BenLesh/status/1241525265228017670"&gt;https://twitter.com/BenLesh/status/1241525265228017670&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;As you can see, as soon as the application is starting to grow, so does the complexity of managing the state. This is why &lt;strong&gt;the state management strategy is not something that you want to add late in your project, you should plan for it because it can be the decisive factor for the success or failure of your application.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“If you fail to plan, you are planning to fail!” - Benjamin Franklin&lt;/em&gt;&lt;/p&gt;

</description>
      <category>state</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
