<?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: Amine Amhoume</title>
    <description>The latest articles on DEV Community by Amine Amhoume (@amiinequ).</description>
    <link>https://dev.to/amiinequ</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%2F513704%2F90e7074c-0af5-4076-aaf3-13297415740b.jpg</url>
      <title>DEV Community: Amine Amhoume</title>
      <link>https://dev.to/amiinequ</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amiinequ"/>
    <language>en</language>
    <item>
      <title>React all hooks series: Let' acquire the useContext hook.</title>
      <dc:creator>Amine Amhoume</dc:creator>
      <pubDate>Tue, 01 Feb 2022 07:57:13 +0000</pubDate>
      <link>https://dev.to/amiinequ/react-all-hooks-series-let-acquire-the-usecontext-hook-8a0</link>
      <guid>https://dev.to/amiinequ/react-all-hooks-series-let-acquire-the-usecontext-hook-8a0</guid>
      <description>&lt;p&gt;Look! Every feature should solve a problem, so if you want to understand a new concept of a feature, then you have to figure out what problem it solves and how.&lt;/p&gt;

&lt;p&gt;This is a journey to comprehend the problem with passing data using props and how Context API solve this problem. By the end of this tutorial will get the prize of acquiring the power of &lt;code&gt;useContext&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;Prerequisites? Basic ReactJS, know how to use  &lt;code&gt;useState()&lt;/code&gt; hook, and to understand what are props.&lt;/p&gt;

&lt;p&gt;That's it. Let's go.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Context API?
&lt;/h1&gt;

&lt;p&gt;Context provides a way to pass data through the component tree without having to pass props down manually at every level. We can implement it to work with global state and avoid props drilling. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/9uIjYTJCtMnHsMpbHW/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/9uIjYTJCtMnHsMpbHW/giphy.gif" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Props drilling is when we are obligated to pass props to components that don't use them but their children do.&lt;/p&gt;

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

&lt;p&gt;In the above picture, we are obligated to pass the&lt;code&gt;name&lt;/code&gt; state through the &lt;code&gt;Settings&lt;/code&gt; component as a middle man to work with it in the &lt;code&gt;Profile&lt;/code&gt; and &lt;code&gt;Shipping&lt;/code&gt; components ( Maybe pass it to the Appearance component too if it has children that require the state "name").&lt;/p&gt;

&lt;p&gt;Code example?&lt;/p&gt;

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

&lt;p&gt;In the &lt;code&gt;Home&lt;/code&gt; component, we have the global state called &lt;code&gt;name&lt;/code&gt;, consider it as a username. We pass the &lt;code&gt;name&lt;/code&gt; and&lt;code&gt;setName&lt;/code&gt; to the &lt;code&gt;Settings&lt;/code&gt; component as props and transfer them again to the &lt;code&gt;Profile&lt;/code&gt; and &lt;code&gt;Shipping&lt;/code&gt; components.&lt;/p&gt;

&lt;p&gt;Let's cut the middle man by using the Context API. &lt;/p&gt;

&lt;p&gt;First, we import &lt;code&gt;createContext&lt;/code&gt; , initialize it with a null value, and export it :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useState, createContext} from 'react';
export const NameContext = createContext(null);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we wrap the component we want to pass the data down to with a context &lt;code&gt;Provider&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;Keep in mind that the &lt;code&gt;value&lt;/code&gt; prop is required. Here, we are passing an object.&lt;/p&gt;

&lt;p&gt;Now the &lt;code&gt;Settings&lt;/code&gt; component is clean.&lt;/p&gt;

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

&lt;p&gt;"Profile" and "Shippings":&lt;/p&gt;

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

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

&lt;p&gt;We import the &lt;code&gt;NameContext&lt;/code&gt; we initialized before in &lt;code&gt;Home&lt;/code&gt; , then we create a &lt;code&gt;Consumer&lt;/code&gt; that wraps a function, the function takes an &lt;code&gt;obj&lt;/code&gt; as a parameter ( it's the one we passed using the value prop) and return &lt;code&gt;JSX&lt;/code&gt; with the state we want to render.&lt;/p&gt;

&lt;p&gt;That was nice, right? I mean we cut the middle man (Settings component ). We no longer depend on it.&lt;/p&gt;

&lt;p&gt;Our solution to props drilling is working but it's vulnerable.&lt;/p&gt;

&lt;p&gt;Imagine if we have more that a global state to pass down the component tree.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/mEV42F38lur6PbfapW/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/mEV42F38lur6PbfapW/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's create another state called currency. User's currency in use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [currency, setCurrency] = useState('USD')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We initialize a context for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const CurrencyContext = createContext(null);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, pass it by wrapping the &lt;code&gt;Settings&lt;/code&gt; component with the &lt;code&gt;currencyContext.Provider&lt;/code&gt; :&lt;/p&gt;

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

&lt;p&gt;Let's use it in the &lt;code&gt;Shipping&lt;/code&gt; component:&lt;/p&gt;

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

&lt;p&gt;Inside the &lt;code&gt;NameContext&lt;/code&gt; consumer, we inject the &lt;code&gt;CurrencyContext&lt;/code&gt; consumer with a function that returns the JSX which uses the state. &lt;/p&gt;

&lt;p&gt;It's hairy and ugly. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hrpCvvpELwhN2JQZR9/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hrpCvvpELwhN2JQZR9/giphy.gif" width="480" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need to simplify it. Let's invoke the &lt;code&gt;useContext&lt;/code&gt; hook.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Amazing useContext hook.
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;useContext&lt;/code&gt; is so simple to use. First, we import it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useContext} from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the &lt;code&gt;Profile&lt;/code&gt; component, we initialize it with the name of the context as a value:&lt;/p&gt;

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

