<?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: guillermodv</title>
    <description>The latest articles on DEV Community by guillermodv (@guillermodv).</description>
    <link>https://dev.to/guillermodv</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%2F481395%2F7c4110d0-37ed-4522-9032-bf6a449e66cc.png</url>
      <title>DEV Community: guillermodv</title>
      <link>https://dev.to/guillermodv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guillermodv"/>
    <language>en</language>
    <item>
      <title>Should I still use connect() function? #React.js</title>
      <dc:creator>guillermodv</dc:creator>
      <pubDate>Mon, 12 Oct 2020 00:06:50 +0000</pubDate>
      <link>https://dev.to/guillermodv/should-i-steal-using-connect-function-2cna</link>
      <guid>https://dev.to/guillermodv/should-i-steal-using-connect-function-2cna</guid>
      <description>&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%2Fi%2Fw077fs4s3rrr1laueo8x.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%2Fi%2Fw077fs4s3rrr1laueo8x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;With a small read, Help me to decide that…&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Let´s review it just a little.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;I will start writing just a few lines of code.&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;React&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s"&gt;"react"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="nn"&gt;addTodo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;deleteTodo&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="o"&gt;./&lt;/span&gt;&lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;TodoApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addTodo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;deleteTodo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;todos&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;
&lt;span class="c1"&gt;//A fantastic code...&lt;/span&gt;
&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mapStateToProps&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;todos:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;mapDispatchToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;addTodo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;deleteTodo&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;mapStateToProps&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;mapDispatchToProps&lt;/span&gt;
&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="nc"&gt;TodoApp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Let's recap!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The connect() (Higher-Order Component) function connects a React component to a Redux store.&lt;br&gt;
This function provides its connected component with the data that I need from the store, and the functions that I need to use to dispatch actions to the store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mapStateToProps&lt;/span&gt;&lt;span class="o"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;mapDispatchToProps&lt;/span&gt;&lt;span class="o"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;mergeProps&lt;/span&gt;&lt;span class="o"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;?)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On connect function we have mapStateToProps and mapDispatchToProps props.&lt;/p&gt;

