<?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: Lunotte</title>
    <description>The latest articles on DEV Community by Lunotte (@lunotte).</description>
    <link>https://dev.to/lunotte</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%2F850083%2F6d7ece0f-d47b-4456-8af8-454c336594fa.png</url>
      <title>DEV Community: Lunotte</title>
      <link>https://dev.to/lunotte</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lunotte"/>
    <language>en</language>
    <item>
      <title>Easily handle http requests for React and React Native with React Http Request Handler (RH2) a React library</title>
      <dc:creator>Lunotte</dc:creator>
      <pubDate>Thu, 18 Aug 2022 00:12:00 +0000</pubDate>
      <link>https://dev.to/lunotte/presentation-of-react-http-request-handler-rh2-a-react-library-4o8b</link>
      <guid>https://dev.to/lunotte/presentation-of-react-http-request-handler-rh2-a-react-library-4o8b</guid>
      <description>&lt;p&gt;React Http Request Handler (RH2) is a library that aims to simplify HTTP calls in a React application. One of its assets, you can use it for a web application (ReactJs) or for a mobile application (React Native).&lt;br&gt;
Another advantage of RH2, it gets rid of the effects that are necessary when you want to make HTTP requests. How is it possible to do without? Rh2 implements it internally, it removes this task from you.&lt;br&gt;
The calls to the services that you will need to make will be made using the Axios library, it is used to make the requests, its varied settings encouraged us to adopt it.To use RH2, we have created hooks, you just have to pass them the Axios configuration as a parameter, as well as additional parameters that are optional. These, to help you make the most of the features of RH2.&lt;/p&gt;

&lt;p&gt;The rest of this article is based on the documentation: There are references to classes, you can find the details in the link below.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.npmjs.com/package/react-http-request-handler" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--r7XxGAJ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.npmjs.com/338e4905a2684ca96e08c7780fc68412.png" height="462" class="m-0" width="880"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.npmjs.com/package/react-http-request-handler" rel="noopener noreferrer" class="c-link"&gt;
          react-http-request-handler - npm
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          This React library using customized hooks is aimed to help users handling HTTP requests. The request and its trigger are simply configured, then executed by Axios. Optional parameters can also be configured depending on the web client needs. For example :. Latest version: 1.1.1, last published: 21 hours ago. Start using react-http-request-handler in your project by running `npm i react-http-request-handler`. There are no other projects in the npm registry using react-http-request-handler.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1do8fu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.npmjs.com/b0f1a8318363185cc2ea6a40ac23eeb2.png" width="32" height="32"&gt;
        npmjs.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;To start we need to install the library with the following command : &lt;br&gt;
&lt;code&gt;npm install react-http-request-handler&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The version of the library while writing this article is version 1.1.1&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the project sources, there is a folder named example in which you can see configuration examples (be careful, it’s a bit of a mess…).&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Rh2InitializationParameter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Rh2Initializer&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-http-request-handler&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&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;./App&lt;/span&gt;&lt;span class="dl"&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;initSettings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Rh2InitializationParameter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;debugMode&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Rh2Initializer&lt;/span&gt; &lt;span class="na"&gt;rh2Settings&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;initSettings&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Rh2Initializer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example there are two important elements. The initSettings property is worth an object of type Rh2InitializationParameter with the activation of debug mode as a parameter. This mode will display logs to help understand if something is not going as expected.&lt;br&gt;
Other properties are available for initialization. For example, you can give a method to execute in case of an error in your HTTP request (500, 400, etc…), an example is present in the documentation.&lt;/p&gt;

&lt;p&gt;Then, initSettings is passed as input to the Rh2Initializer component. If you do not want to pass any configuration, this is quite possible. You will simply have to wrap your application with the component without passing any parameters.&lt;/p&gt;

&lt;p&gt;To understand the interest of this phase, it is good to know what it is for. Here we have an example with the configuration which is practically the smallest.&lt;br&gt;
If you go a little further, you will notice that you can configure several settings so that the library generates as many &lt;a href="https://github.com/axios/axios"&gt;Axios&lt;/a&gt; instances as you need. These instances will be identified with a key, this to allow you to execute an HTTP request with the instance of your choice, if none is defined to execute the request, don’t panic. We have made sure that the configuration is as simple as possible, so, if the key is not filled in, we take the first available instance (the order of the instances is equivalent to the order of the information given during the configuration).&lt;br&gt;
In our example, we haven’t configured anything about this, so RH2 will initialize a default instance.&lt;/p&gt;