&lt;p&gt;Just like that, we use it inside our JSX. No nested functions and thus no ugliness.&lt;/p&gt;

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

&lt;p&gt;Congratualations, the journey has ended and you have acquired a new hook. Use it cleverly.&lt;/p&gt;

&lt;p&gt;That was quite straight to the point, wasn't it?&lt;/p&gt;

&lt;p&gt;See you in the next article…&lt;/p&gt;

&lt;p&gt;Wait! &lt;/p&gt;

&lt;p&gt;I'll be glad if you share my article.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>redux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>ReactJS all hooks series: UseReducer in action.</title>
      <dc:creator>Amine Amhoume</dc:creator>
      <pubDate>Fri, 21 Jan 2022 14:31:11 +0000</pubDate>
      <link>https://dev.to/amiinequ/reactjs-all-hooks-series-usereducer-in-action-360m</link>
      <guid>https://dev.to/amiinequ/reactjs-all-hooks-series-usereducer-in-action-360m</guid>
      <description>&lt;p&gt;To be honest, I am tired of counter-app examples.&lt;/p&gt;

&lt;p&gt;I had to step on and write this article to demonstrate to react beginners a real use case of &lt;code&gt;useReducer()&lt;/code&gt; because I am sure you are tired of the counter app examples too. No?&lt;/p&gt;

&lt;p&gt;In this article, we'll discuss what  is &lt;code&gt;useReducer()&lt;/code&gt;, its syntax, and implement it to create an effect of a well-known website (spoiler, it's Pinterest).&lt;/p&gt;

&lt;p&gt;Prerequisite? Basic ReactJS and to know what is "state". Are you familiar with Redux or not? It doesn't matter. Optionally, it's good to know the reduce() method. Here's a small &lt;a href="https://dev.to/amiinequ/javascripts-reduce-method-in-a-nutshell-gik"&gt;post that explains it in a nutshell.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this article is for beginners who are into React hooks.&lt;/p&gt;

&lt;p&gt;Before we proceed I would like to state that I am jobless for the moment and I am looking for a remote frontend job position. How can you help? Refer my &lt;a href="https://amhoume.com" rel="noopener noreferrer"&gt;portfolio&lt;/a&gt; to someone who's looking for a frontend developer, less than that, you can share my article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/E2CMs3zElINbc4pNC7/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/E2CMs3zElINbc4pNC7/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you and let's go.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is useReducer() hook?
&lt;/h1&gt;

&lt;p&gt;It's all about state management. &lt;code&gt;useReducer()&lt;/code&gt; is an alternative to &lt;code&gt;useState()&lt;/code&gt;. We use it to handle complex state logic because it gives us the ability to handle complex state logic that involves sub-values or when the next state depends on the previous one.&lt;/p&gt;

&lt;p&gt;Here's the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useReducer } from 'react';

const initialState = SomeValue;

const reducer = (state, action) =&amp;gt; {
    switch(action)
      case 'toClick':
         return newState
    default:
      throw new Error("Unexpected action");
}

