<?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: Olena Drugalya</title>
    <description>The latest articles on DEV Community by Olena Drugalya (@olenadrugalya).</description>
    <link>https://dev.to/olenadrugalya</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%2F443240%2F50a350f7-8199-4912-9add-bb54336cf107.jpg</url>
      <title>DEV Community: Olena Drugalya</title>
      <link>https://dev.to/olenadrugalya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olenadrugalya"/>
    <language>en</language>
    <item>
      <title>Intro to React Portals</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Thu, 10 Jul 2025 11:13:02 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/intro-to-react-portals-4lp</link>
      <guid>https://dev.to/olenadrugalya/intro-to-react-portals-4lp</guid>
      <description>&lt;p&gt;This small blog post is about React Portals - when and where to use them and IF you need to use them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;As a frontend developer I am working with React for almost 5 years and I never had a need to use Portals. But once I was watching a learning video and this topic came up. I decided to learn a bit more about Portals and share the knowledge about this topic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Portal?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Portal&lt;/strong&gt; is a mechanism or pattern, which provides a way to render content outside the current DOM hierarchy.&lt;br&gt;
  Usually React renders child components inside the parent components DOM, but sometimes you need to render those children &lt;em&gt;outside&lt;/em&gt; the parent's DOM - for example on the higher level of app DOM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;modal&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt; &amp;lt;-- for example her&lt;/span&gt;&lt;span class="err"&gt;e
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;content&lt;/span&gt;&lt;span class="dl"&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="o"&gt;~~&lt;/span&gt;&lt;span class="nx"&gt;child&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;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt; &amp;lt;-- instead of her&lt;/span&gt;&lt;span class="err"&gt;e
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are specific use cases where to use Portals and we will look at them below in this post. &lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;p&gt;Portal is created by a function call from "react-dom" library  &lt;code&gt;createPortal()&lt;/code&gt;. &lt;br&gt;
It accepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;child&lt;/code&gt;: the React element you want to render.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;container&lt;/code&gt;: the actual DOM node (outside your app’s root) where it should be inserted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It returns a &lt;strong&gt;React element&lt;/strong&gt; that React knows how to mount into a different DOM node. It kinda tells React: “Render this child over here”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createPortal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here is an example of how it works under the hood:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;modal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;modal&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;portalElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createPortal&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;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;Modal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;,&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;  &lt;span class="nx"&gt;modal&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This means: you’re telling React to render &lt;code&gt;&amp;lt;div&amp;gt;My Modal&amp;lt;/div&amp;gt;&lt;/code&gt;.&lt;br&gt;
But instead of placing it where the component is written, React inserts it into &lt;code&gt;modal&lt;/code&gt;. It’s part of the &lt;strong&gt;virtual DOM tree&lt;/strong&gt;, but lives &lt;strong&gt;outside&lt;/strong&gt; the regular DOM tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use Portals
&lt;/h2&gt;

&lt;p&gt;There are some child components for which its &lt;strong&gt;recommended&lt;/strong&gt; to use Portals &lt;strong&gt;IF&lt;/strong&gt; you write them from scratch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modals / Dialogs&lt;/li&gt;
&lt;li&gt;Tooltips&lt;/li&gt;
&lt;li&gt;Popovers&lt;/li&gt;
&lt;li&gt;Dropdowns&lt;/li&gt;
&lt;li&gt;Overlays&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these components there are some &lt;strong&gt;benefits&lt;/strong&gt; when using Portals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it helps to escape parent styles ( like &lt;code&gt;overflow: hidden&lt;/code&gt; style)&lt;/li&gt;
&lt;li&gt;it helps to manage their focus correctly - useful for accessibility&lt;/li&gt;
&lt;li&gt;it still keeps React context - works with state, hooks etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use&lt;/strong&gt; Portals WHEN:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you are building your own custom modal or dialog&lt;/li&gt;
&lt;li&gt;you are working on design systems or low-level UI frameworks&lt;/li&gt;
&lt;li&gt;you need manual DOM isolation for layout or z-index issue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of existing UI libraries (like MaterailUI) are using Portals already under the hood - so we benefit from them without writing one ourselves :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Using Zustand in React applications</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 16 May 2022 12:52:51 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/using-zustand-in-react-applications-opg</link>
      <guid>https://dev.to/olenadrugalya/using-zustand-in-react-applications-opg</guid>
      <description>&lt;p&gt;In this post we are going to look closely to one of the state management libraries - &lt;strong&gt;Zustand&lt;/strong&gt;. &lt;br&gt;
This is a good alternative to &lt;a href="https://redux.js.org/"&gt;Redux&lt;/a&gt; for development of React applications if you find Redux too complicated or too heavy for your current project.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Zustand?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Zustand&lt;/strong&gt; (a german word that means &lt;em&gt;'state'&lt;/em&gt; in English) - a state-management library. In official documentation they call it "a small, fast and scalable bearbones state-management solution". &lt;br&gt;
It helps to solve such problems as &lt;a href="https://react-redux.js.org/api/hooks#stale-props-and-zombie-children"&gt;"zombie child" problem&lt;/a&gt;, React concurrency and context loss between mixed renderers. &lt;br&gt;
If you want to read about those issues more detailed, here are the links: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React concurency - &lt;a href="https://github.com/bvaughn/rfcs/blob/useMutableSource/text/0000-use-mutable-source.md"&gt;https://github.com/bvaughn/rfcs/blob/useMutableSource/text/0000-use-mutable-source.md&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;context loss - &lt;a href="https://github.com/facebook/react/issues/13332"&gt;https://github.com/facebook/react/issues/13332&lt;/a&gt;
AND the most beauty of it is that the store it provides is a &lt;strong&gt;hook&lt;/strong&gt; and you can put objects or functions in it. How awesome is that!&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  How to use Zustand?
&lt;/h2&gt;

&lt;p&gt;Below you will find the step-by step guide about how to use Zustand in your React project. &lt;br&gt;
Before you follow the steps, make sure that you can created a react project either with CRA or Vite and it is up and running.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1 - Import
&lt;/h2&gt;

&lt;p&gt;This library can be imported to your project very easy with npm or yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install zustand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yard add zustand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2 - Create Store
&lt;/h2&gt;

&lt;p&gt;Zustand gives us possibility to use the state in any component in our application. In order to do that, we need to create a store for the state.&lt;br&gt;
We will be using a simple To-Do app as an example here. &lt;br&gt;
In the project files we create a folder which we call 'store' and in this folder we create a file with the name &lt;code&gt;'useToDoStore.ts'&lt;/code&gt;. &lt;br&gt;
Remember, that Zustand store is a hook so it has to be named with 'use' at the beginning (&lt;a href="https://reactjs.org/docs/hooks-custom.html"&gt;rule of custom hooks&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;'useToDoStore.ts'&lt;/code&gt; file we create first an interface for our to-do task:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ToDo&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;title&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that we create an interface for our ToDoStore, which will include an array of ToDos and methods to create, update and delete ToDos:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ToDoStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;toDos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="nl"&gt;createToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;updateToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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;title&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;deleteToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&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 we can continue and create actually a store. To do this, we need to import a specific method &lt;code&gt;create&lt;/code&gt; from the Zustand library and than use this method to create a store:&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="nx"&gt;create&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;zustand&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;const&lt;/span&gt; &lt;span class="nx"&gt;useToDoStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ToDoStore&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;set&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;get&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="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="c1"&gt;// our array of todos is empty but&lt;/span&gt;
             &lt;span class="c1"&gt;// it could be predefined as well&lt;/span&gt;
  &lt;span class="na"&gt;createToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&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="na"&gt;updateToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&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="na"&gt;deleteToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Method &lt;code&gt;create&lt;/code&gt; give us access to 2 important functions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;set() - this function update the current state&lt;/li&gt;
&lt;li&gt;get() - this function gives access to the current state to outside
Those functions work the same way as setters and getters in the class. We will use them when defining our store functions for ToDos.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 3 - Create Functions
&lt;/h2&gt;

&lt;p&gt;Here we will use &lt;code&gt;set()&lt;/code&gt; and &lt;code&gt;get()&lt;/code&gt; functions from &lt;code&gt;create&lt;/code&gt; to define what our store functions will do. &lt;/p&gt;

&lt;p&gt;Let's start from the function which creates todos:&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="nx"&gt;createToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// first we get all todos&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;generateId&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="c1"&gt;// helper function to create ID&lt;/span&gt;
      &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newTodo&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;Than we create a function which updates todos:&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="nx"&gt;updateToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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;title&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="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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&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 the function which deletes todo:&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="nx"&gt;deleteToDo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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="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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&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;Congratulations, we have a store now :) Lets use it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4 - Use Store