&lt;p&gt;To finish on this subject, it is interesting to know the following information. Although &lt;a href="https://www.npmjs.com/package/react-http-request-handler"&gt;react-http-request-handler&lt;/a&gt; is there to help you manage the execution of HTTP requests yourself, you can recover the Axios instances that have been generated. This can be useful if we haven’t handled a case that you need or if you need to create a custom interceptor.&lt;br&gt;
The service to recover the instances is &lt;strong&gt;rh2ConfigService&lt;/strong&gt;, it is possible to recover all the instances or just one with its key.&lt;/p&gt;

&lt;p&gt;To continue, I will give you an example of use. To keep it very simple, I will not complete the configuration presented above. This means that RH2 will use an instance generated by default.&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AxiosRequestConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Rh2AxiosConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rh2AxiosConfigService&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-http-request-handler&lt;/span&gt;&lt;span class="dl"&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;axiosConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AxiosRequestConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;url&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://jsonplaceholder.typicode.com/todos&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&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;configACharger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Rh2AxiosConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TEST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;axiosRequestConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;axiosConfig&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;rh2AxiosConfigService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addConfigAxios&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;configACharger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example shows how to add a new configuration to the library. Not all configs need to be stored. In our current case, it is to introduce you to the use of the &lt;a href="https://dev.toandler#preloaded-hook"&gt;useRh2WithName&lt;/a&gt; hook, it allows you to execute a request via its label. In case you don’t want to save the configuration, there is the &lt;a href="https://www.npmjs.com/package/react-http-request-handler#hooks-without-preloading"&gt;useRh2WithParameters&lt;/a&gt; hook.&lt;/p&gt;

&lt;p&gt;This example consists of three parts, first of all, we configure the information relating to the request that we are going to want to execute, the url and the type of request.&lt;br&gt;
Then, this configuration is combined with that of &lt;strong&gt;RH2&lt;/strong&gt;, we have entered a label named TEST, this to identify this configuration, you can add as many as you need.&lt;br&gt;
Our type &lt;strong&gt;Rh2AxiosConfig&lt;/strong&gt; being of a class named &lt;strong&gt;Rh2EffectAxiosConfigHandler&lt;/strong&gt;, among the possible parameters, there is the handling of the request if it ends in error or with success. If you have defined an error configuration when initializing the library via &lt;strong&gt;Rh2InitializationParameter&lt;/strong&gt;, and we had redefined a configuration for this request, this one would be used first. This was an example, other very useful properties are to be discovered via the documentation.&lt;br&gt;
In the last line we can read the service used to add the configuration.&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRh2WithName&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-http-request-handler&lt;/span&gt;&lt;span class="dl"&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;testWithName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRh2WithName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TEST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;testWithName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a React component, you still have to call our &lt;a href="https://www.npmjs.com/package/react-http-request-handler#preloaded-hook"&gt;useRh2WithName&lt;/a&gt; hook with its label to execute the request to jsonplaceholder. Below we will describe what will be displayed in the log.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--czQ0HfaT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sl6d6zvv24w93dy6oy5h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--czQ0HfaT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sl6d6zvv24w93dy6oy5h.png" alt="The different statuses of a request used with RH2" width="588" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above we have three different logs, these are three different states of our previously configured query.&lt;br&gt;
The first, our component loads for the first time, all statuses are initialized to false because no processing has started.&lt;br&gt;
The second log shows us that the component has read the &lt;a href="https://www.npmjs.com/package/react-http-request-handler#preloaded-hook"&gt;useRh2WithName&lt;/a&gt; hook call, the request is loading.&lt;br&gt;
The last one allows to see that the request is completed successfully, in the data property is contained the result of the invoked request. When you configure the queries, you can order to return only the result of this one (default value) or all the information with the &lt;strong&gt;onlyResult&lt;/strong&gt; property. An example is below.&lt;/p&gt;

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

&lt;p&gt;This article aims to present the &lt;a href="https://www.npmjs.com/package/react-http-request-handler"&gt;React Http Request Handler (RH2)&lt;/a&gt; library, the example presented was as simple as possible in terms of configuration. It is possible to enter multiple settings. If you have any problems, you want to have other features or you want to share something else, you are welcome in the community. The discussion forum is here -&amp;gt; &lt;a href="https://github.com/Lunotte/react-http-request-handler/discussions/29"&gt;Discussion&lt;/a&gt;&lt;/p&gt;

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