<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://blog.kirimanjaro.com</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Apr 2026 12:43:53 GMT</lastBuildDate><atom:link href="https://blog.kirimanjaro.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[useReducer in React]]></title><description><![CDATA[State Management
In REACT state management is a vital part of the general architecture of a REACT app. The first and most powerful state management hook is the useState() hook. This is a very powerful hook and is ideal for tracking a state that does ...]]></description><link>https://blog.kirimanjaro.com/usereducer-in-react</link><guid isPermaLink="true">https://blog.kirimanjaro.com/usereducer-in-react</guid><category><![CDATA[technology]]></category><category><![CDATA[React]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Gideon Kirimanjaro]]></dc:creator><pubDate>Fri, 10 Feb 2023 06:56:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/vpOeXr5wmR4/upload/b98c43caf9b07b5770a8831d5ce4efa8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-state-management">State Management</h3>
<p>In REACT state management is a vital part of the general architecture of a REACT app. The first and most powerful state management hook is the useState() hook. This is a very powerful hook and is ideal for tracking a state that does not have complex logic. However, when there is an application with a state that has more complex logic then the useReducer() hook is ideal for this kind of scenario.</p>
<h3 id="heading-syntax">Syntax</h3>
<pre><code class="lang-plaintext">const [state, dispatch] = useReducer(reducer, initialState)
</code></pre>
<p>The <strong><em><mark>reducer</mark></em></strong> is the user-defined reducer function while the <strong><em><mark>initialState</mark></em></strong> is the pre-defined initial state of the application.</p>
<h3 id="heading-use-case">Use Case</h3>
<p>Let’s start with a simple example of a log-in form that has the email and password. We are supposed to track the state using a reducer function. How can we go about this?</p>
<h4 id="heading-step-1-define-the-initial-state-and-the-reducer-function">Step 1: Define the initial state and the reducer function</h4>
<h5 id="heading-initial-state">Initial state</h5>
<pre><code class="lang-plaintext">const [state, dispatch] = useReducer(reducer, {email: "",passWord: ""});
</code></pre>
<h5 id="heading-reducer-function">Reducer Function</h5>
<table><tbody><tr><td><p>1</p></td><td><p><code>const reducer = (state, action) =&gt; {</code></p></td></tr></tbody></table>

<h5 id="heading-explanation">Explanation</h5>
<p>The reducer function is simply a switch statement that returns something if the test case is true. Here the test case is the type of a predefined action which is <strong><em><mark>‘handleMail’</mark></em></strong> and <strong><em><mark>‘handlePassword’</mark></em></strong>. The <strong><mark>…state </mark></strong> tracks the previous state and the email and password update the state with a payload (<strong><em><mark>action.payload</mark></em></strong>). The payload is the new state that the user inputs. This ensures that state updates are always tracked.</p>
<h4 id="heading-step-2-define-the-form-functions">Step 2: Define the form functions</h4>
<table><tbody><tr><td><p>1</p></td><td><p><code>const emailHandler = (e) =&gt; {</code></p></td></tr></tbody></table>

<h5 id="heading-explanation-1">Explanation</h5>
<p>The form functions that handle the email and the password have a dispatch that has an object with the action type and a payload. The type is used to access the action which is used inside the reducer function accessed as ‘<strong><em><mark>action.type</mark></em></strong>‘. The payload stores the form input which is later accessed in the reducer function as ‘<strong><em><mark>action.payload‘</mark></em></strong>.</p>
<h4 id="heading-step-3create-the-form">Step 3:Create the form</h4>
<table><tbody><tr><td><p>1</p></td><td><p><code>&lt;form&gt;</code></p></td></tr></tbody></table>

<h5 id="heading-explanation-2">Explanation</h5>
<p>The form is a simple HTML form. The ‘value’ is the initial state defined in step 1 above. The state changes are tracked through the ‘onChange’ event listener.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>The useReducer() hook is a simple and convenient way to update state and share data between our components in an organized way.</p>
<p>Updating the state only requires calling the dispatch(action) with the required action. The reducer() will update the forwarded state. Once the reducer updates the state it will re-render the component with the new state value.</p>
<p>A word of advice is that you should use the useState() hook for simple state management.</p>
<p>Happy coding</p>
<p><img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f642.svg" alt="🙂" /></p>
]]></content:encoded></item><item><title><![CDATA[useEffect in React]]></title><description><![CDATA[The useEffect() hook is part of functional programming and the word ‘effect’ is used to describe the “side effect” of a REACT app.
Common side effects include:

API requests.

Using unpredictable timing functions. e.g setTimeOut or setInterval.

Dire...]]></description><link>https://blog.kirimanjaro.com/useeffect-in-react</link><guid isPermaLink="true">https://blog.kirimanjaro.com/useeffect-in-react</guid><category><![CDATA[technology]]></category><category><![CDATA[React]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Gideon Kirimanjaro]]></dc:creator><pubDate>Fri, 10 Feb 2023 06:47:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/cckf4TsHAuw/upload/1a9508ef4ff636bf0ee54a69f733be42.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The useEffect() hook is part of functional programming and the word ‘effect’ is used to describe the “side effect” of a REACT app.</p>
<p>Common side effects include:</p>
<ul>
<li><p><strong>API requests.</strong></p>
</li>
<li><p><strong>Using unpredictable timing functions. e.g setTimeOut or setInterval.</strong></p>
</li>
<li><p><strong>Directly updating the DOM. e.g using Document/ Window.</strong></p>
</li>
</ul>
<h3 id="heading-structure">Structure</h3>
<p>The useEffect() hook accepts two arguments. The <strong><em><mark>dependency</mark></em></strong> normally refers to the re-render array. This array includes <strong><mark>all the dependencies that the side effect relies on</mark></strong><mark>.</mark></p>
<p><strong><em>useEffect(&lt;function&gt;, &lt;dependency&gt;</em></strong></p>
<h3 id="heading-use-case-1-run-on-first-render"><strong>Use Case 1 (Run on first render)</strong></h3>
<p>Suppose that we want to fetch data from an API. This usually happens when the app is initially loaded. So to make sure that the API is called only on the first render we write:</p>
<p><strong>useEffect(() =&gt; {<br />//Runs only on the first render<br />}, []);</strong></p>
<h4 id="heading-example">Example</h4>
<pre><code class="lang-plaintext">  const [todos, setTodos] = useState([]);
  useEffect(() =&gt; {fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response =&gt; response.json())
  .then(json =&gt;  setTodos(json))

  },[])
</code></pre>
<h3 id="heading-use-case-2-re-render-on-dependency-value-change"><strong>Use case 2: Re-render On Dependency Value Change</strong></h3>
<p>We have seen from the example above that when we want the effect to run on the first render we include an empty array. But in some instances, we may want the effect to re-render based on certain dynamic values. This is when we include dynamic values inside the dependency array.</p>
<p><strong><em>useEffect(() =&gt; {<br />//Runs on the first render<br />//And any time any dependency value changes<br />}, [Dynamic state]);</em></strong></p>
<h3 id="heading-example-1">Example</h3>
<pre><code class="lang-plaintext">export default function App() {
  const [number, setNumber] = useState(0);
  const [render, setRender] = useState(0)
  useEffect(()=&gt;{
    setRender(number +1)
  },[number])

  const addTwo = () =&gt; {
    setNumber(number+1);
  };

  return (
    &lt;div className="App"&gt;
      &lt;button onClick={addTwo}&gt;+&lt;/button&gt;
      &lt;h3&gt;{number}&lt;/h3&gt;
      &lt;h4&gt;number of renders {render} &lt;/h4&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>The example above shows how re-rendering is done using the dynamic value ‘number’. Every time the ‘number’ changes the re-render value ‘render’ changes since the side effect re-renders based on the ‘number’ value.</p>
<h3 id="heading-effect-clean-up"><strong>Effect Clean up</strong></h3>
<p>The side effects are not supposed to run forever. At some point, <strong><mark> the effect must be shut off in order to reduce memory use</mark></strong>. All that is needed is a return function that clears a pre-defined time-out.</p>
<pre><code class="lang-plaintext">export default function App() {
  const [number, setNumber] = useState(0);
  const [render, setRender] = useState(0)
  useEffect(()=&gt;{
    let timer = setTimeout(setRender(number +1),1000)
    return ()=&gt;{
      return  setTimeout(timer)
    }

  },[number])

  const addTwo = () =&gt; {
    setNumber(number+1);
  };
  return (
    &lt;div className="App"&gt;
      &lt;button onClick={addTwo}&gt;+&lt;/button&gt;
      &lt;h3&gt;{number}&lt;/h3&gt;
      &lt;h4&gt;number of renders {render} &lt;/h4&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>The cleanup function will be called when the component is unmounted. The cleanup function is not required in every instance and it is only used when there is a need to stop a repeated side effect when the component unmounts.</p>
<p>Make sure to follow my blog for more REACT tutorials.</p>
<p>Happy coding!</p>
]]></content:encoded></item></channel></rss>