&lt;/h2&gt;

&lt;p&gt;To use a store, go to the file with a component (for example ToDoList.tsx), import the ToDoStore and destructure the state and/or necessary methods from the hook. You can use all methods or just specific method in any component:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createToDo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updateToDo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteToDo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useToDoStore&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="o"&gt;=&amp;gt;&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;todos&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;createToDo&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;updateToDo&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;deleteToDo&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 your state can be used in the file of specific component where you need it to be used. Use the hook anywhere without any providers. &lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;this library is small and easy to use&lt;/li&gt;
&lt;li&gt;renders components only on change&lt;/li&gt;
&lt;li&gt;no context providers needed&lt;/li&gt;
&lt;li&gt;can be used without React&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to learn more about Zustand and what is proposes, visit the official page at &lt;a href="https://github.com/pmndrs/zustand"&gt;https://github.com/pmndrs/zustand&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to plan your first project with microservices and Spring Boot</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 26 Jul 2021 08:42:39 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/how-to-plan-your-first-project-with-microservices-and-spring-boot-4bpl</link>
      <guid>https://dev.to/olenadrugalya/how-to-plan-your-first-project-with-microservices-and-spring-boot-4bpl</guid>
      <description>&lt;p&gt;This blog post is about a possible way of how to prepare and plan your first microservices project. It describes the basic steps which are usually need to be done before you actually can start coding. &lt;/p&gt;

&lt;p&gt;To proceed with the blog post, you need to be familiar with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer"&gt;REST API&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://spring.io/projects/spring-boot"&gt;Spring Boot&lt;/a&gt; framework and know how to create a simple project. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://microservices.io/"&gt;microservices&lt;/a&gt; architecture &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  WHY PLANNING?
&lt;/h2&gt;

&lt;p&gt;When I was a freelance developer, I did not care about too much planning of an application if I had an idea. I would just do a minimal to-do list so that I could just keep track of my activity during the day, and go straight to coding. For me that was enough, since I was the one doing all the work. &lt;/p&gt;

&lt;p&gt;When you work in the company, that is totally another story. Depending on the company of course, but in most cases you are not the one who is doing all the job anymore. Usually you are added to a team of developers who are going to cooperate on a project together. &lt;/p&gt;

&lt;p&gt;And here planning is crucial for the following &lt;strong&gt;reasons&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Everyone sees the project differently&lt;/li&gt;
&lt;li&gt;Everyone is going to work and contribute to the same project&lt;/li&gt;
&lt;li&gt;Everyone uses different tools for implementation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If planning is done properly and in a right way, everyone can be on the same page, understand every project detail in a same way and use the same tools for development.&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 1 - Define Microservices
&lt;/h2&gt;

&lt;p&gt;As you already know, &lt;strong&gt;microservices&lt;/strong&gt; is an architectural style that structures an application as a collection of services.&lt;/p&gt;

&lt;p&gt;With simple words, its many small applications which are part of a large application. They are small, independent, easy to maintain and test, and they can be developed separately by a small team. &lt;/p&gt;

&lt;p&gt;It is a good practice to define a microservice according to &lt;em&gt;single responsibility&lt;/em&gt; principle. It means that one microservice should be responsible just for one task. &lt;/p&gt;

&lt;p&gt;Sometimes its difficult to define those logical or business tasks, in some cases they're obvious (for example, with online web shops there definitely would be a microservice for the user or product). Anyway, the important step 1 is to try to define them all at the beginning.&lt;/p&gt;

&lt;p&gt;Each microservice should be a separate SpringBoot project, its a good practice to do so if we want to use different technologies for each of them. &lt;br&gt;
For example, one of them could be written in Java and use PostgreSQL database, another one could be written in Kotlin and use MongoDB. &lt;/p&gt;
&lt;h2&gt;
  
  
  STEP 2 - Define Endpoints
&lt;/h2&gt;

&lt;p&gt;After we have decided, what microservices will be in our application, we can start defining endpoints for API for each microservice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;API Endpoint&lt;/strong&gt; in simple words is an URL of a service. It is like a location, from which APIs can get the resources they need.&lt;/p&gt;

&lt;p&gt;One API can provide multiple endpoints. Every endpoint operates through &lt;strong&gt;request&lt;/strong&gt; and &lt;strong&gt;response&lt;/strong&gt; - &lt;em&gt;Client&lt;/em&gt; sends the request and &lt;em&gt;Endpoint&lt;/em&gt; sends back the response. &lt;/p&gt;

&lt;p&gt;Usually the SpringBoot application runs on &lt;code&gt;localhost:8080&lt;/code&gt;, so if our application is for example a clothes shop, one of API Endpoints could be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8080/clothes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When such URL will be obtained by the browser, the browser will make GET request and receive the list of clothes in return. &lt;/p&gt;

&lt;p&gt;So, in this second step we make sure we define all the endpoints for our application (for example for the most important HTTP requests like GET, POST, PUT or DELETE etc).&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 3 - Define classes
&lt;/h2&gt;

&lt;p&gt;No, we still cannot write any code importunately :) I know how eager you are to start coding at this stage, but be patient, we are almost done with the preparations.&lt;/p&gt;

&lt;p&gt;Here at this point we define all the classes we would use in the application. We are pretty much now have the full overview and clear understanding of what exactly would be as building blocks of the app. &lt;/p&gt;

&lt;p&gt;We can also write down what attributes and methods each class will have and what type each attribute will be.&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 4 - Write Userstories
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;User story&lt;/strong&gt; is a part of &lt;a href="https://www.atlassian.com/agile/project-management"&gt;Agile project management&lt;/a&gt;. It is an informal description of a software feature from the perspective of the end user. &lt;/p&gt;

&lt;p&gt;A good and simple user story should be a sentence, written according to this template:&lt;br&gt;
&lt;code&gt;“As a [persona], I [want to], [so that].”&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example: "As a customer, I want to be able to update my info, so that I can change first name"&lt;/p&gt;

&lt;p&gt;When we break our tasks into concrete user stories and add proper description to them, it means that any developer who will implement the feature, will know exactly what to do. &lt;/p&gt;

&lt;p&gt;To learn more about &lt;strong&gt;user stories&lt;/strong&gt;, here is a good article -  &lt;a href="https://www.atlassian.com/agile/project-management/user-stories"&gt;User Stories with Examples and Template&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 5 - Prepare wireframes
&lt;/h2&gt;

&lt;p&gt;After we are done with all the above preparations for the app logic, we can think about its visual look. This step is important, since it let us visualize how exactly our application will look like on the page.&lt;/p&gt;

&lt;p&gt;There are hundreds of tools and websites available for doing sketching/ wireframing online or locally (for example such as Sketch or Figma). There is also a good tool which allows to collaborate with other developers live and draw the sketch together - &lt;a href="https://excalidraw.com"&gt;https://excalidraw.com&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Wireframing helps to understand and clarify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how many pages will you application have&lt;/li&gt;
&lt;li&gt;what would be the general layout as well as layouts for desktop and mobile view&lt;/li&gt;
&lt;li&gt;how header and footer will look like and on which pages they will repeat&lt;/li&gt;
&lt;li&gt;how the pop-up windows will look like&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the wireframing of each page is done, we should have solid understanding and view about the application we are going to build. Now we can start actual coding! :)&lt;/p&gt;

</description>
      <category>java</category>
      <category>kotlin</category>
      <category>spring</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Java for JavaScript Developer - week 2</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 31 May 2021 07:58:33 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/java-for-javascript-developer-week-2-3dkf</link>
      <guid>https://dev.to/olenadrugalya/java-for-javascript-developer-week-2-3dkf</guid>
      <description>&lt;p&gt;This blog post continues a series "Java for JavaScript Developers", in which I write about my Java learning path after being web developer. Here I will write about basics of Java and what challenges did I have during the study.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge 1 - Data Types.
&lt;/h2&gt;

&lt;p&gt;Java is a static language, it means that data types are checked before the run-time, so we define a variable, we have to define its type as well. In JavaScript we don't need to define a type of variable, it is being checked during the execution of the application. So, for me it was something i had to used to, good thing that I'm using IntelliJ IDE and it has a very clever compiler. So whenever I was defining a variable without a type, the compiler was showing me an error right away.&lt;/p&gt;

