<?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: Dennis San Jose</title>
    <description>The latest articles on DEV Community by Dennis San Jose (@sanjosedennis).</description>
    <link>https://dev.to/sanjosedennis</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%2F664615%2Ff70b11b9-5a5c-487c-a2bd-972865d8982b.jpg</url>
      <title>DEV Community: Dennis San Jose</title>
      <link>https://dev.to/sanjosedennis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanjosedennis"/>
    <language>en</language>
    <item>
      <title>React: Introduction to useReducer hook</title>
      <dc:creator>Dennis San Jose</dc:creator>
      <pubDate>Wed, 13 Jul 2022 07:00:25 +0000</pubDate>
      <link>https://dev.to/sanjosedennis/react-introduction-to-usereducer-hook-5461</link>
      <guid>https://dev.to/sanjosedennis/react-introduction-to-usereducer-hook-5461</guid>
      <description>&lt;p&gt;&lt;em&gt;In this article, I'm assuming that you have at least some knowledge of using React and want to explore React Hooks.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useReducer
&lt;/h2&gt;

&lt;p&gt;This hook is used to handle complex state in our application. Inspired by redux state management pattern (If you're not familiar with redux you can check it &lt;a href="https://redux.js.org/introduction/getting-started" rel="noopener noreferrer"&gt;here&lt;/a&gt;). It is used to store and update states, just like the useState hook. It accepts reducer function as its first parameter and the initial state as the second.&lt;/p&gt;

&lt;p&gt;It returns an array that holds the current value and a dispatch function in which you can pass an action to the reducer function.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [state, dispatch] = useReducer(reducerFn, initialState)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;state&lt;/strong&gt; - value of the current state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dispatch&lt;/strong&gt; - is the method that will be used to call the reducerFn. It accepts an object that represents the type of action. Send the type of action to the reducer function and update the state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reducerFn&lt;/strong&gt; - a function that returns some state data, triggered by action type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initialState&lt;/strong&gt; - initial value of state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understand by Example
&lt;/h3&gt;

&lt;p&gt;In the following example, we will create a simple app that will increment and decrement counter by pressing the button. We will be using the useReducer for this.&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 = {
  counter: 0
};

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return { ...state, counter: state.counter + 1 };
    case 'decrement':
        return { ...state, counter: state.counter - 1 };
    default:
      throw new Error();
  }
}

function MyComponent() {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
    &amp;lt;div style={{ margin: 12 }}&amp;gt;
      &amp;lt;div&amp;gt; {state.counter}&amp;lt;/div&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; dispatch({ type: 'increment' })}&amp;gt;
        Increment
      &amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; dispatch({ type: 'decrement' })}&amp;gt;
        Decrement
      &amp;lt;/button&amp;gt;

    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Here we import the &lt;strong&gt;useReducer&lt;/strong&gt; hook at the top of the component. We define the &lt;strong&gt;initialState&lt;/strong&gt; and &lt;strong&gt;reducer&lt;/strong&gt; function outside our component since all the values will be passed using the dispatch method.&lt;/p&gt;

&lt;p&gt;The reducer function takes two arguments: &lt;strong&gt;state&lt;/strong&gt; and &lt;strong&gt;action&lt;/strong&gt;. &lt;strong&gt;state&lt;/strong&gt; holds the value of current state, on initial render it's value is the &lt;strong&gt;initialState&lt;/strong&gt; object, and &lt;strong&gt;action&lt;/strong&gt; is used to check what action which condition to trigger. &lt;strong&gt;type&lt;/strong&gt; is the property we’re passing from the dispatch action to determine which condition to trigger inside the reducer function.&lt;/p&gt;

&lt;p&gt;The component will be re-rendered every time counter state value changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use useReducer?
&lt;/h3&gt;

&lt;p&gt;In the above code example, you can see that useReducer implementation is much longer than useState hook. It is prefer to use useReducer if the state of component has a complex structure, think of object with many properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is useReducer a replacement for Redux?
&lt;/h3&gt;