&lt;p&gt;The mapStateToProps and mapDispatchToProps deals with your Redux stores.&lt;br&gt;
For more information about connect read the official documentation. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Ok, so you don't want to write a function Connect, how do you think to replace mapStateToProps and mapDispatchToProps ? &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mmmm, let me think just a little more.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;How do I think to replace that?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If I don't write this connect you need to still receiving props from the state and I want to still dispatch functions, how can I still using this?&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Hooks it`s the answer!&lt;/em&gt;
&lt;/h3&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%2Fi%2F8s90ztx8evvwyk35ygb6.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%2Fi%2F8s90ztx8evvwyk35ygb6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Oh buddy, what that means?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Hooks&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The ability to handle a component's state and side effects is now a common pattern in a functional component. &lt;br&gt;
The react-redux library now has support for Hooks in React and React Native apps that make use of Redux as the state management library. &lt;br&gt;
React Redux offers a set of Hook APIs as an alternative to the omnipresent connect().&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Let's use these hooks!&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  *using useSelector
&lt;/h2&gt;

&lt;p&gt;useSelector is The equivalent of map state to props. It takes in a function argument that returns the part of the state that you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  *using useDispatch
&lt;/h2&gt;

&lt;p&gt;useDispatch is The equivalent of map dispatch to props. We will invoke useDispatch and store it to a variable, dispatch. Dispatch will work with the all actions imported from the actions folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Let's rewrite our code!&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;import React from "react";&lt;br&gt;
import { useDispatch, useSelector } from "react-redux";&lt;br&gt;
import { addTodo, deleteTodo } from './actions’&lt;/p&gt;

&lt;p&gt;export const TodoApp = () =&amp;gt; {&lt;br&gt;
&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
  const todos = useSelector(state =&amp;gt; state.todos);&lt;br&gt;
  const dispatch = useDispatch();&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
  return (&lt;br&gt;
     //a fantastic code that dispatch actions&lt;br&gt;
     (example: dispatch(addTodo()))&lt;br&gt;
  );&lt;br&gt;
};&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The main benefit of using the Redux Hooks is that they are conceptually simpler than wrapping components in the connect higher-order component. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Another benefit of not using the higher-order component is that you no longer get what I call “Virtual DOM of death” (will be my next article).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With connect, you are wrapping your component and injecting props into it. While the Redux Hooks have many benefits, there is one benefit of using connect instead of the Redux Hooks, and that is that it keeps your component decoupled from Redux&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Tell me in the comments what do you think about that, and what do you suggest to me to still using connect function?.&lt;/em&gt;
&lt;/h3&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Reference.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://es.wikipedia.org/wiki/Steve_Jobs" rel="noopener noreferrer"&gt;https://es.wikipedia.org/wiki/Steve_Jobs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://react-redux.js.org/api/connect" rel="noopener noreferrer"&gt;https://react-redux.js.org/api/connect&lt;/a&gt;&lt;br&gt;
&lt;a href="https://react-redux.js.org/api/hooks" rel="noopener noreferrer"&gt;https://react-redux.js.org/api/hooks&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>redux</category>
      <category>hook</category>
    </item>
    <item>
      <title>Using React.memo() to skip unnecessary rendering.</title>
      <dc:creator>guillermodv</dc:creator>
      <pubDate>Sat, 03 Oct 2020 23:00:29 +0000</pubDate>
      <link>https://dev.to/guillermodv/using-react-memo-to-skip-unnecessary-rendering-5ae2</link>
      <guid>https://dev.to/guillermodv/using-react-memo-to-skip-unnecessary-rendering-5ae2</guid>
      <description>&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%2Fi%2Fk4ysr968wj6153ou9kcg.jpg" 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%2Fi%2Fk4ysr968wj6153ou9kcg.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“the one who does not have a memory, creates one of paper” Gabriel García Marquez.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why do we need to use memo on React.js components?
&lt;/h1&gt;

&lt;p&gt;First of all, let´s recap how React works. Every time that our state changes the component will be rerendered, so each child component will be re-render throwing functions on the creation component.&lt;br&gt;
Then if for example, we have a component father called Customers and a child component called ListCostumers that shows a list of Customers with every row has a lot of information of that component that was obtained by the way of fetch on use effect method or just using a get function like Axios or something like that. Every time that the father component (Customers) will be re-rendered her son ListCostumers will be rerendered and the fetch function will be called, paying for that a lot of time on computer time and money if we pay for cloud database use or something like that.&lt;br&gt;
To avoid this, we can say that the child component (List Customers) may be memorized (React.memo) to avoid re-renders.&lt;/p&gt;
&lt;h1&gt;
  
  
  How can I use that and what does it mean?
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Let me write just a line of code…&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ListCustomers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;fantastic&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;...));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React started to observe this received props called customers and memorized them and after the first render of this component every time that we re-render this child component customers will be compared with the last customer's props and if we don't have any changes this child component will not be rerendered avoiding a fetch or render functions that consume a lot of computational power.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, ok great from now, I am going to always use memo components. Is it great?&lt;/li&gt;
&lt;li&gt;Just no, boy...&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Why can't we always use a memo?
&lt;/h1&gt;

&lt;p&gt;Memo consumes a lot of computational power too, you need to consider that a list comparative for example cost in terms of computational times.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, Every line of fantastic code will consume computer time.&lt;/li&gt;
&lt;li&gt;Yes, but it's not easy we need to consider a lot of things.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We need to considerate that the power consumption of memo comparatives is lower than executing a fetch or another function that we need to use to render our child components and how we know each child component too ( cascade render ) and for example if we need to fetch from AWS or Cloud Database we need to pay this computational power if we have this situation come on, use memo!.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Great power comes with great responsibility, use it with care, you need to ensure where you want to use your computational power.&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%2Fi%2Fm42v4cdb64enq35i6y8a.jpg" 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%2Fi%2Fm42v4cdb64enq35i6y8a.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Gabriel_Garc%C3%ADa_M%C3%A1rquez" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Gabriel_Garc%C3%ADa_M%C3%A1rquez&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;React 16.6: React.memo() &lt;a href="https://reactjs.org/blog/2018/10/23/react-v-16-6.html" rel="noopener noreferrer"&gt;https://reactjs.org/blog/2018/10/23/react-v-16-6.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Guillermo A. Del Vecchio.&lt;/em&gt;&lt;br&gt;
Sr software engineer, Golden Boy React.js / React Native Developer. &lt;/p&gt;

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