&lt;p&gt;There are 2 types of data in Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;primitive&lt;/li&gt;
&lt;li&gt;non-primitive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Primitive&lt;/em&gt; type is a type, which size in the memory and location are defined. They are the building blocks of data manipulation and the very basic types. They can be divided into 4 categories : Boolean (boolean type), Character (character literal), Integer (integer type) and Floating-Point.&lt;/p&gt;

&lt;p&gt;There are 8 types of primitive data types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;boolean&lt;/li&gt;
&lt;li&gt;byte&lt;/li&gt;
&lt;li&gt;char&lt;/li&gt;
&lt;li&gt;short&lt;/li&gt;
&lt;li&gt;int&lt;/li&gt;
&lt;li&gt;long&lt;/li&gt;
&lt;li&gt;float&lt;/li&gt;
&lt;li&gt;double&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Non-primitive&lt;/em&gt; type is a type, which size in the memory and location are not defined. They include String, Classes, Interfaces and Arrays. &lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge 2 - Semicolon.
&lt;/h2&gt;

&lt;p&gt;Yes, this is painful topic if you came from the world of JavaScript :) Now with Java you should always remember to finish your line of code with a semicolon. It shows the compiler where an instruction ends and where the next instruction begins.&lt;br&gt;
 Semicolon allows the Java program to be written in one line or multiple lines, by letting the compiler know where to end the instructions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Challenge 3 - Variables.
&lt;/h2&gt;

&lt;p&gt;In JavaScript there are 2 types of keywords to define a variable - const and let. They are almost identical with just only one difference - &lt;em&gt;const&lt;/em&gt; indicates that the value of variable cannot be re-assigned and &lt;em&gt;let&lt;/em&gt; can be re-assigned. So, of course const keyword is widely used in JS.&lt;/p&gt;

&lt;p&gt;In Java however, there are no keywords to define a variable. Instead its important to assign a data type first. Also there access modifiers you will have to take into consideration, but those are not so important with variables.&lt;/p&gt;

&lt;p&gt;There are &lt;em&gt;three&lt;/em&gt; types of variables in Java: local, instance and static.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;1) Local Variable&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists.&lt;/p&gt;

&lt;p&gt;A local variable cannot be defined with "static" keyword.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;2) Instance Variable&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static&lt;/p&gt;

&lt;p&gt;It is called instance variable because its value is instance specific and is not shared among instances.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;3) Static variable&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory. &lt;/p&gt;
&lt;h2&gt;
  
  
  Challenge 4 - Implicit and Explicit Casting.
&lt;/h2&gt;

&lt;p&gt;This is totally something you never see in JavaScript, so its Java exclusive thing. &lt;/p&gt;

&lt;p&gt;Type Casting is nothing but converting a primitive or interface or class into other type. There is a rule in Java Language that classes or interface which shares the same type hierarchy only can be typecasted. If there is no relationship between then Java will throw ClassCastException. Type casting are of two types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Implicit Casting (Widening)&lt;/li&gt;
&lt;li&gt;Explicit Casting (Narrowing)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implicit Casting/ Widening&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Automatic type conversion can happen, if both types are compatible and target type is larger than source type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// No casting needed for below conversion&lt;/span&gt;
&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explicit Casting / Narrowing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you are assigning a larger type to a smaller type, then Explicit Casting is required&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;75.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Explicit casting is needed for below conversion&lt;/span&gt;
&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;short&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Challenge 5 - The Equality Operator.
&lt;/h2&gt;

&lt;p&gt;As you know, JavaScript has 2 equality operators - &lt;br&gt;
== (equality by value)&lt;br&gt;
=== (equality by value and type)&lt;/p&gt;

&lt;p&gt;Java offers you just one nice equality operator -  == and nothing else, because there is no need to check for equality by type. Just enjoy one operator here :)&lt;/p&gt;
&lt;h2&gt;
  
  
  Challenge 6 - Methods.
&lt;/h2&gt;

&lt;p&gt;Here is all the fun begins :)&lt;br&gt;
In JavaScript is all about functions, it can be called a building block of the language. &lt;br&gt;
To define the function all you need is &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;keyword Const&lt;/li&gt;
&lt;li&gt;function name (keyword &lt;em&gt;function&lt;/em&gt; is needed if we dont use arrow function)&lt;/li&gt;
&lt;li&gt;parameters&lt;/li&gt;
&lt;li&gt;function body
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(..&lt;/span&gt;&lt;span class="nx"&gt;params&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;function&lt;/span&gt; &lt;span class="nf"&gt;body&lt;/span&gt;&lt;span class="p"&gt;...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In Java each method consist of:&lt;/p&gt;

&lt;p&gt;1.) Access specifier (it is the access type of the method. It specifies the visibility of the method ). Java provides &lt;strong&gt;four&lt;/strong&gt; types of access specifier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Public:&lt;/strong&gt; The method is accessible by all classes when we use public specifier in our application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private:&lt;/strong&gt; When we use a private access specifier, the method is accessible only in the classes in which it is defined.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protected:&lt;/strong&gt; When we use protected access specifier, the method is accessible within the same package or subclasses in a different package.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Default:&lt;/strong&gt; When we do not use any access specifier in the method declaration, Java uses default access specifier by default.
It is visible only from the same package only.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.) Return Type: Return type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If the method does not return anything, we use void keyword.&lt;/p&gt;

&lt;p&gt;3.) Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is invoked by its name.&lt;/p&gt;

&lt;p&gt;4.) Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and variable name. If the method has no parameter, left the parentheses blank.&lt;/p&gt;

&lt;p&gt;5.) Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;myFunction&lt;/span&gt;&lt;span class="o"&gt;(...&lt;/span&gt;&lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="o"&gt;...)&lt;/span&gt; &lt;span class="o"&gt;{...&lt;/span&gt;&lt;span class="na"&gt;function&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;...};&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as you might see, there are may different things a developer has to do differently if he/she switches from JavaScript to Java. Although for me personally only the basic syntax was a bit difficult to grasp. The main concepts though like OOP or collections were easy to understand and use. &lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Learning Java as JavaScript Developer - week 1</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Thu, 13 May 2021 08:25:30 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/learning-java-as-javascript-developer-week-1-2hnm</link>
      <guid>https://dev.to/olenadrugalya/learning-java-as-javascript-developer-week-1-2hnm</guid>
      <description>&lt;p&gt;This blog post is a beginning of the series "Java for JavaScript Developers" and it describes my experience while learning Java programming language. &lt;/p&gt;

&lt;p&gt;So, as many of you have already know from Twitter, due to my job specification I started to learn Java after being a web developer and using JavaScript for building projects. During a few years of using JavaScript and building projects mostly with React and other front-end frameworks, I have to dig myself into back-end. &lt;/p&gt;

&lt;p&gt;That felt a bit weird I should say. Even though I used to learn new languages all the time (I speak 5 languages and finished linguistic university), it takes time to get used to new rules and new syntax. &lt;/p&gt;

&lt;h2&gt;
  
  
  Differences
&lt;/h2&gt;

&lt;p&gt;After the first week of study I can already distinguish the main differences between 2 languages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Java is &lt;em&gt;static&lt;/em&gt; language and JS is &lt;em&gt;dynamic&lt;/em&gt; language. &lt;br&gt;
What this means is they check their data types on different stages. &lt;br&gt;
&lt;strong&gt;Java&lt;/strong&gt; checks types before run-time, each variable has to be associated with a type upon declaration.&lt;br&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt; checks types on the fly, during execution and the type of variable is unknown until program compilation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java is &lt;em&gt;one-paradigm&lt;/em&gt; and JS is &lt;em&gt;multi-paradigm&lt;/em&gt; &lt;br&gt;
What this means is that:&lt;br&gt;
&lt;strong&gt;Java&lt;/strong&gt; is strictly object-oriented, meaning it solves programming problems based on the concept of objects and everything is built around objects. &lt;br&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt; on the contrary can be used as object-oriented, procedural or scripting language (it is used mainly as scripting though).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java is &lt;em&gt;stand-alone&lt;/em&gt; language and JS is &lt;em&gt;co-dependant&lt;/em&gt; language&lt;br&gt;
What that means is that:&lt;br&gt;
&lt;strong&gt;Java&lt;/strong&gt; can be used to build a fully-functional application by developer without any additional languages. It can run on multiple platforms.&lt;br&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt; is depending on HTML and CSS and cannot be used as a stand-alone language to build a fully-functional application. Even though nowadays JS is widely used for back-end (Node.js), it still needs cooperation with additional languages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java objects are &lt;em&gt;class-based&lt;/em&gt; and JS objects are &lt;em&gt;prototype-based&lt;/em&gt;&lt;br&gt;
What that means is that:&lt;br&gt;
&lt;strong&gt;Java&lt;/strong&gt; uses classes as templates for objects creation. It allows a class to derive (inherit) the properties of another class. It also supports multiple inheritances i.e.; a class can derive properties from multiple classes.&lt;br&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt; uses prototypes - special objects, to which additional properties can be attached, which will be shared across all the instances of their constructor function. It does not support multiple inheritance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java supports &lt;em&gt;multi-threading&lt;/em&gt; and JS doesn't.&lt;br&gt;
This one says it all in one sentence :) JavaScript does not support multi-threading because the JavaScript interpreter in the browser is a single thread.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Similarities
&lt;/h2&gt;

