<?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: Mary Sule</title>
    <description>The latest articles on DEV Community by Mary Sule (@marysule38).</description>
    <link>https://dev.to/marysule38</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%2F897633%2Ff8160dc9-d3b0-4626-8fc2-f9076ff617a1.jpeg</url>
      <title>DEV Community: Mary Sule</title>
      <link>https://dev.to/marysule38</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marysule38"/>
    <language>en</language>
    <item>
      <title>Setting up a React Counter Custom Hook with a combination of State.</title>
      <dc:creator>Mary Sule</dc:creator>
      <pubDate>Sat, 07 Jan 2023 08:13:19 +0000</pubDate>
      <link>https://dev.to/marysule38/setting-up-a-react-counter-custom-hook-with-a-combination-of-state-317o</link>
      <guid>https://dev.to/marysule38/setting-up-a-react-counter-custom-hook-with-a-combination-of-state-317o</guid>
      <description>&lt;p&gt;This is something some persons might overlook thinking it's easier because it has to do with a counter. But yeah, they might be right.&lt;br&gt;
Let me share a bit of my journey..&lt;br&gt;
 I applied for Altschool of engineering  frontend track late 2021 and got in by 2022. It has been an amazing journey thus far with a lot of experiences and learning. In second semester I was taught React  which is the most popular frontend framework. After 2 months of in-depth learning, the semester was wrapped up with an examination to test our knowledge and understanding of what we have been taught in React. It was a total of four questions to answer one. I was skeptical on which to do but then again, I started having issues with my laptop and was almost given up but then I had t remind myself of how I really needed this..&lt;br&gt;
Okay, enough of my story...would share more in my next article but for now, let's stick to the topic.&lt;/p&gt;

&lt;p&gt;React Custom hook is an important aspect of react. It's actually one of the uniqueness of react. which is reusability. Rather than rewriting same code on multiple components with common stateful logic, It is easier to put the code in a custom hook and reuse it. It is used to add special and unique functionalities to react applications.&lt;br&gt;
&lt;a href="https://media2.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%2Feyqppmo2cyjyq36fjt5i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Feyqppmo2cyjyq36fjt5i.png" alt="Image description" width="800" height="488"&gt;&lt;/a&gt;&lt;br&gt;
The instructions given for the exam project was to implement the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a custom counter with increment, decrement, reset, &lt;br&gt;
setValue functions with a valid UI component&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implementation of combination of State with useReducer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A 404 page&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A page for Error Boundary&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test for a good SEO&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First I installed my react app using&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install vite&lt;/code&gt; following the documentation from &lt;code&gt;https://vitejs.dev/guide/&lt;/code&gt;. After installation, I went ahead and created ny react components based on the requirement.&lt;br&gt;
&lt;/p&gt;

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

export default function useCounter(initialValue = 0) {
  const [count, setCount] = useState(initialValue);
  function increase() {
    setCount((prevCount) =&amp;gt; prevCount + 1);
  }
  function decrease() {
    setCount((prevCount) =&amp;gt; prevCount - 1);
  }
  function reset() {
    setCount(0);
  }
  function setValue(e) {
    let num = Number(e.target.value);

    setCount(num);
  }
  return { count, increase, decrease, reset, setValue };
}


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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; is used in functional components to declare state variables as &lt;code&gt;this.state&lt;/code&gt; is used in class component. setState is a way of preserving the state variable. &lt;br&gt;
After declaring the state variable we initialized our variable which is the &lt;code&gt;count&lt;/code&gt;. It could be any name depending on what you are working on. A name similar to your project should be considered for better readability. A set value of 0 was initialized which will be the default counter number. &lt;br&gt;
The custom hook I created &lt;code&gt;useCounter&lt;/code&gt; was then imported into my Counter Component which contains the increment, decrement and reset buttons.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;UseReducer&lt;/strong&gt;
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  The useReducer is very similar to useState. It is also a react hook. It can only be called at the top of a component. It returns a dispatch function that allows to update the state to a different value and trigger a re-render.
&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;import { useReducer } from "react";

function ReducerCounter() {


  function setValue(value) {
    let num = Number(value);

    return num;
  }
  function reducer(count, action) {
    switch (action.type) {
      case "set_value":
        return setValue(action.payload);
      case "increase":
        return ++count;
      case "decrease":
        return --count;
      case "reset":
        return 0;
      default:
        return count;
    }
  }
  function inputHandler(e) {
    dispatch({
      type: "set_value",
      payload: e.target.value,
    })
  }
  const [count, dispatch] = useReducer(reducer, 0);

  return (
    &amp;lt;div className="counter"&amp;gt;
      &amp;lt;h1&amp;gt;UseReducer Counter&amp;lt;/h1&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;input
          type="text"
          placeholder="set counter value"
          onChange={inputHandler}
        /&amp;gt;
        &amp;lt;h2 className="count-num"&amp;gt;Count : {count}&amp;lt;/h2&amp;gt;
        &amp;lt;div className="buttons-wrapper"&amp;gt;
          &amp;lt;button
            onClick={() =&amp;gt; {
              dispatch({ type: "increase" });
            }}
          &amp;gt;
            +
          &amp;lt;/button&amp;gt;
          &amp;lt;button
            onClick={() =&amp;gt; {
              dispatch({ type: "reset" });
            }}
          &amp;gt;
            reset
          &amp;lt;/button&amp;gt;
          &amp;lt;button
            onClick={() =&amp;gt; {
              dispatch({ type: "decrease" });
            }}
          &amp;gt;
            -
          &amp;lt;/button&amp;gt;

        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
export default ReducerCounter;

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

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Differences between useState and useReducer
&lt;/h2&gt;

&lt;p&gt;useState is a basic Hook that manages simple state transformation while the useReducer is an additional Hook for managing more complex state logic.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Error Boundary&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This captures and handles javascript errors anywhere in it's child tree. I implemented it following the documentation from React documentation.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;404 Page&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I created a 404 error page that cannot be found.&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'

export default function NotFound() {
  return (
    &amp;lt;div className='notfound'&amp;gt;
      &amp;lt;h1&amp;gt;404&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;Error Page&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;SEO in React&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I implemented SEO easily following what I was taught, some documentations '&lt;a href="https://ahrefs.com/blog/react-seo/" rel="noopener noreferrer"&gt;https://ahrefs.com/blog/react-seo/&lt;/a&gt;', and youtube videos.&lt;/p&gt;

&lt;p&gt;My work was easily hosted on a frontend hosting platform called 'vercel' and then pushed to github for proper documentation.&lt;/p&gt;

&lt;p&gt;This custom hook might be easy but it is a good way of learning about hooks, state and useReducer mostly for beginners. It is easier understand.&lt;br&gt;
I enjoyed working on this project and I know you would too.&lt;/p&gt;

&lt;p&gt;Reach out for questions or any contributions.&lt;br&gt;
Thanks to Altschool for the opportunity to  learn and share my knowledge with others.&lt;/p&gt;

&lt;p&gt;I hope this was helpful.&lt;/p&gt;

</description>
      <category>career</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