&lt;p&gt;The answer is &lt;strong&gt;No&lt;/strong&gt;. It is not intended to replace redux, instead it should be used in components that have complex state logic on it. Some might tend to think that useReducer feature is intended to replace the redux. In fact, useReducer is restricted to component only.&lt;/p&gt;

&lt;p&gt;And that's all folks. Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you have any question or feedback. Feel free to comment below. &lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React: Introduction to useEffect hook</title>
      <dc:creator>Dennis San Jose</dc:creator>
      <pubDate>Tue, 05 Jul 2022 14:33:21 +0000</pubDate>
      <link>https://dev.to/sanjosedennis/react-introduction-to-useeffect-hook-4d1m</link>
      <guid>https://dev.to/sanjosedennis/react-introduction-to-useeffect-hook-4d1m</guid>
      <description>&lt;p&gt;In this article, I'm assuming that you have at least some knowledge of using React and want to explore React Hooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;useEffect&lt;/strong&gt; is one of the known hooks in React application.  Some call it the &lt;strong&gt;“Effect Hook”&lt;/strong&gt;. This hook allows you to perform side effects in functional components. It handles &lt;strong&gt;componentDidMount&lt;/strong&gt;, &lt;strong&gt;componentDidUpdate&lt;/strong&gt; and &lt;strong&gt;componentWillUnmount&lt;/strong&gt; all in one call.&lt;/p&gt;

&lt;p&gt;In case you are not familiar with Side Effect. It is any execution that is outside the scope of the function being executed. We will not cover much about side effect in this article.&lt;/p&gt;

&lt;p&gt;Examples of side effects are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetching data&lt;/li&gt;
&lt;li&gt;Manual DOM Manipulation&lt;/li&gt;
&lt;li&gt;Setting up a subscription&lt;/li&gt;
&lt;li&gt;Updating global variables&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Question: When useEffect runs?
&lt;/h2&gt;

&lt;p&gt;By default it usually run after very render. But there are ways to control it. Let's look at how to run code after the component mounts (&lt;strong&gt;componentDidMount&lt;/strong&gt;), after it re-renders (&lt;strong&gt;componentDidUpdate&lt;/strong&gt;) , and before it unmount (&lt;strong&gt;componentWillUnmount&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;To run it only once after the component has been mounted, you can do it 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;// Make sure to pass an empty array on the second argument
useEffect(() =&amp;gt; {
  console.log(‘Component Mounted!’); // This will trigger only once.
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run it every time a props/state variable changes, you can add an array of variables to the second argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Pass a variable to the second argument, and every time that variable changes this useEffect will run.
useEffect(() =&amp;gt; {
  console.log(‘Counter’, counter); // This will trigger every time counter state changes.
}, [counter]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to do some cleanup when the component unmount, you can do it 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;useEffect(() =&amp;gt; {
    // Run on Component Mount
    const handleAuthorSubscription = author =&amp;gt; {
      setAuthor(author);
    }
    API.subscribeToAuthor(userId, handleAuthorSubscriptionChange);

    // Run on Component Unmount
    return () =&amp;gt; {
      API.unSubscribeToAuthor(userId, handleAuthorSubscriptionChange);
    };
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You just need to add a return function to a useEffect callback function, code inside that returned function will get triggered when a component unmount from a DOM. We use this function to do a cleanup just like removing eventListeners, clearInterval, clearTimeout, etc. &lt;/p&gt;

&lt;h2&gt;
  
  
  Things to remember in useEffect
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First argument is a callback function. Inside of it, we can perform any side effect there and set some data in state variables.&lt;/li&gt;
&lt;li&gt;Second Argument: Array contains data, useEffect will get triggered every time any data of that array changes. Passing an empty array will trigger the useEffect after the component has been mounted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that is all I wanted to share with you. This is my very first article. Hopefully you find this article helpful in learning some basics of React Hooks. If you have any question or feedback for improvement, feel free to comment you're all welcome.&lt;/p&gt;

&lt;p&gt;Thanks for Reading! :) &lt;/p&gt;

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