<?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>Forem: Juliana Hassan</title>
    <description>The latest articles on Forem by Juliana Hassan (@julsgirl).</description>
    <link>https://forem.com/julsgirl</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%2F1019435%2F985d8683-8324-499b-9892-129abc534b99.jpeg</url>
      <title>Forem: Juliana Hassan</title>
      <link>https://forem.com/julsgirl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/julsgirl"/>
    <language>en</language>
    <item>
      <title>How i built a calculator using React js useReducer</title>
      <dc:creator>Juliana Hassan</dc:creator>
      <pubDate>Sun, 05 Feb 2023 06:37:21 +0000</pubDate>
      <link>https://forem.com/julsgirl/how-i-built-a-calculator-using-react-js-usereducer-480c</link>
      <guid>https://forem.com/julsgirl/how-i-built-a-calculator-using-react-js-usereducer-480c</guid>
      <description>&lt;p&gt;React js is a javascript library for building awesome ui.&lt;br&gt;
In this blog post, i would be sharing how i was able to build a simple calculator using the useReducer Hook, useState hook.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up the Application.
&lt;/h2&gt;

&lt;p&gt;To bootstrap the application we use create react app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-calculator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second thing to do is to remove the boilerplate code and install the necessary dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-router-dom helmet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;i wanted my app to have a good seo ranking so i install helmet.&lt;br&gt;
i also wanted to be able to have different routes hence the need for react router dom.&lt;br&gt;
i set up error boundary on my code to ensure that even if my app had an error it would not entirely break.&lt;br&gt;
any route that is not accounted for would fall into the catch all route or the error page.&lt;br&gt;
here is a code snippet of how my  main.jsx looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Counter from "./Counter";
import { Route, Routes } from "react-router-dom";
import Error from "./Error";
import { Helmet } from "react-helmet";
import ErrorBoundary from "./ErrorBoundary";

function App() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Helmet&amp;gt;
        &amp;lt;title&amp;gt;reduce counter&amp;lt;/title&amp;gt;
        &amp;lt;meta name="description" content="reducer counter" /&amp;gt;
      &amp;lt;/Helmet&amp;gt;
      &amp;lt;ErrorBoundary&amp;gt;
        &amp;lt;Routes&amp;gt;
          &amp;lt;Route path="/" element={&amp;lt;Counter /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="*" element={&amp;lt;Error /&amp;gt;} /&amp;gt;
        &amp;lt;/Routes&amp;gt;
      &amp;lt;/ErrorBoundary&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;
&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, useState } from "react";
import { reducer, initialState } from "./countReducer";
import React from "react";
import {
  INC_COUNT,
  DEC_COUNT,
  RESET_COUNT,
  ADD_VALUE,
  REDUCE_VALUE,
} from "./types";
import "./counter.css";

const Counter = () =&amp;gt; {
  const [state, dispatch] = useReducer(reducer, initialState);
  const [input, setInput] = useState(+0);
  const data = (e) =&amp;gt; {
    setInput(+e.target.value);
  };
  if (input &amp;gt;= 10000) {
    throw new Error("value is too big");
  }
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;div className="container"&amp;gt;
        &amp;lt;div className="count-bar"&amp;gt;
          &amp;lt;label htmlFor="" className="count-bar-lebel"&amp;gt;
            total
          &amp;lt;/label&amp;gt;
          &amp;lt;p&amp;gt;{state.number}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="count-text"&amp;gt;
          &amp;lt;label htmlFor="" className="input-label"&amp;gt;
            add any number
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            type="number"
            onChange={data}
            className="count-input"
            placeholder="enter any number"
            value={input}
          /&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;div className="values"&amp;gt;
          &amp;lt;button
            onClick={() =&amp;gt; {
              dispatch({ type: ADD_VALUE, payload: input });
              setInput(0);
            }}
          &amp;gt;
            add any number
          &amp;lt;/button&amp;gt;
          &amp;lt;button
            onClick={() =&amp;gt; {
              dispatch({ type: REDUCE_VALUE, payload: input });
              setInput(0);
            }}
          &amp;gt;
            remove any number
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="values"&amp;gt;
          &amp;lt;button onClick={() =&amp;gt; dispatch({ type: INC_COUNT })}&amp;gt;
            add (+1){" "}
          &amp;lt;/button&amp;gt;
          &amp;lt;button onClick={() =&amp;gt; dispatch({ type: DEC_COUNT })}&amp;gt;
            reduce (-1)
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="reset"&amp;gt;
          &amp;lt;button onClick={() =&amp;gt; dispatch({ type: RESET_COUNT })}&amp;gt;
            reset (0){" "}
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;p className="warning"&amp;gt;numbers greater than 10000 throws an error&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing i did when creating this component was to import both useState and useReducer from react,&lt;br&gt;
then imported initialState and the reducer function from countReducer.&lt;br&gt;
useRreducer takes in both the reducer function and the initialState.&lt;br&gt;
state and dispatch are destructured from the useReducer hook.&lt;br&gt;
I created my action types from a seperate file to aviod error.&lt;br&gt;
The app has different action types namely  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; INC_COUNT,&lt;/li&gt;
&lt;li&gt;  DEC_COUNT,&lt;/li&gt;
&lt;li&gt;  RESET_COUNT,&lt;/li&gt;
&lt;li&gt;  ADD_VALUE,&lt;/li&gt;
&lt;li&gt;  REDUCE_VALUE,&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;INC_COUNT is responsible for adding one to the state value.&lt;br&gt;
DEC_COUNT is responsible for removing one from the state value.&lt;br&gt;
RESET_COUNT is responsible for resetting the count value to zero.&lt;/p&gt;

&lt;p&gt;ADD_VALUE adds any desired value to the state.&lt;/p&gt;

&lt;p&gt;REDUCE_VALUE removes any amount from the state&lt;/p&gt;
&lt;h2&gt;
  
  
  How the logic works
&lt;/h2&gt;

&lt;p&gt;whenever any of the buttons are clicked, there is an onClick handler that contains the dispatch function that is triggered&lt;/p&gt;

&lt;p&gt;this is how the reducer function looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  INC_COUNT,
  DEC_COUNT,
  RESET_COUNT,
  ADD_VALUE,
  REDUCE_VALUE,
} from "./types";

export const initialState = {
  number: 0,
};

export const reducer = (state, action) =&amp;gt; {
  switch (action.type) {
    case INC_COUNT:
      return {
        ...state,
        number: state.number + 1,
      };

    case DEC_COUNT:
      return {
        ...state,
        number: state.number - 1,
      };
    case RESET_COUNT:
      return {
        number: 0,
      };
    case ADD_VALUE:
      return {
        ...state,
        number: state.number + action.payload,
      };
    case REDUCE_VALUE:
      return {
        ...state,
        number: state.number - action.payload,
      };
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>career</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
