<?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: Jhon-Idrovo</title>
    <description>The latest articles on DEV Community by Jhon-Idrovo (@jhon_idrovo).</description>
    <link>https://dev.to/jhon_idrovo</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%2F627256%2F9d4faa26-70c0-4b64-bb7d-f6799baf1f9d.png</url>
      <title>DEV Community: Jhon-Idrovo</title>
      <link>https://dev.to/jhon_idrovo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhon_idrovo"/>
    <language>en</language>
    <item>
      <title>How to solve a programming problem</title>
      <dc:creator>Jhon-Idrovo</dc:creator>
      <pubDate>Mon, 04 Oct 2021 14:36:56 +0000</pubDate>
      <link>https://dev.to/jhon_idrovo/how-to-solve-a-programming-problem-p44</link>
      <guid>https://dev.to/jhon_idrovo/how-to-solve-a-programming-problem-p44</guid>
      <description>&lt;p&gt;Since programming is at its core problem-solving, we need to have a good ability to do exactly that. Here I'm going to walk step by step through an approach that can help you to become better at that valuable skill.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Define the real problem
&lt;/h2&gt;

&lt;p&gt;There's no worst thing than having spent hours solving the wrong problem. That's why first you need to define the problem. That sounds trivial, but it's fairly easy to follow a rabbit hole along the way and end up trying to solve a specificity that has almost nothing to do with the initial problem. &lt;/p&gt;

&lt;p&gt;To explain this part better think about this problem that I had to solve: We need to generate random colors that can be assigned to categories so that it helps users identify them easily.&lt;/p&gt;

&lt;p&gt;In the example case, I ended up working with binary operations to generate the colors and an external package to test if the colors were different enough. Yes, it was working (kinda) but with an overly complex system.&lt;br&gt;
There is an easier way! Now let's find the root problem and focus on it.&lt;/p&gt;

&lt;p&gt;One of the best approaches I know for doing that is to ask yourself  why the problem needs to be solved multiple times:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Why do I need an algorithm to generate random colors?"&lt;/li&gt;
&lt;li&gt;Because you need random, different enough colors.&lt;/li&gt;
&lt;li&gt;"Why do I need to generate those colors?"&lt;/li&gt;
&lt;li&gt;To use them as a distinction on each category.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice how after the questions there's no binary algorithm implicated, we only need a bunch of different colors. So, why not generate them by ourselves? We can create a list of predefined colors from which we can choose one randomly, and if they're all different enough we don't even need to test for that. We don’t need the package either!&lt;/p&gt;

&lt;h2&gt;
  
  
  Gather information
&lt;/h2&gt;