&lt;p&gt;BUT there are some similarities between the languages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Object-Oriented Programming: Both languages have access to OOPs concepts such as abstraction, inheritance, and polymorphism.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Front-end Development: Java and JS both extend their applications in frontend development. JS can be embedded directly into HTML, which is implemented as a framework or a library; whereas, Java is used as Java Applet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Back-end Development: Both languages offer server-side support. Java supports major backend technologies such as JBoss, Apache, and WebSphere. Node.js serve JavaScript-powered servers.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, these are the main differences and similarities between Java and JavaScript and if you want to explore even more on this topic, take a look at this &lt;a href="https://hackr.io/blog/java-vs-javascript" rel="noopener noreferrer"&gt;article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my next blog post I will write about basics of Java language and compare them to basics of Java Script :)&lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>java</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>development</category>
    </item>
    <item>
      <title>Introduction to NextJS</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Tue, 04 May 2021 13:20:09 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/introduction-to-nextjs-3gi4</link>
      <guid>https://dev.to/olenadrugalya/introduction-to-nextjs-3gi4</guid>
      <description>&lt;p&gt;This blog post is an easy introduction to NextJS and it's features.&lt;br&gt;
However, before starting to read this blog post, you must be familiar with &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; library.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is NextJS?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;NextJS&lt;/strong&gt; is a React framework, which means is a platform for developing software applications by using React library. &lt;br&gt;
It has a lot of features which make development of React applications easier. &lt;/p&gt;

&lt;p&gt;As a programmer, we don’t need to start from scratch when there are already tools designed to help us with our projects. Frameworks are software that is developed and used by developers to build applications, so NextJS is one of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why to Use NextJS?
&lt;/h2&gt;

&lt;p&gt;This question appeared in my head many times before I wasn't familiar with NextJS. I was pretty sure that React library itself is enough to build an application. But as I was developing my web developer's skills and started to build highly-scaled large applications, I had to import many additional libraries together with React (for example, for routing). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NextJS&lt;/strong&gt; makes the development of large React application easier, since it provides many additional features, which we will discuss below in details. This framework solves many common problems and simply makes the life of React developer easier :) We still write React code and use React features, but also together with that we are provided by lots of built-in features to solve common problems and clear instructions how to use them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features of NextJS
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;NextJS&lt;/strong&gt; offers many benefits for the React developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;page-based routing system (with support for dynamic routes) &lt;br&gt;
With NextJS we don't need to care about writing a code for routers for the pages, we just create a page in a special folder and NextJS provides it with routing, simple as that :)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pre-rendering, both static generation (SSG) and server-side rendering (SSR) are supported&lt;br&gt;
Server-side rendering (SSR) is preparing of content of a page on a server, while one-page React application uses client-side rendering (CSR). The problem with CSR is that it's not actually SEO friendly, because search engines will not see the actual content of the page. By using SSR in NextJS we can avoid such issues as flickering page while data fetching and our website content will be SEO friendly. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;built-in CSS and Sass support, and support for any CSS-in-JS library&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;development environment with Fast Refresh support&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;full-stack capabilities&lt;br&gt;
&lt;strong&gt;NextJS&lt;/strong&gt; makes it easier for React developers to add back-end code to the project. It very easy here to add our own code for storing data, getting data, authentication etc. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With all that being said, I highly recommend you to consider using this framework and if you have already decided to learn it - congratulations, you made a good choice :)&lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Connecting React project with Firebase</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Wed, 21 Apr 2021 09:19:31 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/connecting-react-project-with-firebase-2n3g</link>
      <guid>https://dev.to/olenadrugalya/connecting-react-project-with-firebase-2n3g</guid>
      <description>&lt;p&gt;While developing a web application, developers often have to interact with the database (save and fetch data to and from a database).&lt;br&gt;
This blog post describes an easy way to connect your React project with Firebase database. &lt;/p&gt;

&lt;p&gt;To read this blog post, you need to be familiar with &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; library and &lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase&lt;/a&gt; database. &lt;/p&gt;
&lt;h2&gt;
  
  
  What is Firebase
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Firebase&lt;/strong&gt; is a backend service provided by Google. It contains a &lt;em&gt;database&lt;/em&gt; and &lt;em&gt;API&lt;/em&gt; to which we can send requests. To start working with this service, you only need a Goggle account. &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%2F6hl67vrzgojpmqkrmgsa.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%2F6hl67vrzgojpmqkrmgsa.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Why to use Firebase with React
&lt;/h2&gt;

&lt;p&gt;When we use React library in our frontend project, we would not want to connect to database directly. If we do so, we will have security issues. Why?&lt;/p&gt;

&lt;p&gt;We could simply connect our single page React app in a similar way as we connect a Node.js app (or any other server-side app):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mysql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;database-path&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;username&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;password&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SELECT * FROM users&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;As written in the code above, we could use some database driver/ package (in this example for MySQL) and then use methods exposed by that driver to connect to the database and then run queries against it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BUT&lt;/strong&gt; if we connect to database like shown above, we have to put all our database credentials and queries directly into our frontend code. This code is accessible by everyone though! &lt;/p&gt;

&lt;p&gt;Just open the dev tools in browser and see the code - that's how web works.&lt;/p&gt;

&lt;p&gt;That’s why any code that “talks” to a database belongs onto a server and the users can’t access that code. &lt;/p&gt;

&lt;p&gt;This is exactly the way how &lt;strong&gt;React&lt;/strong&gt; works with &lt;strong&gt;Firebase&lt;/strong&gt; - React needs a backend API to which it will send HTTP requests and Firebase provides that API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting React project with Firebase
&lt;/h2&gt;

&lt;p&gt;As mentioned above, Firebase provides a database API, which accepts a data in a JSON format returns a data in a special format back. React will use this API to fetch data from it or to send data to it to store into database (buy using methods POST and GET).&lt;/p&gt;

&lt;p&gt;To configure a Firebase, just simply follow the steps on &lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase official page&lt;/a&gt;. Below Ill write down them shortly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;click Get Started&lt;/li&gt;
&lt;li&gt;start a new project&lt;/li&gt;
&lt;li&gt;go to Realtime Database (on the left side) &lt;/li&gt;
&lt;li&gt;create database &lt;/li&gt;
&lt;li&gt;choose "Start in test mode"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And you got your database and API link (see screenshot below):&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%2Fizdadov677cqhe70d3xv.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%2Fizdadov677cqhe70d3xv.png" alt="firebase_api"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That was simple :) Now let's use it in our project. &lt;/p&gt;

&lt;p&gt;Let's assume we have a simple React project with a form. The user fills in the form and after button click all info goes to database. &lt;/p&gt;

&lt;p&gt;Our Form component will look like this:&lt;/p&gt;

