<?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: shaaandi</title>
    <description>The latest articles on DEV Community by shaaandi (@shaaandi).</description>
    <link>https://dev.to/shaaandi</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%2F445668%2F4b8885d7-3f8a-471c-bcfa-6f076af8ec6b.png</url>
      <title>DEV Community: shaaandi</title>
      <link>https://dev.to/shaaandi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shaaandi"/>
    <language>en</language>
    <item>
      <title>Returning JSX from hooks
</title>
      <dc:creator>shaaandi</dc:creator>
      <pubDate>Sat, 20 Nov 2021 09:14:02 +0000</pubDate>
      <link>https://dev.to/shaaandi/returning-jsx-from-hooks-ppl</link>
      <guid>https://dev.to/shaaandi/returning-jsx-from-hooks-ppl</guid>
      <description>&lt;p&gt;I want to know if there is any performance issue if I do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const useCounter = () =&amp;gt; {
  const [counter, setCounter] = useState(0)

  const increment = () =&amp;gt; setCounter(p =&amp;gt; p + 1)

  return {
  jsx: {
    count: &amp;lt;div&amp;gt;{counter}&amp;lt;/div&amp;gt;,
    incrementBtn: &amp;lt;button onClick={increment}&amp;gt;Add&amp;lt;/button&amp;gt;
  },
  }
}
// parent:
const Parent = () =&amp;gt; {
  const { jsx: { count, incrementBtn } } = useCounter()
  return (
    &amp;lt;div&amp;gt;
      {count}
      {incrementBtn}
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WHY I am doing this ?&lt;br&gt;
The above is a very simple, meaningless, example I have created to just show what I want to do. However, I will list the motivation here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separation of concerns. I want to keep my app in three layers.
&lt;/h2&gt;

&lt;p&gt;UI Layer- Any code the defines the core design -- for example, buttons, or maybe big components like a Map, Table. &lt;/p&gt;

&lt;p&gt;Integration Layer- Any code the connects my UI with the API layer. Here we can think of using useQuery hooks to fetch data and then transform it into the UI layer. &lt;/p&gt;

&lt;p&gt;Layout Layer - Any code the handles the final logic of keeping the above code in specified places in a view.&lt;/p&gt;

&lt;p&gt;So, doing above, I came across cases where two components are tightly coupled and need to share state but I do want to give the final positioning power to the Layout layer, so in the case, I define those components in a hook with all the logic needed and then return them as JSX and then the layout layer places them where it wants.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstracting the inner state to components.
&lt;/h2&gt;

&lt;p&gt;The second reason is to make the things more faster to develop for specific cases. For example, a component that provides a ui and also the state that changes upon interaction with that UI.&lt;/p&gt;

&lt;p&gt;In summary, just want to know if there is any perf concern here in the above code. And if yes, please state how ? Thanks. ❤️&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>Returning JSX from hooks</title>
      <dc:creator>shaaandi</dc:creator>
      <pubDate>Sat, 20 Nov 2021 07:56:51 +0000</pubDate>
      <link>https://dev.to/shaaandi/returning-jsx-from-hooks-1724</link>
      <guid>https://dev.to/shaaandi/returning-jsx-from-hooks-1724</guid>
      <description>&lt;p&gt;I want to know if there is any performance issue if I do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hook: useCounter 
const useCounter = () =&amp;gt; {
  const [counter, setCounter] = useState(0)

  const increment = () =&amp;gt; setCounter(p =&amp;gt; p + 1)

  return {
  jsx: {
    count: &amp;lt;div&amp;gt;{counter}&amp;lt;/div&amp;gt;,
    incrementBtn: &amp;lt;button onClick={increment}&amp;gt;Add&amp;lt;/button&amp;gt;
  },
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Parent = () =&amp;gt; {
  const { jsx: { count, incrementBtn } } = useCounter()
  return (
    &amp;lt;div&amp;gt;
      {count}
      {incrementBtn}
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WHY I am doing this ?&lt;br&gt;
The above is a very simple, meaningless, example I have created to just show what I want to do. However, I will list the motivation here.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Separation of concerns.
I want to keep my app in three layers. 
UI Layer- Any code the defines the core design -- for example, buttons, or maybe big components like a Map, Table.
Integration Layer- Any code the connects my UI with the API layer. Here we can think of using useQuery hooks to fetch data and then transform it into the UI layer.
Layout Layer - Any code the handles the final logic of keeping the above code in specified places in a view.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, doing above, I came across cases where two components are tightly coupled and need to share state but I do want to give the final positioning power to the Layout layer, so in the case, I define those components in a hook with all the logic needed and then return them as JSX and then the layout layer places them where it wants.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Abstracting the inner state to components. 
The second reason is to make the things more faster to develop for specific cases. For example, a component that provides a ui and also the state that changes upon interaction with that UI. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In summary, just want to know if there is any perf concern here in the above code. And if yes, please state how ? Thanks. ❤️&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