function ComponentA() {

      const [actualState, dispatch] = useReducer(reducer, initialState);

      return (
           &amp;lt;button onClick={() =&amp;gt; dispatch('toClick')}&amp;gt;
                Click me
           &amp;lt;/button&amp;gt;
           );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We import &lt;code&gt;useReducer&lt;/code&gt;, then we have the &lt;code&gt;initialState&lt;/code&gt; which is the default value of the state.&lt;/p&gt;

&lt;p&gt;Also, the reducer function takes two arguments: The state and an action and contains a switch statement. &lt;/p&gt;

&lt;p&gt;In case the action is 'toClick' we will do something to the state and return it. But where? Now comes the hook itself.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const [actualState, dispatch] = useReducer(reducer, initialState);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useReducer()&lt;/code&gt; returns an array of two values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the state ( where we save our calculated value from the switch statement).&lt;/li&gt;
&lt;li&gt;A dispatch() method. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;useReducer()&lt;/code&gt; hook itself takes two arguments: the reducer function and the initial state of which we defined before.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useReducer()&lt;/code&gt; takes the &lt;code&gt;initailState&lt;/code&gt; and passes it to the reducer function through the state argument and whatever it returns, it saves it inside the &lt;code&gt;actualState&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In programming, everything is confusing until explained with a real example.&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%2Fhxla5f51pqthce0tkycq.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%2Fhxla5f51pqthce0tkycq.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  A Real use case.
&lt;/h1&gt;

&lt;p&gt;So I decided to work on a Pinterest clone. It turns out that the home page has this effect where four buttons represents four themes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/6cVo7eXTaaVC0SE8Dg/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/6cVo7eXTaaVC0SE8Dg/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I have four buttons and each click changes the state of four elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the color for the buttons.&lt;/li&gt;
&lt;li&gt;The theme title.&lt;/li&gt;
&lt;li&gt;The background pictures.&lt;/li&gt;
&lt;li&gt;The color arrow down button&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's go.&lt;/p&gt;

&lt;p&gt;The default text is "Weeknight dinner" so I created a theme named &lt;code&gt;dinner&lt;/code&gt; with the color of &lt;code&gt;darkYello&lt;/code&gt;, so my initial state is going to be an object with three states: The color, the text, and the theme.&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%2F2rzuhzk8ae7psuof5l0a.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%2F2rzuhzk8ae7psuof5l0a.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next is the dispatcher:&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%2F1uymf2fax6gd4uhr3wb9.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%2F1uymf2fax6gd4uhr3wb9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It acts like the usual &lt;code&gt;setState&lt;/code&gt; but instead, it sets an action. &lt;/p&gt;

&lt;p&gt;Here, we have four buttons, each one has a dispatch function with an &lt;code&gt;action&lt;/code&gt; as an object which contains the action's type.&lt;/p&gt;

&lt;p&gt;Now the reducer function:&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%2Fqr46cf9x0mb3hlragmwd.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%2Fqr46cf9x0mb3hlragmwd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It takes two arguments: the state and the action.&lt;/p&gt;

&lt;p&gt;We defined a switch statement because we have multiple actions. So, in case the user clicked on the button with the dispatcher &lt;code&gt;green&lt;/code&gt;,  the case &lt;code&gt;green&lt;/code&gt; will be triggered and change the state of Color, text, and theme.&lt;/p&gt;

&lt;p&gt;Now the &lt;code&gt;useReducer()&lt;/code&gt; hook.&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%2Fy9s6ulan9yxrhjf0npg2.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%2Fy9s6ulan9yxrhjf0npg2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It takes the reducer function defined earlier and the initial state and returns an array. The returned array contains an object of the states: text, theme, and color, not to forget the dispatch method.&lt;/p&gt;

&lt;p&gt;Here's how I am implementing it in the JSX.&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%2Ffnj1ozwm3mgocqzg3k5m.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%2Ffnj1ozwm3mgocqzg3k5m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PS: I am using TailwindCSS.&lt;/p&gt;

&lt;p&gt;If the state is &lt;code&gt;darkYello&lt;/code&gt;, then the background is &lt;code&gt;bg-darkYello&lt;/code&gt;, if it's blue or green then the background is going to be gray.&lt;/p&gt;

&lt;p&gt;And for the title.&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%2F0bn56tjgyjmt4716afnu.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%2F0bn56tjgyjmt4716afnu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am just placing the state text and if's null it will display "Weeknight dinner" as a default value.&lt;/p&gt;

&lt;h1&gt;
  
  
  A wrap up!
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/2aI0NbmSnKo9R4NgFE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/2aI0NbmSnKo9R4NgFE/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The user will click on a button and the dispatch function will be triggered with an action, The reducer function will take that action plus the current state (if it's the first time, then it will take the initial state) and compare the action's type with all the cases, if one is found it will do the logic and assign the result to the states defined in the &lt;code&gt;useReducer()&lt;/code&gt; hook, if no, it will return a state or throw an exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/lmvMgBJU9oRZxmTljV/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/lmvMgBJU9oRZxmTljV/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Is that it? &lt;/p&gt;

&lt;p&gt;Well! yes, but actually no. &lt;/p&gt;

&lt;p&gt;It's time for you to practice. If you have a dummy react project that uses &lt;code&gt;useState&lt;/code&gt;, try it to replace it with useReducer.&lt;/p&gt;

&lt;p&gt;The goal was to make you understand the &lt;code&gt;useReducer()&lt;/code&gt; hook without using the counter app example.&lt;/p&gt;

&lt;p&gt;I hope that you enjoyed the article and if so, please don't forget to share it. Also, feel free to add a comment if you see that I forgot to mention something important. &lt;/p&gt;

&lt;p&gt;Thank you. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>redux</category>
    </item>
    <item>
      <title>Javascript's reduce method in a nutshell.</title>
      <dc:creator>Amine Amhoume</dc:creator>
      <pubDate>Tue, 18 Jan 2022 11:57:56 +0000</pubDate>
      <link>https://dev.to/amiinequ/javascripts-reduce-method-in-a-nutshell-gik</link>
      <guid>https://dev.to/amiinequ/javascripts-reduce-method-in-a-nutshell-gik</guid>
      <description>&lt;p&gt;The word "reduce" in the English language means :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;to diminish in size, amount, extent, or number&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's suppose that we have an array of items&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const cartItems = [1,3,5,7,9];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I want the sum of all the items.&lt;/p&gt;

&lt;p&gt;I could use the For Loop but it's going to be a bit hairy. The method &lt;code&gt;reduce()&lt;/code&gt; will give us one total number with less code (always go for the less-code option).&lt;/p&gt;

&lt;p&gt;reduce() takes two arguments: a callback function (the reducer itself) and an initial value. The callback function takes two arguments: the previous value and the current value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let total = cartItems.reduce((previousValue, currentValue) =&amp;gt; {
    return previousValue + currentValue;
}, 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's calculate the first rotation on the array.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;previousValue&lt;/code&gt; is going to be equal to 0 while the &lt;code&gt;currentValue&lt;/code&gt; is going to be equal to the first item in the array, which is 1.&lt;/p&gt;

&lt;p&gt;Next, the &lt;code&gt;previousValue&lt;/code&gt; is going to be equal to 1 while the &lt;code&gt;currentValue&lt;/code&gt; is going to be equal to 3 and so it goes. The total amount will be sum of all the numbers: 25&lt;/p&gt;

&lt;p&gt;Keep in mind that the initial Value is totally optional. When omitted, the first value of the array is used as an initial value.&lt;/p&gt;

&lt;p&gt;Note: the reverse of the &lt;code&gt;reduce()&lt;/code&gt; method is &lt;code&gt;reduceRight()&lt;/code&gt;. Yes! It takes items from right to left.&lt;/p&gt;

&lt;p&gt;Ladies and gentlemen, that was a quick tutorial of the reduce() method. &lt;/p&gt;

&lt;p&gt;Don't forget to practice.&lt;/p&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Dev beginners, learn to use &lt;details&gt; and &lt;summary&gt; elemnts in your favor.</title>
      <dc:creator>Amine Amhoume</dc:creator>
      <pubDate>Thu, 30 Sep 2021 17:27:10 +0000</pubDate>
      <link>https://dev.to/amiinequ/dev-beginners-learn-to-use-details-and-summary-elemnts-in-your-favor-5282</link>
      <guid>https://dev.to/amiinequ/dev-beginners-learn-to-use-details-and-summary-elemnts-in-your-favor-5282</guid>
      <description>&lt;p&gt;You know when you are just into learning HTML and CSS and you are like "Wooow! There are too many tags and attributes to remember. I have to remember them all". &lt;/p&gt;

&lt;p&gt;No! You don't. &lt;/p&gt;

&lt;p&gt;I say, practice what you know. Discover what you don't know, then practice it. &lt;/p&gt;

&lt;p&gt;To be short, in this article, you will learn how to use &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; HTML elements to make an accordion. We will create an accordion and style it with CSS but first…&lt;/p&gt;

&lt;h1&gt;
  
  
  What are Details and Summary? 
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt; HTML element creates a widget that can disclose some information when toggled. The element has two modes: close and open. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; tag which is apart from the  element and it should be filled with the title of the accordion. A question most of the time. &lt;br&gt;
Enough theory. Here's an example:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;details&amp;gt;
      &amp;lt;summary&amp;gt; What is a Details tag?&amp;lt;/summary&amp;gt;
      &amp;lt;p&amp;gt;
        The details HTML element creates a disclosure widget in which information is visible only when the widget is toggled into an "open" state. A summary or label must be provided using the summary  element.
      &amp;lt;/p&amp;gt;
    &amp;lt;/details&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fqpymz6td1lamzxw6gogv.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%2Fqpymz6td1lamzxw6gogv.png" alt="FAQ accordion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that the &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; tag is inside &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We can implement this to create a FAQ accordion such as in the picture above. &lt;/p&gt;

&lt;p&gt;Here's the HTML markup:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
  &amp;lt;div class="question-1"&amp;gt;
    &amp;lt;details&amp;gt;
      &amp;lt;summary&amp;gt; What is a Details tag?&amp;lt;/summary&amp;gt;
      &amp;lt;p&amp;gt;
        The details HTML element creates a disclosure widget in which information is visible only when the widget is toggled into an "open" state. A summary or label must be provided using the summary  element.
      &amp;lt;/p&amp;gt;
    &amp;lt;/details&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;div class="question-2"&amp;gt;
    &amp;lt;details&amp;gt;
      &amp;lt;summary&amp;gt; When to use it?&amp;lt;/summary&amp;gt;
      &amp;lt;p&amp;gt;
        We use details tag when we want to make an accordion element.
      &amp;lt;/p&amp;gt;
    &amp;lt;/details&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;All simple. We have a div container and two other divs as questions. Inside the two questions we have the details tag, the summary element as the question, and the p tag as the answer to the questions. Easy to grasp. &lt;/p&gt;

&lt;p&gt;In the CSS file, we start by setting up our body such so:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*{
  box-sizon: border-box;
  margin: 0;
  padding: 0;
}
body{
    background-color: #CA2E55;
    font-family: 'Roboto', sans-serif;
    font-weight: 500;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Extremely simple. Now let's style our container:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #FFE0B5;
    width: 50%;
    margin: 20px auto;
    border-radius: 15px ;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To put the container in the center we can use a width that is less than 100% and the magical: &lt;code&gt;margin: 0 auto;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We put a 20px margin so it doesn't stick at the top. We use flex to center everything in the center.&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%2Fnue2mjmspjbxnhkuiha4.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%2Fnue2mjmspjbxnhkuiha4.png" alt="FAQ accordion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;question-1 and question-2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;question-1 and question-2: 
.question-1{
  width: 90%;
  border: 2px solid;
  margin: 15px 0;
  border-radius: 15px ;
}
.question-2{
  width: 90%;
  border: 2px solid; 
  margin: 15px 0;
  border-radius: 15px ;
}
amhoume.medium.comNow the details element when closed. 
details{
  font-size: 1.5rem;
  margin: 10px;
  cursor: pointer;
}
and when it's opened. 
details[open]{
  background-color: white;
  transition: all .8s ease;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fxew273hpxhavzz7e0dcc.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%2Fxew273hpxhavzz7e0dcc.png" alt="FAQ accordion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the details element when closed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;details{
  font-size: 1.5rem;
  margin: 10px;
  cursor: pointer;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and when it’s opened.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;details[open]{
  background-color: white;
  transition: all .8s ease;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we used the attribute open to style the element when it's opened.&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%2Fhbqn7gw6883wvyyhzq9k.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%2Fhbqn7gw6883wvyyhzq9k.png" alt="Create a FAQ accordion using details and summary elements"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;br&gt;
Hi, my name is Amine, I am from Morroco and I AM HORRIBLE AT UI/UX. &lt;/p&gt;

&lt;p&gt;Notice the arrow? That comes as default style and it makes the element much ugly than it is. Let's change it. &lt;br&gt;
We can use an icon of an arrow :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;details &amp;gt; summary {
    list-style-image: url('data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20width%3D%221em%22%20height%3D%221em%22%20preserveAspectRatio%3D%22xMidYMid%20meet%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M19%2012l-7-6v5H6v2h6v5z%22%20fill%3D%22currentColor%22%2F%3E%3C%2Fsvg%3E');
  border-bottom: black 1px solid;
  margin-bottom: 10px;
  padding-bottom: 10px;
}

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

&lt;/div&gt;



&lt;p&gt;You can cut it off by using: &lt;code&gt;list-style: none;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's it. We have a simple FAQ accordion. &lt;br&gt;
Also, I want to tell you that you can use the open attribute in the HTML to keep it open all the time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;details open&amp;gt;
      &amp;lt;summary&amp;gt; When to use it?&amp;lt;/summary&amp;gt;
      &amp;lt;p&amp;gt;
        We use details tag when we want to make an accordion element.
      &amp;lt;/p&amp;gt;
    &amp;lt;/details&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the detail element to create a dropdown menu. I tried it once and suggest you use a CSS preprocessor such as Sass. &lt;/p&gt;

&lt;p&gt;And here is the &lt;a href="https://codepen.io/aminequ/pen/eYWQqOy" rel="noopener noreferrer"&gt;Codepen demo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, exploring HTML tags and various attribute is one of the best things that you can do as a beginner. &lt;/p&gt;

&lt;p&gt;I won't say do it when you have time but whenever you get the imposter syndrome telling you that you can't write HTML/CSS. &lt;br&gt;
It happened to me and that's why I am sharing. &lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/AmiineQu" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and contact me via &lt;a href="https://www.linkedin.com/in/amine-amhoume-023517109/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to create a contact form in React.js for beginners"</title>
      <dc:creator>Amine Amhoume</dc:creator>
      <pubDate>Wed, 15 Sep 2021 10:40:13 +0000</pubDate>
      <link>https://dev.to/amiinequ/how-to-create-a-contact-form-in-react-js-for-beginners-3571</link>
      <guid>https://dev.to/amiinequ/how-to-create-a-contact-form-in-react-js-for-beginners-3571</guid>
      <description>&lt;p&gt;A contact form is one of the most essential parts of any website… blah blah blah.🗣&lt;br&gt;
 &lt;br&gt;
You already know this.&lt;br&gt;
 &lt;br&gt;
You are here because you know how important forms are. And you want to create one I guess.&lt;/p&gt;

&lt;p&gt;Welcome, today we will learn how to create a form using Reactjs.&lt;/p&gt;

&lt;p&gt;Here's what we will be building: &lt;a href="https://tutorial-react-form.netlify.app/"&gt;React-form&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It's simple, has no fancy UI, and is somehow useless.&lt;/p&gt;

&lt;p&gt;GitHub repo: &lt;a href="https://github.com/aminequ/react-form"&gt;github.com/aminequ/react-form&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Basic knowledge of React stateless components.&lt;/li&gt;
&lt;li&gt;Basic knowledge of CSS/Sass.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it. Let's begin.&lt;/p&gt;
&lt;h1&gt;
  
  
  Setting up the environment.
&lt;/h1&gt;

&lt;p&gt;Create a react project using either npm or yarn. Both work well.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app form&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Nope, I didn't trick you! npx is the same as npm. &lt;/p&gt;

&lt;p&gt;We will be using Sass as a preprocessor. But I promise I won't bother you with any styling.&lt;/p&gt;

&lt;p&gt;Install &lt;code&gt;node-sass&lt;/code&gt; like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install node-sass&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open the project with your beloved code editor then delete the default unnecessary files. If you have never done that, here's how, otherwise you can skip this part.&lt;/p&gt;

&lt;p&gt;Delete these:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;App.test.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;reportWebVitals.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;setupTests.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remove the default code and styles from &lt;code&gt;App.js/App.css/index.js/index.css&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;App.js&lt;/code&gt; should only contain the App function that returns an empty &lt;code&gt;div&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now create a new folder inside the &lt;code&gt;src&lt;/code&gt; folder and give it the name of "components", then create a new component and name it &lt;code&gt;Form.js&lt;/code&gt;. Also, create a stylesheet and name it "formStyle.scss".&lt;/p&gt;

&lt;p&gt;Start the local server: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Adding style.
&lt;/h1&gt;

&lt;p&gt;I said it before, it's nothing fancy but if you want me to explain all the styling, then please, feel free to leave a comment. For now, just copy the styling from the GitHub repo and place it where it should be placed ( formStyle.scss ) 😎&lt;/p&gt;
&lt;h1&gt;
  
  
  Let's code the form.
&lt;/h1&gt;

&lt;p&gt;Following written coding tutorials can be tricky sometimes but I will help you, so code this inside the &lt;code&gt;Form.js&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import './formStyle.scss'

function Form() {
   return(
      &amp;lt;div className="container"&amp;gt;
   &amp;lt;/div&amp;gt;
 )
}
export default Form

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

&lt;/div&gt;



&lt;p&gt;We imported react, the styling, and created a stateless function with the name of "Form" . The function returns a &lt;/p&gt; with a class name of "container".

&lt;p&gt;Inside the container, go on and:&lt;/p&gt;

&lt;p&gt;Add an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; with "Contact us".&lt;/p&gt;

&lt;p&gt;Create a form element and give it the class name of "form".&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;div&lt;/code&gt; with a class name of "name". This will act as a container for first and last name fields.&lt;/p&gt;

&lt;p&gt;Add a &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; tag with an attribute of &lt;code&gt;for="firsrName"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Check out this quote about the  tag:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;…This is the most important element if you want to build accessible forms - when implemented properly, screenreaders will speak a form element's label along with any related instructions, as well as it being useful for sighted users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, always remember to include the "for" attribute.&lt;/p&gt;

&lt;p&gt;Now add an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; element with the following attributes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;type="text"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;name="firstName"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;className="firstName"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;tabIndex="1"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To make our form accessible, we put the "tabIndex" attribute to help the users tab through the form with the order we want and not the default order elements are coded. &lt;/p&gt;

&lt;p&gt;In this case, we gave the first-name number one which means it's going to be the first input the user will tab through.&lt;/p&gt;

&lt;p&gt;Now, repeat and create another  and  elements for last name.Put same attributes, but change the word the "first" with "last" and give &lt;code&gt;thetabeIndex&lt;/code&gt; attribute the value of "2".&lt;/p&gt;

&lt;p&gt;The output code:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="container"&amp;gt;
  &amp;lt;h1&amp;gt;Contact us&amp;lt;/h1&amp;gt;

 &amp;lt;form className="form"&amp;gt;

    &amp;lt;div className="name"&amp;gt;

       &amp;lt;label for="firstName"&amp;gt;First name&amp;lt;/label&amp;gt;
       &amp;lt;input
            type="text"
            name="firstName"
            className="firstName"
            tabIndex="1"
       /&amp;gt;
       &amp;lt;label for="lastName"&amp;gt;Last name&amp;lt;/label&amp;gt;
       &amp;lt;input
            type="text"
            name="lastName"
            className="lastName"
            tabIndex="2"
       /&amp;gt;
    &amp;lt;/div&amp;gt;
 &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Let's create the email input field.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="email"&amp;gt;Email&amp;lt;/label&amp;gt;
&amp;lt;input
     type="email"
     name="email"
     id="email"
     className="email"
     placeholder="example@corp.com"
     tabIndex="3"
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Almost nothing new, we added a &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; with the &lt;code&gt;for&lt;/code&gt; attribute with the value of "email" and a string of "Email". Next, add an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; with almost the same attributes.&lt;/p&gt;

&lt;p&gt;We gave the email input the &lt;code&gt;tabeIndex&lt;/code&gt; of "3" so it's the third filed the user will tab through.&lt;/p&gt;

&lt;p&gt;Let's go for the message input which will not be an  element but a . The difference? It's so simple.&lt;/p&gt;

&lt;p&gt;We use the  when we want to have a one-line text field just like in name and email.  is for multiple line text. Here, a message could be a one-sentence, a paragraph, or a whole cover letter.&lt;/p&gt;

&lt;p&gt;I usually post such semantic tips and other front-end development tricks on my &lt;a href="https://medium.com/r/?url=https%3A%2F%2Ftwitter.com%2FAmiineQu"&gt;Twitter&lt;/a&gt; and &lt;a href="https://medium.com/r/?url=https%3A%2F%2Fwww.instagram.com%2Famin3.js%2F"&gt;Instagram&lt;/a&gt;. I invite you to follow me and fill your feed with useful content.&lt;/p&gt;

&lt;p&gt;Now add a &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; with a for attribute and text of "message". Add a &lt;code&gt;&amp;lt;textarea&amp;gt;&lt;/code&gt; tag with the attributes of: &lt;code&gt;name/className/placeholder&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Output code:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="message"&amp;gt;Message&amp;lt;/label&amp;gt;

&amp;lt;textarea
  placeholder="Start typing..."
  className="message"
  name="message"
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;let's add a submit button which is going to be the cherry on the cack. &lt;/p&gt;

&lt;p&gt;Create a  element with the type of submit and a className of "send":&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button type="submit" className="send"&amp;gt;Send&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Make sure you've put everything in place and check if the form looks as in the live version&lt;/p&gt;

&lt;p&gt;If there's an error, go back and check the output codes or refer to the code in the repo. &lt;/p&gt;

&lt;p&gt;All good?&lt;/p&gt;

&lt;h1&gt;
  
  
  Make it functional with useRef()
&lt;/h1&gt;

&lt;p&gt;Our form will only be functional when we are able to take the data submitted by the user and store it into an object, then do something with it later.&lt;/p&gt;

&lt;p&gt;Let's first understand what exactly is the useRef() hook. In short, hooks let you use React features without writing a class and useRef() is one of these features. &lt;/p&gt;

&lt;p&gt;Okay! And what does this feature of useRef() do?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Refs provide a way to access DOM nodes or React elements created in the render method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;useRef() can be a replacement for useState(). It behaves exactly as useState() except that the latest triggers a re-render while useRef() doesn't. &lt;/p&gt;

&lt;p&gt;useRef returns an object with a property called current and it refers to the current node or element targeted by the hook. You will understand this in a few seconds. &lt;/p&gt;

&lt;p&gt;In order to work with &lt;code&gt;useRef()&lt;/code&gt; hook, first we need to import it just like how we import useState and other hooks. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;import React, { useRef} from 'react'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here's the syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const whereWeStoreInputValue= useRef(initialValue)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's implement it into our form. &lt;/p&gt;

&lt;p&gt;Create the following variables inside the Form component but not inside JSX of course. &lt;/p&gt;

&lt;p&gt;Each input should have its own variable.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const firstNameRef = useRef(null)
const lastNameRef =  useRef(null)
const emailRef = useRef(null)
const messageRef =  useRef(null)
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Notice that we used null as an initial value because we want no value at the beginning. You can use an empty string instead.&lt;/p&gt;

&lt;p&gt;Now we need to go back to the form inputs and add the &lt;code&gt;ref={}&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;in each input in the form, add the ref attribute with corresponded variable. &lt;/p&gt;

&lt;p&gt;For example, The field responsible for Firstname can have the following attribute &lt;code&gt;ref={firstNameRef}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That will connect the useRef variable with the right element. Get it?&lt;/p&gt;

&lt;p&gt;Now let's create a function that will handle the submission of the form. We want to a function that will contain the retrieved data and store it in an object. &lt;/p&gt;

&lt;p&gt;Code this inside the &lt;code&gt;Form&lt;/code&gt; component and right below the four variables we created previously:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleSubmit = (event) =&amp;gt; {
      event.preventDefault()

      const data = {
            firstName: firstNameRef.current.value,
            lastName: lastNameRef.current.value,
            email: emailRef.current.value,
            message: messageRef.current.value
            }
      alert("tadaaa!: \n" + JSON.stringify(data) + "Your data 😎")
}
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The function has the event as an argument. We use "event" with the well-known method "preventDefault" to stop the browser from performing the default behavior of the target element, in our case, it's refreshing the page. &lt;/p&gt;

&lt;p&gt;Next, we created an object with four properties, each property contains the reference to the current value of the targeted element. &lt;/p&gt;

&lt;p&gt;For example, in the code above, we put &lt;code&gt;ref={firstNameRef}&lt;/code&gt; inside the first name input, then we referred to it back in the object data:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;firstName: firstNameRef.current.value&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For the scope of this article, we only alert the data back to the user. 🤷‍♂️ &lt;/p&gt;

&lt;p&gt;Again, check if everything works, if not, go back to where you think missed up.&lt;br&gt;
 &lt;br&gt;
That's for this part. In the next one, we will see how can validate the user inputs before we pass them to the &lt;code&gt;data&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;If you feel like you've learned something new, share this article, someone somewhere is looking for it. They will appreciate you and consider you as their saver. &lt;/p&gt;

&lt;p&gt;See you.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>html</category>
      <category>a11y</category>
    </item>
    <item>
      <title>Javascript Arrays: A guide, a cheat sheet, and for beginners. </title>
      <dc:creator>Amine Amhoume</dc:creator>
      <pubDate>Wed, 09 Dec 2020 14:11:33 +0000</pubDate>
      <link>https://dev.to/amiinequ/javascript-arrays-a-guide-a-cheat-sheet-and-for-beginners-only-pt1-m79</link>
      <guid>https://dev.to/amiinequ/javascript-arrays-a-guide-a-cheat-sheet-and-for-beginners-only-pt1-m79</guid>
      <description>&lt;p&gt;Aright! Hear me out!&lt;br&gt;
 &lt;br&gt;
When you do make it, give back. &lt;/p&gt;

&lt;p&gt;When I first started learning Javascript and came across Arrays, I said to myself "Okay! I know what is an array. I know how to use it in python so it's going to be the same thing". &lt;/p&gt;

&lt;p&gt;After digging deeper into javascript, I realized how stupid I was.&lt;br&gt;
 &lt;br&gt;
Arrays in Javascript are a bit different than what they are in Python. At least this is my opinion on them.&lt;/p&gt;

&lt;p&gt;They are the most used data structure and an important component in any javascript application. So, I decided to write a piece of content about arrays because I believe I can resonate with beginners who want to learn about them.&lt;/p&gt;

&lt;p&gt;This guide will be several parts divided and in this part, we will talk about basic arrays and some simple methods. &lt;/p&gt;

&lt;p&gt;By the end of this article, you will learn about arrays and their methods. You can also bookmark this article and use it whenever you up to work with Arrays. &lt;/p&gt;

&lt;p&gt;You will forget the syntax. We all do. And that's okay&lt;/p&gt;

&lt;p&gt;No further hype.&lt;/p&gt;
&lt;h1&gt;
  
  
  What's an array? 
&lt;/h1&gt;

&lt;p&gt;Simply put, an array is a type of data structure. We use arrays when we want to make an ordered collection of items. These items can be users, food, place, or anything. &lt;/p&gt;

&lt;p&gt;So, again, we have some data and we put it in an ordered structure. Just like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let Users = ["Isaac Netero", "Zeno Zoldyck", "James Bond"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Each item has its own index. An "Index" is the order number of the item.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users[0] is Isaac Netero 
Users[1] is Zeno Zoldyck 
…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Since everything in Javascript is made of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects"&gt;Objects&lt;/a&gt;, Arrays also are objects behind the scene. &lt;/p&gt;

&lt;p&gt;Wait! But we didn't put any numbers in our array. Yes, that how Javascript represents an array's indexes. &lt;/p&gt;

&lt;p&gt;Okay! So what's the difference between Objects and arrays. &lt;br&gt;
Both are different types of data structures. &lt;/p&gt;

&lt;p&gt;In javascript, Arrays are more flexible and convenient to use than Objects because they have so my methods that make working and manipulating data inside arrays flexible. &lt;/p&gt;

&lt;p&gt;Show me. &lt;/p&gt;
&lt;h1&gt;
  
  
  Arrays Methods.
&lt;/h1&gt;

&lt;p&gt;Here we go! &lt;/p&gt;

&lt;p&gt;We have a simple array that represents a bunch of students. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;let students = ["Amine", "Alicia", "John"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pop()&lt;/strong&gt; removes the last item from the array and returns that value. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;let alicia = students.pop(1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;removes "Alicia" from the array and returns it back to the variable &lt;em&gt;alicia&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;push()&lt;/strong&gt; adds items to the end of the array and returns the new length of the array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let newLenght = studnets.push('Alicia')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add &lt;em&gt;Alicia&lt;/em&gt; to the end and store the length (3) in the &lt;em&gt;newLenght&lt;/em&gt; variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;shift()&lt;/strong&gt; removes the first element from an array and returns that removed element.&lt;br&gt;
 &lt;br&gt;
&lt;code&gt;let amine = students.shift(1)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;omit "Amine " and store the returned value in the variable &lt;em&gt;amine&lt;/em&gt;&lt;br&gt;
 &lt;br&gt;
&lt;strong&gt;join()&lt;/strong&gt; gathers all the items of an array in one string.&lt;br&gt;
Items can be separated by commas or anything inside the &lt;strong&gt;()&lt;/strong&gt;.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;students.join(', ') //returns "Amine, Alicia, John"
studnets.join('-') //returns "Amine-Alicia-John"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reverse()&lt;/strong&gt; Yes! you guessed it right. It reverses an array. The first item becomes the last one, and the last item becomes the first in the array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;students.reverse() //returns ["John', "Alicia", "Amine"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What's the flexible way of accessing the last item of an array you don't know its length?&lt;/em&gt;&lt;br&gt;
 &lt;br&gt;
Answer: &lt;code&gt;students.reverse()[0]&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Once you learn more about arrays and their methods, you will able to create such things by yourself.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Continue on...&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;indexOf()&lt;/strong&gt; returns the first index at which a given item can be found in the array, or -1 if it is not present.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;students.indexOf('Amine') //returns 0&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;fill()&lt;/strong&gt; changes all items in an array to a static value, from a start index (default 0) to an end index (default array.length).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let newStatic = students.fill(null, 0, 1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
change items to null from the index of 0 to 1 (one item) where null can be any value.&lt;br&gt;
 &lt;br&gt;
returns &lt;code&gt;[null, "Alicia", "John"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;NICE!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;every()&lt;/strong&gt; tests whether all items in the array pass the test implemented by the provided function. &lt;strong&gt;It returns a Boolean value&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;let's check if all the items in the array are strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let test = (value) =&amp;gt; value = toString(value)
console.log(students.every(test))
//returns true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each item of the array takes the place of 'value'. Humm? Do you get it?&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;In another way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let name of students){
 if (name = toString(name){ 
   return true
   }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;findIndex()&lt;/strong&gt; returns the index of the first item in the array that passes a testing function.&lt;/p&gt;

&lt;p&gt;Let's suppose we have a very very long array of names and one number, and we want to return the index of that number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Users = ["Amine", "Alicia", "John", 22]

let test = (value) =&amp;gt; value = parseInt(value);

console.log(students.findIndex(test))
//returns 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;concat()&lt;/strong&gt; is used to merge two or more arrays. &lt;/p&gt;

&lt;p&gt;let's add another array and concatenated with the first one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let otherClass = ['Zoldyck', 'Chrollo', 'Merieum']
let newClass = students.concat(otherClass)
["Amine", "Alicia", "John", "Zoldyck", "Chrollo", "Merieum"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;slice()&lt;/strong&gt; returns a bunch of items you select using start / end indexes.&lt;br&gt;
 &lt;br&gt;
Let's take the &lt;em&gt;newClass&lt;/em&gt; and return the students of the old class only.&lt;br&gt;
 &lt;br&gt;
&lt;code&gt;newClass.slice(0,3)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[ Amine, Alicia, John]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;forEach()&lt;/strong&gt; executes a provided function once for each array element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;students.forEach(student =&amp;gt; console.log(student)) 

Amine
Alicia
John
Zoldyck
Chrollo
Merieum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An alternative method to do the same is &lt;strong&gt;filter()&lt;/strong&gt;. But this method returns an array.&lt;/p&gt;

&lt;p&gt;Enough for today. Practice, practice practice is the only advice I can give you and the best thing you can do for now.&lt;/p&gt;

&lt;p&gt;Ready for part two? &lt;/p&gt;

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