&lt;p&gt;// Form.js&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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Form&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="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// We are using refs here to access the value of DOM nodes&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;titleRef&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;imageRef&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addressRef&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;descRef&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&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="nt"&gt;form&lt;/span&gt; &lt;span class="na"&gt;onSubmit&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSubmit&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="nt"&gt;div&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="nt"&gt;label&lt;/span&gt; &lt;span class="na"&gt;htmlFor&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Title:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;label&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="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;titleRef&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="nt"&gt;div&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="nt"&gt;div&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="nt"&gt;label&lt;/span&gt; &lt;span class="na"&gt;htmlFor&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"img"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Image:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;label&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="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"url"&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"img"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;imageRef&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="nt"&gt;div&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="nt"&gt;div&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="nt"&gt;label&lt;/span&gt; &lt;span class="na"&gt;htmlFor&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Address:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;label&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="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"address"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;addressRef&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="nt"&gt;div&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="nt"&gt;div&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="nt"&gt;label&lt;/span&gt; &lt;span class="na"&gt;htmlFor&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"desc"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Description:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;label&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="nt"&gt;textarea&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"desc"&lt;/span&gt; &lt;span class="na"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;descRef&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="nt"&gt;div&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="nt"&gt;div&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="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Add&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&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="nt"&gt;div&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="nt"&gt;form&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="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;When the user fills in the required fields in the form and click on Add button, the form will trigger &lt;code&gt;onSubmit&lt;/code&gt; event, which in its turn will call a method &lt;code&gt;handleSubmit&lt;/code&gt;. &lt;br&gt;
This method creates an object, which we will use to &lt;em&gt;send&lt;/em&gt; the user's data to the database:&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;handleSubmit&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&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="nf"&gt;preventDefault&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;enteredData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;titleRef&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;imageRef&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;addressRef&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;descRef&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="nx"&gt;value&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;Now we can send &lt;code&gt;enteredData&lt;/code&gt; object to the database by using API provided by database and JavaScript method &lt;code&gt;fetch()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Method &lt;code&gt;fetch()&lt;/code&gt;accepts as parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;path&lt;/strong&gt; (in our case - Firebase API link)
Note: When we use the Firebase API link as path, we need to add the name of data we store and &lt;code&gt;.json&lt;/code&gt; to the end (for example, if we store todos, we add &lt;code&gt;todos.json&lt;/code&gt; to the end of API link)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;init object&lt;/strong&gt; with number of different settings &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we use &lt;code&gt;fetch()&lt;/code&gt; just with &lt;strong&gt;path&lt;/strong&gt;, it will execute GET method by default. Since we want to send data, we need to add second parameter and specify that we want to use POST method.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;handleSubmit&lt;/code&gt; method now would look like this:&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;handleSubmit&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&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="nf"&gt;preventDefault&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;enteredData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;titleRef&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;imageRef&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;addressRef&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;descRef&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://test-a02ef-default-rtdb.firebaseio.com/info.json&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="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="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;body&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="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enteredData&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&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="s2"&gt;application/json&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;After the user provided all required data to the form and clicked &lt;code&gt;Add&lt;/code&gt; button, the form sends the data to database. We can refresh our Firebase project and see the data being stored there. Yay!&lt;/p&gt;

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

&lt;p&gt;With Firebase we can store our data to the database. We can also fetch the data from our database and render it to the page we need by using the same API link provided by Firebase. It might look as if we are interacting directly with a database, but now we know that this is not what’s happening!&lt;/p&gt;

&lt;p&gt;Instead, services like Firebase give you a backend which is kind of hidden from the user and we only use that backend API to interact with the database.&lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>firebase</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React Live Coding Interview Tips</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 12 Apr 2021 13:46:26 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/react-live-coding-interview-tips-1f5g</link>
      <guid>https://dev.to/olenadrugalya/react-live-coding-interview-tips-1f5g</guid>
      <description>&lt;p&gt;This blog post is not about how one should ace or nail the live coding interview for the junior frontend developer, but &lt;strong&gt;the practical advices&lt;/strong&gt; on how to prepare for it and some tasks which might have been given.&lt;/p&gt;

&lt;p&gt;All information in this post is based on my own experience and some research which I did in advance. It is not a set of rules of &lt;em&gt;how you should do it&lt;/em&gt;, but more like &lt;em&gt;what you can expect&lt;/em&gt; and &lt;em&gt;how you can prepare&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Alright, with all that being said, let's imagine we have received that invitation to the next stage of job interview and it would be &lt;strong&gt;live coding technical interview&lt;/strong&gt;. We have a mixture of excitement and fear and impostor syndrome all at once - that is totally fine, just give it a time to settle down :)&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiojyxw2dcqqcrfzbktom.jpeg" 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%2Fiojyxw2dcqqcrfzbktom.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we can think clearly again, we understand that there is a level of uncertainty and frustration connected with how we should prepare and what. There are few steps to get rid of those.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysing the Given Info
&lt;/h2&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%2F13hzvy1857rbdnp5iaov.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%2F13hzvy1857rbdnp5iaov.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's analyse what info do we actually have on your hands. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We know it's a junior position - it means it doesn't have to be that difficult technical-wise. &lt;/li&gt;
&lt;li&gt;We know you will be using React library - we are familiar with it, we used it so many times.&lt;/li&gt;
&lt;li&gt;We know it would be live coding - it means they will ask us to share our screen and give us tasks which we have to code on the go.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But we still don't know what exactly gonna happen and what we should prepare or repeat. It's a good practice to ask the interviewer about it.&lt;/p&gt;

&lt;p&gt;We can write a nice answer back, in which we say &lt;em&gt;thank you&lt;/em&gt; for the invitation and ask politely what we should prepare. In most cases they will answer back with some detailed info like "you should write Hello World in a simple CRA project, you should also know how to work with data and events and how to fetch data from API". &lt;/p&gt;

&lt;p&gt;Here we go! We have more info now on our hands and that removed uncertainty and frustration, so we can go ahead and make a plan for interview preparation. &lt;/p&gt;

&lt;h2&gt;
  
  
  Making a Plan for Interview Preparation
&lt;/h2&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%2F754kr3ldjbpkczbntnbk.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%2F754kr3ldjbpkczbntnbk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go through the email from interviewer again and write down bullet points from it. Usually it contains a lot of clues of what we should prepare.&lt;/li&gt;
&lt;li&gt;Google top 50 React interview questions and save the link - we want to repeat theory as well&lt;/li&gt;
&lt;li&gt;Write down and save a coding solution for each bullet point (for example - fetch data from API - write down a code for fetching data with Fetch or Axios; events - write down a code for creating onClick() event and so on.)&lt;/li&gt;
&lt;li&gt;Think about and find a person who could perform a mock interview for you - if you have such possibility of course. Otherwise find similar mock interview on YouTube and watch it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that we have a precise plan, we can start your preparations and we will be more confident and calm right now, since we know exactly what we should repeat or prepare.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing for the Interview
&lt;/h2&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%2Fmexyl2pqdytkzwv7ci97.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%2Fmexyl2pqdytkzwv7ci97.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here what we do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;repeat the theory of React library&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep in mind that the interviewer might ask to explain what is React, its main features, what is component, state, props and JSX. The purpose of that is mostly to see how to you can &lt;em&gt;explain these concepts to another person&lt;/em&gt;. It's a good practice to say those loud while preparing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a project in advance &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It takes time to install CRA with package manager, so to avoid waiting time it's a good practice to do it in advance. remove all unnecessary code from it, so your App function has only return() in it with one &lt;code&gt;div&lt;/code&gt;. Install all other dependancies which you think might be helpful to you or can be used (like &lt;code&gt;axios&lt;/code&gt;, &lt;code&gt;react-router-dom&lt;/code&gt;, &lt;code&gt;bootstrap&lt;/code&gt; etc.) &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;code the solutions for the bullet points&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While coding the solutions, try to talk about what are you doing. Here is the example of possible explanation:&lt;br&gt;
"In the return() function of App component we add a &lt;code&gt;button&lt;/code&gt; with text Add Count. We add &lt;code&gt;onClick()&lt;/code&gt;event to this button and this event will accept a function &lt;code&gt;handleClick&lt;/code&gt;. Now we need to create this function. We create an arrow function &lt;code&gt;handleClick&lt;/code&gt; and inside the function we increase the counter to 1. We use method &lt;code&gt;setCounter&lt;/code&gt; to change the previous value of &lt;code&gt;counter&lt;/code&gt; object to the new value". &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ask your friend/relative/fellow developer to make a mock interview 
This is a great way to prepare for the interview. It is possible to find out your weak sides and to improve them, as well as to improve your communication skills.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Interview Day
&lt;/h2&gt;

