<?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[This web dev stuff is hard]]></title><description><![CDATA[A blog about web development.]]></description><link>https://paularmstrong.now.sh</link><generator>RSS for Node</generator><lastBuildDate>Thu, 03 Jan 2019 19:08:11 GMT</lastBuildDate><item><title><![CDATA[Prop type performance considerations for React]]></title><description><![CDATA[Rendering large trees of components in React can be expensive. When the state managed by Redux changes, it triggers updates to the main…]]></description><link>https://paularmstrong.now.sh/02-prop-types/</link><guid isPermaLink="false">https://paularmstrong.now.sh/02-prop-types/</guid><pubDate>Thu, 03 Jan 2019 16:19:46 GMT</pubDate><content:encoded>&lt;p&gt;Rendering large trees of components in React can be expensive. When the state managed by Redux changes, it triggers updates to the main component tree and on down through all of the sub-trees. When state updates often, this can start to slow down your application while it needs to re-render every component.&lt;/p&gt;
&lt;p&gt;To prevent this from happening, you might make use of &lt;code class=&quot;language-text&quot;&gt;PureComponent&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;shouldComponentUpdate&lt;/code&gt;. While each of these is a strategy for managing updates to components and their sub-trees, there are certain patterns that can slip by and not be caught appropriately.&lt;/p&gt;
&lt;p&gt;It is also important to understand that using &lt;code class=&quot;language-text&quot;&gt;PureComponent&lt;/code&gt; is typically the goal, whenever possible, since it is optimized at React’s library level. Custom &lt;code class=&quot;language-text&quot;&gt;shouldComponentUpdate&lt;/code&gt; implementations are encouraged when &lt;code class=&quot;language-text&quot;&gt;PureComponent&lt;/code&gt; may not be possible (e.g. an entire data structure object is required as a prop to render a component, like a &lt;a href=&quot;https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object.html&quot;&gt;Tweet object&lt;/a&gt;).&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Note: I found this in some archives of half-finished blog posts written sometime between 2015 and 2018. Some items may be a bit dated, the patterns not super new, but I hope that the lessons and ideas are helpful either way.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;New Arrays and Objects&lt;/h2&gt;
&lt;p&gt;Most components use (or should use) shallow comparisons against their props and state during &lt;code class=&quot;language-text&quot;&gt;shouldComponentUpdate&lt;/code&gt; (or in &lt;code class=&quot;language-text&quot;&gt;PureComponent&lt;/code&gt;) in order to determine whether or not they need to be updated and re-rendered.&lt;/p&gt;
&lt;p&gt;When a property is an object or array, every time a new instance of one is passed in, it will fail shallow comparison and trigger an update. Take the following scenarios:&lt;/p&gt;
&lt;h3&gt;Static data&lt;/h3&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;  render() {
    return (
&lt;span class=&quot;token deleted&quot;&gt;-     &amp;lt;Component locationState={{ foo: &apos;bar&apos; }} /&gt;&lt;/span&gt;
    );
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above case, every time &lt;code class=&quot;language-text&quot;&gt;render&lt;/code&gt; is called, a new object with the same key/value pairs will be sent in &lt;em&gt;every&lt;/em&gt; time. This is unnecessary, since the data is static. It can easily be alleviated by hoisting the static data outside of any lifecycle.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token inserted&quot;&gt;+ const locationState = { foo: &apos;bar&apos; };&lt;/span&gt;

// ...

  render() {
    return (
&lt;span class=&quot;token inserted&quot;&gt;+     &amp;lt;Component locationState={locationState} /&gt;&lt;/span&gt;
    );
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, &lt;code class=&quot;language-text&quot;&gt;this.props.locationState&lt;/code&gt; will always be stricly equal (&lt;code class=&quot;language-text&quot;&gt;===&lt;/code&gt;) to &lt;code class=&quot;language-text&quot;&gt;nextProps.locationState&lt;/code&gt; and not trigger an update/re-render when using a &lt;code class=&quot;language-text&quot;&gt;PureComponent&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Building from instance props or state&lt;/h3&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;  render() {
&lt;span class=&quot;token deleted&quot;&gt;-   const { item1, item2 } = this.props;&lt;/span&gt;
    return (
&lt;span class=&quot;token deleted&quot;&gt;-     &amp;lt;Component data={[ item1, item2 ]} /&gt;&lt;/span&gt;
    );
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, a sub-component is dependent on the current component’s props and state. It may not be possible to prevent rendering at the parent level, so we need another solution to avoid creating a new array instance of these properties if they haven’t changed.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token inserted&quot;&gt;+ import createInstanceSelector from &apos;modules/create-instance-selector&apos;;&lt;/span&gt;

// ...

  render() {
    return (
&lt;span class=&quot;token inserted&quot;&gt;+     &amp;lt;Component data={this.getData()} /&gt;&lt;/span&gt;
    );
  }

&lt;span class=&quot;token inserted&quot;&gt;+ getData = createInstanceSelector(&lt;/span&gt;
&lt;span class=&quot;token inserted&quot;&gt;+   (props, state) =&gt; props.item1,&lt;/span&gt;
&lt;span class=&quot;token inserted&quot;&gt;+   (props, state) =&gt; props.item2,&lt;/span&gt;
&lt;span class=&quot;token inserted&quot;&gt;+   (item1, item2) =&gt; [ item1, item2 ]&lt;/span&gt;
&lt;span class=&quot;token inserted&quot;&gt;+ );&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We have a module called &lt;code class=&quot;language-text&quot;&gt;createInstanceSelector&lt;/code&gt; for this purpose. It works like &lt;code class=&quot;language-text&quot;&gt;reselect&lt;/code&gt;’s &lt;code class=&quot;language-text&quot;&gt;selectors&lt;/code&gt;, in that it memoizes composable, derived data. In the above case, &lt;code class=&quot;language-text&quot;&gt;this.getData()&lt;/code&gt; will only return a new array if either &lt;code class=&quot;language-text&quot;&gt;props.item1&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;props.item2&lt;/code&gt; has changed since the last call. Here’s an example of how you might implement that:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;js&quot;&gt;&lt;pre class=&quot;language-js&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; createSelector &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;reselect&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;componentInstance&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;selectors&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; instanceSelector &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;createSelector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;selectors&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; componentInstance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;instanceSelector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instance&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;props&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; instance&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;instance &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;New Functions&lt;/h2&gt;
&lt;p&gt;Similar to arrays and objects, every time a function is defined, it will fail strict equality checks. There are multiple ways to ensure you don’t create a new function on each render cycle. Pick the one that works best for you.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;  render() {
    return (
&lt;span class=&quot;token deleted&quot;&gt;-     &amp;lt;Component identityFunction={(item) =&gt; item.id} /&gt;&lt;/span&gt;
    );
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token inserted&quot;&gt;+ const identityFunction = (item) =&gt; item.id;&lt;/span&gt;
  // ...
  render() {
    return (
&lt;span class=&quot;token inserted&quot;&gt;+     &amp;lt;Component identityFunction={identityFunction} /&gt;&lt;/span&gt;
    );
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;Tip: You can use the &lt;a href=&quot;https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md&quot;&gt;eslint-plugin-react/jsx-no-bind&lt;/a&gt; rule to disallow the undesired pattern&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Components&lt;/h2&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;  render() {
    return (
&lt;span class=&quot;token deleted&quot;&gt;-     &amp;lt;Component button={&amp;lt;Button /&gt;} /&gt;&lt;/span&gt;
    )
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Functions that render sub-trees&lt;/h2&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;  class ParentComponent extends Component {
    render() {
      return (
&lt;span class=&quot;token deleted&quot;&gt;-       &amp;lt;Component renderButtons={this._renderButtons}&lt;/span&gt;
      );
    }

&lt;span class=&quot;token deleted&quot;&gt;-   _renderButtons = () =&gt; {&lt;/span&gt;
&lt;span class=&quot;token deleted&quot;&gt;-     const { items } = this.props;&lt;/span&gt;
&lt;span class=&quot;token deleted&quot;&gt;-     return items.map((item, i) =&gt; &amp;lt;Item data={item} key={i} /&gt;);&lt;/span&gt;
&lt;span class=&quot;token deleted&quot;&gt;-   };&lt;/span&gt;
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;When to use this pattern&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;When the output of &lt;code class=&quot;language-text&quot;&gt;_renderButtons&lt;/code&gt; &lt;em&gt;must not&lt;/em&gt; change&lt;/li&gt;
&lt;li&gt;When &lt;code class=&quot;language-text&quot;&gt;Component&lt;/code&gt; &lt;em&gt;must&lt;/em&gt; be re-rendered on every cycle&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;When not to use this pattern&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;When &lt;code class=&quot;language-text&quot;&gt;Component&lt;/code&gt; is a &lt;code class=&quot;language-text&quot;&gt;PureComponent&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep in mind, performance is almost always application specific. When you use &lt;code class=&quot;language-text&quot;&gt;PureComponent&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;shouldComponentUpdate&lt;/code&gt; are up to your needs. Take everything here as a starting guideline and not an end-all, be-all.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[A new blog]]></title><description><![CDATA[It’s been a long time since I wrote a blog. I kept one somewhat active from about 2003 until 2008. The problem was that I never really had a…]]></description><link>https://paularmstrong.now.sh/01-new-blog/</link><guid isPermaLink="false">https://paularmstrong.now.sh/01-new-blog/</guid><pubDate>Mon, 31 Dec 2018 23:41:21 GMT</pubDate><content:encoded>&lt;p&gt;It’s been a long time since I wrote a blog. I kept one somewhat active from about 2003 until 2008. The problem was that I never really had a project or life to concentrate on, so I spent more time redesigning the site and rewriting the application than actually writing content.&lt;/p&gt;
&lt;p&gt;I eventually gave up on the blog. Hunkered down on a life and career. And I learned. I learned a lot.&lt;/p&gt;
&lt;p&gt;Something clicked for me recently and I’ve decided to start pouring things out.&lt;/p&gt;
&lt;p&gt;The hard part about starting a new blog? I want to rewrite the blog software.&lt;/p&gt;
&lt;p&gt;I’ve decided to use &lt;a href=&quot;https://www.gatsbyjs.org/&quot;&gt;Gatsby.js&lt;/a&gt; for the software. Why? No reason, really. I haven’t used it before. I have absolutely no understanding of it other than I know that it uses React.js, can create static sites, and is easily deployed.&lt;/p&gt;
&lt;p&gt;I’ve created a new project in my editor and filtered out everything but the &lt;code class=&quot;language-text&quot;&gt;content/&lt;/code&gt; directory from my eyes so that I concentrate on writing posts instead of rewriting the software. Don’t get me wrong, Gatsby looks great! I just have a terrible habit of rewriting everything I see to just work a little bit differently.&lt;/p&gt;
&lt;p&gt;Here’s to a new blog. One which I turn a blind eye to the source, the design, and all the details of the blog, other than just writing.&lt;/p&gt;</content:encoded></item></channel></rss>