&lt;p&gt;You don't want to make a decision and later change your opinion because there was something you didn't know. This isn't always possible, but the better you research, the less likely you're to have this problem.&lt;br&gt;
You can use Google, documentation, blogs, asking other people (especially if you're a beginner), and any other source of information. Find what other people have done, and how it turned out for them.&lt;br&gt;
At the end of this step, you need to have a list of options with their pros and cons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare
&lt;/h2&gt;

&lt;p&gt;Once you have a good understanding of the topic compare the options and pick the one you think it's the best. It doesn’t matter if you aren't 100% sure. That's why the next step exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prepare for failing
&lt;/h2&gt;

&lt;p&gt;You can't expect all to go exactly as you imagined. Especially in the complex world of software. So, prepare for things not working. When that happens, update the pros and cons of the options. Maybe the one you've chosen has some problems you didn't expect.&lt;/p&gt;

&lt;p&gt;But don’t throw the towel immediately! Try to make things work with the option you've taken, learn more about it. Sometimes it's just a lack of knowledge. &lt;br&gt;
But, if after all you still have problems implementing it and the cons of the current selection are becoming more than any of the other options plus the cost of changing decision. You can experiment with other options.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping thins up
&lt;/h3&gt;

&lt;p&gt;That's all for this time, I hope you find it interesting and can help you in some way. If so, you can follow me to don't miss this type of posts &lt;a href="https://twitter.com/JhonIdrovo_D"&gt;@JhonIdrovo_D&lt;/a&gt;. Also if you want to say hi, just send a DM.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Using custom hooks to manage state</title>
      <dc:creator>Jhon-Idrovo</dc:creator>
      <pubDate>Thu, 30 Sep 2021 01:40:44 +0000</pubDate>
      <link>https://dev.to/jhon_idrovo/using-custom-hooks-to-manage-complex-state-2pgb</link>
      <guid>https://dev.to/jhon_idrovo/using-custom-hooks-to-manage-complex-state-2pgb</guid>
      <description>&lt;p&gt;For a long time, Redux was the go-to choice for state management in React web applications. But, with the new addition of Hooks and stateful function components, we can achieve the same Redux results using these tools. Here I'm presenting you a way in which I use them to manage my state, especially when it becomes complex and abstraction is needed. &lt;/p&gt;

&lt;p&gt;To show this we're going to need an example to work on, and since I don't want to confuse you it'll be rather simple. But, make no mistake, this technique works at any level.&lt;/p&gt;

&lt;p&gt;Imagine that we have an app that lets you save typefaces (which I'm calling fonts for simplicity) that you like on the server. Later, you'll want to retrieve a list of them. Also, you might want to delete or add fonts. Basically, we need the whole CRUD operations.&lt;/p&gt;

&lt;p&gt;Let's start retrieving the list of fonts from the database. In this first version, I'm using a straight approach of putting all logic on the same component.&lt;/p&gt;

&lt;p&gt;Here is how it works. First I've defined two state variables, &lt;em&gt;likedFontsList&lt;/em&gt; is intended to store a list of all the fonts retrieved from the server, and &lt;em&gt;isLoading&lt;/em&gt; is used to show a loading state while we're fetching the data.&lt;br&gt;
Once the component is rendered, the useEffect hook is triggered and we fetch the data using Axios. If the request succeeds we change the loading state to false and store the response data (the liked fonts) on its corresponding variable (likedFontsList)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kgidf9JZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aorqp85zlotfl515pwp4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kgidf9JZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aorqp85zlotfl515pwp4.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is not a complex functionality but we already have kind of a mess here. As I stated previously, we're merging the logic for fetching the data and the logic for displaying it. Not good.&lt;br&gt;
Let's fix it using a custom hook.&lt;/p&gt;

&lt;p&gt;First, we create a hooks folder, and within it, we're going to create our useFonts hook. We're using this naming because it's React's recommended convention.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gkg4-ehp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/155cam14w11e76anxxyn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gkg4-ehp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/155cam14w11e76anxxyn.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's start coding its functionality.&lt;br&gt;
First, we're going to move the state from the LikedFonts component to the useFonts hook.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H2XlcAoD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntn7d9jh97ofim5t0l26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H2XlcAoD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntn7d9jh97ofim5t0l26.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's move the useEffect so that we can fetch the data when the custom hook is called.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XehHNgjs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvdnufx8g8jcp4vsgk60.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XehHNgjs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvdnufx8g8jcp4vsgk60.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, all we have to do is to return the information other components are going to need.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bk8Ivga1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ultvp08zswylia5x6wwk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bk8Ivga1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ultvp08zswylia5x6wwk.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our custom hook is complete! Note how we've abstracted all the logic for fetching from the LikedFonts component. Now it looks more concise since we're dealing with only one thing here, fetching the fonts. This is great for many things including:&lt;/p&gt;

&lt;p&gt;Reusability: Where or what is going to use this data? The hook doesn’t care! Just call it and there you have it! And from the other way, the caller doesn't care how the hook obtains that data.&lt;/p&gt;

&lt;p&gt;We have a "contract": If we want to change something later, like the fetching method or even the schema of the data we receive, we can do it easily since we have it in only one place and more importantly, we have a "contract".  That is when we declared our return statement the first time we were agreeing to return an isLoading variable with a boolean value, and a likedFontsList variable with the liked fonts in a particular schema.&lt;br&gt;
Later if we want to change the schema we can do it easily using a translating function from the new schema to the old one.&lt;/p&gt;

&lt;p&gt;Enough talking! Let's implement our custom hook before it gets cold and losses its crunchiness.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8tjx00dt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0xrwqpyl3wuofc586g3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8tjx00dt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0xrwqpyl3wuofc586g3.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Look at that! We went from 24 lines of code to 12 a 50% less! Now you can see easily and understand what the LikedFonts component is doing, showing the liked fonts.&lt;/p&gt;

&lt;p&gt;And there is even more to it. Remember that we needed all the CRUD operations? Well, we can implement all of them in the custom hook to make it even more powerful. Let's start adding a saveFont method to create a new record.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PPTDm6ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qto3xnx4aggwjk8l5k6l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PPTDm6ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qto3xnx4aggwjk8l5k6l.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, we can handle the errors right here. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D8HGyLao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nyphglayc0hc46ntle75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D8HGyLao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nyphglayc0hc46ntle75.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And just like that we can handle all the operations relate to the fonts in this hook. Furthermore, we can even use other custom hooks inside it (i.g. a useUser hook to send calls to the API only if the user is logged in)&lt;/p&gt;

&lt;p&gt;That's all for the moment. If you found this helpful or interesting please like it or share it with your friends (I know you have). This is my first time sharing this content but I have far more things to tell you so stay tuned! I usually have other posts related to the same theme so that if you combine them you get even more benefits. So, if you want to unlock that power you can follow me. You don't have anything to lose, if you see something you don’t like just unfollow me.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