&lt;p&gt;So, during these 3-4 days before the interview we have prepared theory, practice and communication and feel ourselves much confident than before. Note, that live coding interview is done with timer (usually it's 45 minutes), so the interviewer will appreciate you being fast and to the point.  &lt;/p&gt;

&lt;p&gt;There are some good tips to use during the interview:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;always ask how the interviewer prefer you to do something (for example, if you have been asked to make a button, it's a good practice to ask if they want you to make a component or just simple JSX element). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;listen attentively to the given assignment&lt;br&gt;
If you didn't understand the task, ask to explain you once again. You can explain that by being nervous, not because you don't know how to do it. If the task is long, write down on paper the main points &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;use shortcuts &lt;br&gt;
Its a very good practice to use keyboard shortcuts in your project. it shows that you are aware of how to save time and that you use your keyboard/text editor easily. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;know how to google&lt;br&gt;
Junior position doesn't expect that you know everything (it could be different for higher levels though). So you are definitely allowed to google. Make it your advantage. Instead of trying to remember the syntax of that function, don't waste your and interviewer time - just google it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Possible Assignments
&lt;/h3&gt;

&lt;p&gt;There is a list of possible tasks which the interviewer might ask to code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;write "Hello World" on the page&lt;/li&gt;
&lt;li&gt;create a counter and increase it by clicking on button&lt;/li&gt;
&lt;li&gt;fetch data from given API and store it in variable&lt;/li&gt;
&lt;li&gt;fetch data from API on the first render of the component&lt;/li&gt;
&lt;li&gt;fetch data from given API when you click a button&lt;/li&gt;
&lt;li&gt;render fetched data to the page as a string&lt;/li&gt;
&lt;li&gt;if the data are Books for example, render only name and author of the book to the page&lt;/li&gt;
&lt;li&gt;transform the properties of a book you have just rendered into separate Book component&lt;/li&gt;
&lt;li&gt;render Book component to the page&lt;/li&gt;
&lt;li&gt;create another component which is a collection of books which contains Book component&lt;/li&gt;
&lt;li&gt;render collection of books component to the page&lt;/li&gt;
&lt;li&gt;give it some styling so it looks nice (usually here the margins/paddings/colour or grid/flex would be enough)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The live coding interview is an important part of hiring process. It gives the interviewer a quick possibility to see if we really understand the technology, which we claimed we know. Besides the coding part, it shows how we can explain and communicate our code to others, thus give them an impression how we will collaborate with other team mates. It shows also how quick we can understand the task and what we will use to perform it quick and effective. &lt;/p&gt;

&lt;p&gt;I wish you all good luck in getting that dream job!&lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Ways of Getting Data from API in React</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 05 Apr 2021 09:22:26 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/ways-of-getting-data-from-api-in-react-2kpf</link>
      <guid>https://dev.to/olenadrugalya/ways-of-getting-data-from-api-in-react-2kpf</guid>
      <description>&lt;p&gt;This blog post is about the ways of getting data from API in React. &lt;br&gt;
Before you read this post, you should be familiar with &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; library and &lt;a href="https://en.wikipedia.org/wiki/API" rel="noopener noreferrer"&gt;Application Programming Interface (API)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; library is a wonderful tool for building rich and highly scalable user interfaces. One of its powerful features is the possibility to &lt;em&gt;fetch data&lt;/em&gt; for the web application from the outside and interact with it. &lt;/p&gt;
&lt;h2&gt;
  
  
  Why to Fetch Data?
&lt;/h2&gt;

&lt;p&gt;When you are just starting developing web applications with &lt;strong&gt;React&lt;/strong&gt;, you probably won't need any external data at the beginning. You will build a simple applications like ToDo app or Counter and add your data to the state object of your application. And that is totally fine. &lt;/p&gt;

&lt;p&gt;However, at some point you want to request real world data from an own or a third-party API. For example, if you want to build a book store or weather application, it is faster and convenient to use one of those free data sources available in the Internet. &lt;/p&gt;
&lt;h2&gt;
  
  
  Where to Do Data Fetching
&lt;/h2&gt;

&lt;p&gt;Now that we have decided that we want to fetch data from external source, here comes the question - where exactly in our application we should do that? &lt;/p&gt;

&lt;p&gt;This question depends the following criteria:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;who is interested in data?&lt;/li&gt;
&lt;li&gt;who will show the loading indicator in case data is pending?&lt;/li&gt;
&lt;li&gt;where to show an optional error message when the request fails?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Usually this is a common parent component in the components tree who will do this job. It will fetch the data, store it to its local state and distribute it to the children:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. On the first mounting of the component&lt;/strong&gt;&lt;br&gt;
We use this way when we want the data to be accessible when we first start the application. It means, we need to perform data fetching when our parent component is being mounted. &lt;/p&gt;

&lt;p&gt;In class-based components the perfect place for data fetching is &lt;code&gt;componentDidMount()&lt;/code&gt; lifecycle method.&lt;/p&gt;

&lt;p&gt;In functional components it is &lt;code&gt;useEffect()&lt;/code&gt; hook with an empty dependancy array because we need the data to be fetched once. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. On event being triggered&lt;/strong&gt;&lt;br&gt;
We can fetch data on triggering an event (for example button click) by creating a function, which will make data fetching and then binding that function to the event.&lt;/p&gt;
&lt;h2&gt;
  
  
  Ways of Fetching Data
&lt;/h2&gt;

&lt;p&gt;There are many ways to extract data from API in React:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;using Fetch API&lt;/li&gt;
&lt;li&gt;using Axios library&lt;/li&gt;
&lt;li&gt;using async-await syntax&lt;/li&gt;
&lt;li&gt;using custom hooks&lt;/li&gt;
&lt;li&gt;using React Query library&lt;/li&gt;
&lt;li&gt;using GrapthQL API&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will explore these ways now in details.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Fetching Data with Fetch API
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Fetch API&lt;/strong&gt; is built into most modern browsers on the window object (window.fetch) and enables us to make HTTP requests very easily using JavaScript promises. &lt;/p&gt;

&lt;p&gt;In our CRA we can use &lt;code&gt;fetch()&lt;/code&gt; method to get the data. This method accepts just an URL to the data.&lt;/p&gt;

&lt;p&gt;To do so, we will create a method called &lt;code&gt;fetchData()&lt;/code&gt;. It will call &lt;code&gt;fetch()&lt;/code&gt; method with provided URL, then convert the result to JSON object and print it to the console:&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;fetchData&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="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://randomuser.me/api/&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use this method now anywhere in the application. Here is an example how to use it in &lt;code&gt;useEffect()&lt;/code&gt;hook:&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;useEffect&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="nf"&gt;useEffect&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;fetchData&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;
  
  
  2. Fetching Data with Axios library
&lt;/h3&gt;

&lt;p&gt;It does the same job as Fetch, but the main difference is that it already returns the result as JSON object, so we don't need to convert it.&lt;/p&gt;

&lt;p&gt;First we need to install it using &lt;em&gt;npm&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Than we need to import it to our project and we can use it in the same function &lt;code&gt;fetchData()&lt;/code&gt; instead of &lt;code&gt;fetch()&lt;/code&gt; method:&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="nx"&gt;axios&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;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchData&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="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://randomuser.me/api/&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's convenient about using Axios is that it has a much shorter syntax that allows us to cut down on our code and it includes a lot of tools and features which Fetch does not have in its API.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.Fetching Data with Async-Await syntax
&lt;/h3&gt;

&lt;p&gt;In ES7, it became possible to resolve promises using the &lt;em&gt;async-await&lt;/em&gt; syntax. If you are not familiar with such function, check &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The benefit of this is that it enables us to remove our .then() callbacks and simply get back our asynchronously resolved data. &lt;/p&gt;

&lt;p&gt;Lets re-write our &lt;code&gt;fetchData()&lt;/code&gt; function using this syntax:&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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;result&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;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://randomuser.me/api/&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4.Fetching Data with Custom Hook
&lt;/h3&gt;

&lt;p&gt;We can use the library React-Fetch-Hook to extract the data from API. It includes already several properties we can use: &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt; for errors handling and &lt;code&gt;isLoading&lt;/code&gt; for loading issues. &lt;/p&gt;

&lt;p&gt;First it should be installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-fetch-hook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then it should be imported and used on top of common parent component:&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="nx"&gt;useFetch&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-fetch-hook&lt;/span&gt;&lt;span class="dl"&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;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useFetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://randomuser.me/api/&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are other ways for data fetching as React Query library and GraphQL API, but this blog post is not covering them in depth, but you are free to explore those :)&lt;br&gt;
Happy Fetching!!!&lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is React Helmet and Where To Use It</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 22 Mar 2021 09:19:46 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/what-is-react-helmet-and-where-to-use-it-2ed3</link>
      <guid>https://dev.to/olenadrugalya/what-is-react-helmet-and-where-to-use-it-2ed3</guid>
      <description>&lt;p&gt;This blog post is about an interesting React component - &lt;strong&gt;React Helmet&lt;/strong&gt;.&lt;br&gt;
Before you read this article, you have to be familiar with  &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; library. You would also need a basic knowledge about the HTML &lt;code&gt;head&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;I came across this component while building Gatsby website and it got me interested since I have never come across it before. Let's explore what it is, as well as where and how to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is React Helmet
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="https://github.com/nfl/react-helmet" rel="noopener noreferrer"&gt;official docs&lt;/a&gt; &lt;strong&gt;React Helmet&lt;/strong&gt; is a reusable component, which helps manage all of your changes to the document head. &lt;br&gt;
For example, you can use React Helmet to set the title, description and meta tags for the document dynamically. This is very handy when you have a project with multiple routes and want to update the meta tags for SEO based on the route currently rendered to the page.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Install and Use
&lt;/h2&gt;

&lt;p&gt;It exists actually in the "react-helmet" library, so first we need to install this library:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;helmet&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Now we can import &lt;strong&gt;React Helmet&lt;/strong&gt; component to our project and use it. The following example sets the page title, language and description.&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Helmet&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-helmet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SEO&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="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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&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;Helmet&lt;/span&gt; &lt;span class="na"&gt;htmlAttributes&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="nt"&gt;html&lt;/span&gt; &lt;span class="na"&gt;lang&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"en"&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="nt"&gt;title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello from React Helmet&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;title&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="nt"&gt;meta&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"description"&lt;/span&gt; &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Basic example"&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;Helmet&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="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;
  
  
  Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Supports all valid head tags: &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;base&lt;/code&gt;, &lt;code&gt;meta&lt;/code&gt;, &lt;code&gt;link&lt;/code&gt;, &lt;code&gt;script&lt;/code&gt;, &lt;code&gt;noscript&lt;/code&gt;, and &lt;code&gt;style&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Supports attributes for &lt;code&gt;body&lt;/code&gt;, &lt;code&gt;html&lt;/code&gt; and &lt;code&gt;title&lt;/code&gt; tags.&lt;/li&gt;
&lt;li&gt;Supports server-side rendering.&lt;/li&gt;
&lt;li&gt;Nested components override duplicate head changes (Components further down the tree can override values provided to the Helmet component on a higher level).&lt;/li&gt;
&lt;li&gt;Duplicate head changes are preserved when specified in the same component (support for tags like "apple-touch-icon").&lt;/li&gt;
&lt;li&gt;Callback for tracking DOM changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Dynamic Helmet
&lt;/h2&gt;

&lt;p&gt;Above was an example of simple usage of Helmet, but this is unlikely, that you are going to use it like this in the project. The next example will show how to add title, metadata and other important SEO elements dynamically and importing it inside any component you want. &lt;/p&gt;

&lt;p&gt;We are going to create a SEO component, which will receive some data as props and fill it up in to &lt;code&gt;head&lt;/code&gt; section of the page. &lt;/p&gt;

&lt;p&gt;1.We destructure components &lt;em&gt;props&lt;/em&gt; and receive &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt; and &lt;code&gt;meta&lt;/code&gt; array (it is empty by default):&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Helmet&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-helmet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SEO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;meta&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="p"&gt;{.....}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;2.Now we can use destructured data as properties of &lt;strong&gt;Helmet&lt;/strong&gt; component. We can also use property &lt;code&gt;htmlAttributes&lt;/code&gt; to set up language for the website:&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Helmet&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-helmet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SEO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;meta&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="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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Helmet&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;htmlAttributes&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;lang&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`description`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;description&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="si"&gt;}&lt;/span&gt;
   &lt;span class="p"&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;The &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt; tags are crawled by search engines, that's why it is important to use those at first place.&lt;/p&gt;

&lt;p&gt;3.We can add a bunch of meta objects to the &lt;code&gt;meta&lt;/code&gt; array. They are divided into 2 groups - &lt;strong&gt;OpenGraph&lt;/strong&gt; tags and &lt;strong&gt;Twitter&lt;/strong&gt; tags. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenGraph tags&lt;/strong&gt; (marked with &lt;em&gt;og:&lt;/em&gt;) are crawled by Facebook whenever you share a link through Messenger/ Facebook:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="nx"&gt;meta&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someUrl&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someArticle&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someTitle&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someDescription&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;og:image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someImage&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fb:app_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someFbAppId&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;&lt;strong&gt;Twitter tags&lt;/strong&gt; (marked with &lt;em&gt;twitter&lt;/em&gt;) are crawled by Twitter accordingly: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;


&lt;span class="nx"&gt;meta&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twitter:card&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someSummary&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twitter:creator&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someAuthorName&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twitter:title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someTitle&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twitter:description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someDescription&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twitter:image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someImage&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;This was just a simple example how we can create reusable dynamic SOE component. If you wonder how to make a complex one, check this &lt;a href="https://github.com/kentcdodds/kentcdodds.com/blob/main/src/components/seo/index.js" rel="noopener noreferrer"&gt;SEO&lt;/a&gt; made by Kent Dods. &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;React Helmet&lt;/strong&gt; is aimed to manage and dynamically set what’s in the document’s &lt;code&gt;head&lt;/code&gt; section. &lt;br&gt;
It comes-in especially handy when combined with server-side rendering because it allows to set meta tags that will be read by search engines and social media crawlers. This makes server-side rendering and React Helmet a dynamic duo for creating apps that are SEO and social media friendly.&lt;br&gt;
&lt;strong&gt;Helmet&lt;/strong&gt; is being used as plugin in &lt;a href="https://www.gatsbyjs.com/plugins/gatsby-plugin-react-helmet/" rel="noopener noreferrer"&gt;Gatsby&lt;/a&gt; framework. &lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why Gatsby needs GraphQL?</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 15 Mar 2021 09:12:43 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/why-gatsby-needs-graphql-3hbi</link>
      <guid>https://dev.to/olenadrugalya/why-gatsby-needs-graphql-3hbi</guid>
      <description>&lt;p&gt;This blog post is going to describe why Gatsby needs GraphQL and how it uses it.&lt;/p&gt;

&lt;p&gt;If you read this post, you are probably already familiar with &lt;a href="https://www.gatsbyjs.com/docs/tutorial/" rel="noopener noreferrer"&gt;Gatsby&lt;/a&gt; (React framework for generating static websites) and &lt;a href="https://www.howtographql.com/" rel="noopener noreferrer"&gt;GraphQL&lt;/a&gt; (query language for web APIs). If you are not familiar with those, I strongly insist on learning those first (I added links to documentation on both of these technologies).&lt;/p&gt;

&lt;p&gt;When I decided to build my portfolio using Gatsby, i didn't know that its not that easy :) Well, nothing is easy in a tech world, but I still hoped that if it's a framework and it uses React, i will have my portfolio built in no time. Well, nope. &lt;/p&gt;

&lt;p&gt;It appeared that for some reasons (which were unknown to me at the beginning), Gatsby uses GraphQL to make queries. That was what official documentation and tutorials told me. &lt;br&gt;
But it was still unclear WHY it uses GraphQL. So here Im trying to explore this question.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Gatsby is Managing Data
&lt;/h2&gt;

&lt;p&gt;Gatsby framework is structured the way, that allows to build websites using all parts for that - HTML, CSS, JavaScript and data. The first 3 parts are covered by React, and the data is the rest what lives outside React component. &lt;/p&gt;

&lt;p&gt;It is possible to build a website using only React components and everything that is being put &lt;em&gt;inside&lt;/em&gt; (texts, images etc.). But sometimes it's necessary to get data from &lt;em&gt;outside&lt;/em&gt; the React component or to store it &lt;em&gt;outside&lt;/em&gt; and then bring it &lt;em&gt;inside&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Gatsby’s data layer allows to &lt;strong&gt;pull data&lt;/strong&gt; directly into the components from the following sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WordPress&lt;/li&gt;
&lt;li&gt;filesystems (markdown, JSON etc.)&lt;/li&gt;
&lt;li&gt;headless CMS&lt;/li&gt;
&lt;li&gt;Drupal&lt;/li&gt;
&lt;li&gt;Netlify CMS&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;li&gt;databases&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Gatsby and GraphQL: The Mechanism
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;GraphQL&lt;/strong&gt; is a query language, which Gatsby is using to &lt;em&gt;pull&lt;/em&gt; data to React components. It's absolutely &lt;em&gt;not necessary&lt;/em&gt; to use GraphQL to source the data, there are &lt;a href="https://www.gatsbyjs.com/docs/how-to/querying-data/using-gatsby-without-graphql/" rel="noopener noreferrer"&gt;other ways&lt;/a&gt; to do that. But because GraphQL is already configured for Gatsby framework and because it's same Facebook technology, it is recommended to use it for data sourcing. &lt;/p&gt;

&lt;p&gt;Now that we are figured out, WHY we use GraphQL here, let's see HOW exactly we use it. &lt;/p&gt;

&lt;p&gt;When we start the project with Gatsby, we can see our website at &lt;code&gt;localhost:8000&lt;/code&gt; port. If we navigate to &lt;code&gt;localhost:8000__graphQL&lt;/code&gt;, we will see all available queries for the website:&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%2Fjb1yswdd04v18874ts90.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%2Fjb1yswdd04v18874ts90.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The names of the queries on the left are keywords for Gatsby to understand where to get data from. &lt;/p&gt;

&lt;p&gt;Alright, let's get to practice! let's consider that we have a Gatsby projects with 3 React components - Layout, Home and About. Both of them want to use the same &lt;code&gt;title&lt;/code&gt; info. We can store this info in one location and reference that location from other files.&lt;/p&gt;

&lt;p&gt;Below is &lt;strong&gt;step-by-step guide&lt;/strong&gt; with an example of how to do that:&lt;/p&gt;
&lt;h4&gt;
  
  
  1.Create React components:
&lt;/h4&gt;

&lt;p&gt;src/components/layout.js&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Link&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;gatsby&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Layout&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&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;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/`&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="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; My Awesome Blog &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&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;Link&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;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/about/`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; About &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;src/pages/index.js&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Layout&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;../components/layout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Layout&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="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Amazing Blog which Im creating with Gatsby&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&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="nt"&gt;div&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="nt"&gt;img&lt;/span&gt;
          &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;
          &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Group of pandas eating bamboo"&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="nt"&gt;div&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;Layout&lt;/span&gt;&lt;span class="p"&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;src/pages/about.js&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Layout&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;../components/layout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;About&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Layout&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="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;About My Awesome Blog&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&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="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&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;Layout&lt;/span&gt;&lt;span class="p"&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;h4&gt;
  
  
  2.Open &lt;em&gt;gatsby-config.js&lt;/em&gt; file.
&lt;/h4&gt;

&lt;p&gt;The place for common pieces of data is the &lt;code&gt;siteMetadata&lt;/code&gt; object in the &lt;em&gt;gatsby-config.js&lt;/em&gt; file. Add site title to this file:&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;siteMetadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My Awesome Blog&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;plugins&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;h4&gt;
  
  
  3.Create the query
&lt;/h4&gt;

&lt;p&gt;After we have added data to &lt;code&gt;siteMetadata&lt;/code&gt; object, we can make the query with GraphQL to pull this data to the component which need it. &lt;/p&gt;

&lt;p&gt;To write the query, we will use &lt;code&gt;useStaticQuery&lt;/code&gt;, a new Gatsby feature that provides the ability to use a React Hook to query with GraphQL at build time. Here we should pause and learn the difference between &lt;a href="https://www.gatsbyjs.com/docs/how-to/querying-data/static-query/#how-staticquery-differs-from-page-query" rel="noopener noreferrer"&gt;the static query and the page query&lt;/a&gt; in case we are not familiar with this. &lt;/p&gt;

&lt;p&gt;Let's open &lt;code&gt;Layout.js&lt;/code&gt; file, import GraphQL there and create a query which we save to &lt;code&gt;data&lt;/code&gt; variable:&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useStaticQuery&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;gatsby&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Layout&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useStaticQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="s2"&gt;`
      query {
        site {  
          siteMetadata {
            title
          }
        }
      }
    `&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&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;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/`&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="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; My Awesome Blog &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&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;Link&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;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/about/`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; About &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;h4&gt;
  
  
  4.Use the data inside the component
&lt;/h4&gt;

&lt;p&gt;We have created a variable &lt;code&gt;data&lt;/code&gt; and saved the result of the query in it. The keyword &lt;code&gt;site&lt;/code&gt; in the query points Gatsby where to look for the data during the build. Gatsby sees &lt;strong&gt;site&lt;/strong&gt; keyword, goes to &lt;em&gt;gatsby-config.js&lt;/em&gt;, finds the object &lt;code&gt;siteMetadata&lt;/code&gt;, pulls out &lt;code&gt;title&lt;/code&gt; and stores it to variable &lt;code&gt;data&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now all we need to do is to use &lt;code&gt;data&lt;/code&gt; where we want it (in our case - instead of hard-coded &lt;code&gt;h3&lt;/code&gt; tag:&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useStaticQuery&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;gatsby&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Layout&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useStaticQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="s2"&gt;`
      query {
        site {  
          siteMetadata {
            title
          }
        }
      }
    `&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&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;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/`&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="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;siteMetadata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&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;Link&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;Link&lt;/span&gt; &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/about/`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; About &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;Since our &lt;code&gt;Home&lt;/code&gt; and &lt;code&gt;About&lt;/code&gt; components are wrapped up with &lt;code&gt;Layout&lt;/code&gt; component, the title will appear in every of them. &lt;br&gt;
If we decide to change the title, all we have to do is to change &lt;code&gt;siteMetadata&lt;/code&gt; object property &lt;code&gt;title&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;GraphQL is certainly not required, but the benefits of adopting GraphQL are significant. GraphQL will simplify the process of building and optimising pages, so it’s considered a best practice for structuring and writing Gatsby applications.&lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>gatsby</category>
      <category>beginners</category>
      <category>graphql</category>
    </item>
    <item>
      <title>Introduction to Custom Hooks</title>
      <dc:creator>Olena Drugalya</dc:creator>
      <pubDate>Mon, 08 Mar 2021 09:47:22 +0000</pubDate>
      <link>https://dev.to/olenadrugalya/introduction-to-custom-hooks-2nmk</link>
      <guid>https://dev.to/olenadrugalya/introduction-to-custom-hooks-2nmk</guid>
      <description>&lt;p&gt;This blog post continues the &lt;strong&gt;React Hooks&lt;/strong&gt; series and gives an introduction to Custom Hooks in React.&lt;br&gt;
Let's learn what it takes to create a custom React Hook as well as all the rules we must keep in mind when using Hooks.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Custom Hook?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Custom Hook&lt;/strong&gt; is a JavaScript function which we create by ourselves, when  we want to share logic between other JavaScript functions. It allows you to reuse some piece of code in several parts of your app.&lt;/p&gt;
&lt;h2&gt;
  
  
  When and How to Use
&lt;/h2&gt;

&lt;p&gt;When we want to share the logic between other components, we can extract it to a separate function. According to official documents, the custom hook has to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;start with the key word &lt;strong&gt;use&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;call other hooks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because &lt;strong&gt;custom hook&lt;/strong&gt; is a JS function, the Rules of Hooks apply to it as well. Those are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Never call Hooks from inside a loop, condition or nested function&lt;/li&gt;
&lt;li&gt;Hooks should sit at the top-level of your component&lt;/li&gt;
&lt;li&gt;Only call Hooks from React functional components&lt;/li&gt;
&lt;li&gt;Never call a Hook from a regular function&lt;/li&gt;
&lt;li&gt;Hooks can call other Hooks&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  How to Create Custom Hook?
&lt;/h2&gt;

&lt;p&gt;You create the hook same way as you create any JS function. Look at it as a refactoring of code into another function to make it reusable. It will speed up your productivity and save your time.&lt;/p&gt;

&lt;p&gt;Let's consider the following example, where we have &lt;code&gt;useEffect()&lt;/code&gt;hook which updates the document title:&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;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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="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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&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="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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="mi"&gt;0&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;incrementCount&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;useEffect&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`You clicked &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; times`&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&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="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;You clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&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="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;incrementCount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;ClickMe&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&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="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;What we want to do is to create a custom hook, which accepts a piece of text and updates the document title for us. Here is how we do that:&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;useDocumentTitle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&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;useEffect&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;title&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="nx"&gt;title&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;Our &lt;code&gt;useDocumentHook()&lt;/code&gt; now accepts the string and calls &lt;code&gt;useEffect()&lt;/code&gt; hook inside, which updates the document title with a given title, when the title was changed (we pass title as dependency here).&lt;/p&gt;

&lt;p&gt;So our final code would look like this:&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;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useDocumentTitle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&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;useEffect&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;title&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="nx"&gt;title&lt;/span&gt;&lt;span class="p"&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;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&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="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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="mi"&gt;0&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;incrementCount&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useDocumentTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You clicked &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; times`&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&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="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;You clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&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="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;incrementCount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&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="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;And that is all, as simple as that :) In my next blog post I'll share 5 the most commonly used custom hooks, which make your development faster and easier.&lt;/p&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://www.linkedin.com/in/olenadrugalya/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/OlenaDrugalya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/J3J42JOOA" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.ko-fi.com%2Fcdn%2Fkofi1.png%3Fv%3D2" alt="Buy Me a Coffee at ko-fi.com"&gt;&lt;/a&gt;&lt;/p&gt;

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