<?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: Blake Wight</title>
    <description>The latest articles on DEV Community by Blake Wight (@bwighthunter).</description>
    <link>https://dev.to/bwighthunter</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%2F304560%2F54611688-838b-4d7d-aeaa-811487bdfd71.jpg</url>
      <title>DEV Community: Blake Wight</title>
      <link>https://dev.to/bwighthunter</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bwighthunter"/>
    <language>en</language>
    <item>
      <title>How to write more performant functional React Components</title>
      <dc:creator>Blake Wight</dc:creator>
      <pubDate>Thu, 25 Jun 2020 03:09:30 +0000</pubDate>
      <link>https://dev.to/bwighthunter/how-to-write-more-performant-functional-react-components-1jo7</link>
      <guid>https://dev.to/bwighthunter/how-to-write-more-performant-functional-react-components-1jo7</guid>
      <description>&lt;h1&gt;
  
  
  What are you on about?
&lt;/h1&gt;

&lt;p&gt;When writing React with hooks, I've seen a lot of properties of functional components go un-memoized. In my opinion, this is almost always a no-no. &lt;/p&gt;

&lt;p&gt;This is generally not a good practice because of how React works. Anything that is not memoized is redefined on every re-render. So in general, my rule is to memoize everything defined in the functional component and move everything else out of the component into the global scope.&lt;/p&gt;

&lt;p&gt;Not only does this speed up the performance of the thing that you defined, but not doing this has a compounding effect. If the thing you forgot to memoize is depended on (meaning in the dependency array of useMemo or useCallback) by another property, &lt;em&gt;that&lt;/em&gt; property will be redefined on every render as well. &lt;/p&gt;

&lt;p&gt;Variables that are defined globally do not get redefined when re-renders happen so they can be used without worrying about causing redefinitions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Say what?
&lt;/h1&gt;

&lt;p&gt;Here is a nice before example from a personal project (ignore what is/isn't defined as far as imports):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { Title } = Typography;

const UserScreen = () =&amp;gt; {
  const db = firebaseApp.firestore();
  const { user, logout } = useContext(AuthContext);

  const onSubmit =
    async (newUserProperties) =&amp;gt; {
      return await db
        .collection(DB.COLLECTIONS.USERS)
        .doc(user.uid)
        .update(newUserProperties);
    }

  return (...);
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case, the &lt;code&gt;firebaseApp.firestore()&lt;/code&gt; operation would be called on &lt;em&gt;every&lt;/em&gt; render. Not fun. And then even if we threw the &lt;code&gt;onSubmit&lt;/code&gt; function in a useCallback with db in the dependency array, &lt;em&gt;that&lt;/em&gt; would be redefined on every render as well. So you don't even get credit for almost doing it right!&lt;/p&gt;

&lt;h1&gt;
  
  
  Prove it
&lt;/h1&gt;

&lt;p&gt;Here is a codesandbox that illustrates my point: &lt;a href="https://codesandbox.io/s/relaxed-pasteur-7spqq?file=/src/App.js"&gt;https://codesandbox.io/s/relaxed-pasteur-7spqq?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Photo by &lt;a href="https://unsplash.com/@maximusfrajerus?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Max Frajer&lt;/a&gt; on &lt;a href="/s/photos/speed?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webperf</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Recursive Lists in React</title>
      <dc:creator>Blake Wight</dc:creator>
      <pubDate>Wed, 24 Jun 2020 17:40:32 +0000</pubDate>
      <link>https://dev.to/bwighthunter/recursive-lists-in-react-59a3</link>
      <guid>https://dev.to/bwighthunter/recursive-lists-in-react-59a3</guid>
      <description>&lt;p&gt;Recently I have been working a lot with recursive trees and displaying them in React. While working with them I came up with a pattern that I like, and that I want to share with those that run into this pattern. They can overcome this hurdle in a way that can be reused quite easily.&lt;/p&gt;

&lt;p&gt;To start, you want to create a context that has all of the functionality of each node in your recursive tree might need. Lets for the sake of this article assume we are creating a nested keyword structure that we can use to tag stuff (pictures, tweets, posts, etc…) with. Nested meaning that keywords that have children that are selected, make themselves selected (dog -&amp;gt; retriever, selecting retriever, also tags it with dog). Great. The context now has functions for selecting, and deselecting keywords.&lt;/p&gt;

&lt;p&gt;Now, you create a keyword component that consumes the context and displays something, maybe what I would call a row. The only requirement I would make here is that you should have the component accept a callback that returns a function to retrieve data (in this case, the next page of data). You pass this component as a render method to the recurser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const Recurser = ({ render, getItems, id }) =&amp;gt; {
  const Component = render // for react capital convention
  const { items } = getItems(id)
  return &amp;lt;Component&amp;gt;
    {items.map(itemId =&amp;gt; &amp;lt;Recurser render={render} getItems={getItems} id={itemId}/&amp;gt;)}   
  &amp;lt;/Component&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In general, your structure would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ContextProvider&amp;gt;
  &amp;lt;Recurser&amp;gt;
    &amp;lt;ContextConsumer&amp;amp;Render /&amp;gt;
  &amp;lt;/Recurser&amp;gt;
&amp;lt;/ContextProvider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This assumes some things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You have to pass a function (or a react hook) to the Recurser component so that it can get children given the parent’s id. A function can work, but a dynamic hook can let you connect to redux, or consume a react context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The context provider holds the state for knowing what an item is tagged with, and provides functions for (in the case of keywords) changing that state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The context consumer and render component connects to the context provider and probably another context or redux state. This would allow the render component to get (again for keywords) the keyword’s name given the id. In the case of more complex objects, you could retrieve all properties of that object given an id.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This allows you to have different recursive lists, but keep the way they are nested and loaded. This could be good for a component in a keyword setup page, to set up possible keyword structures, and a second keyword component for tagging items with keywords. The lists can have separate functionality and layout but keep the recursion.&lt;/p&gt;

&lt;p&gt;It seems pretty trivial and seems small enough it’s almost not worth implementing, but it has saved us a lot of time as you can change it to be more complex which for us means wrapping the children in an infinite scroll component. Wrapping children in an infinite scroll allows us to lazy load children, but requires more to be returned from getItems (for us that is stuff like {loading: bool, done: bool, loadMore: func, trigger: string}).&lt;/p&gt;

&lt;p&gt;If this was confusing, please ask questions! This is my first post like this.&lt;/p&gt;

&lt;p&gt;Check out a codepen example here: &lt;a href="https://codesandbox.io/embed/fervent-beaver-9btj1"&gt;https://codesandbox.io/embed/fervent-beaver-9btj1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: The codepen is a pretty big example, but I tried to replicate the fact that in redux you get data with selectors, and you don’t get the promise (or observable, or callback or whatever) in the component itself. It uses a custom hook in the index file that has comment explanations.&lt;/p&gt;

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