<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
      <id>https://wolfgirl.dev/blog/</id>
      <title>PolyWolf's Blog - #reactjs</title>
      <subtitle
        >all posts tagged "reactjs" on a blog written by PolyWolf</subtitle
      >
      <icon>/apple-touch-icon.png</icon>
      <link href="/tags/reactjs/rss.xml" rel="self" />
      <link href="https://wolfgirl.dev/blog/" />
      <updated>2025-03-20T16:56:15Z</updated>
      
    <entry>
      <title><![CDATA[Quick Thoughts On: React vs SolidJS]]></title>
      <link rel="alternate" href="https://wolfgirl.dev/blog/2025-03-20-quick-thoughts-on-react-vs-solidjs/" />
      <id>https://wolfgirl.dev/blog/2025-03-20-quick-thoughts-on-react-vs-solidjs/</id>
      <published>2025-03-20T16:56:15Z</published>
      <updated>2025-03-20T16:56:15Z</updated>
      <summary><![CDATA[TL;DR just use React, or Preact if you care about bundle size.  ---  A while ago, I [wrote my own post composer](https://wolfgirl.dev...]]></summary>
      <content type="html"><![CDATA[<p>TL;DR just use React, or Preact if you care about bundle size.</p><hr><p>A while ago, I <a href=https://wolfgirl.dev/blog/2024-11-08-what-if-anyone-could-use-my-post-composer-/>wrote my own post composer</a>, which I use to make blog posts from my phone, including this one. It's pretty nice!</p><p>One of the requirements I had when making it was "I want my bundle sizes to be as small as possible". Inspired by seeing my work's 66MB React app, I decided to look for other frontend frameworks, still reactive (because that <em>is</em> just the best paradigm), but smaller & faster.</p><p>Eventually, I settled on SolidJS. After having used it for a while on more than just a simple "Hello, World!" project, I can confidently claim there's a pretty fatal (for me) flaw:</p><h2 id=heading-solidjs-defaults-to-not-re-rendering-when-values-change>SolidJS defaults to <em>not</em> re-rendering when values change<a aria-label="Link to heading 'SolidJS defaults to not re-rendering when values change'" data-heading-content="SolidJS defaults to not re-rendering when values change" class=anchor href=#solidjs-defaults-to-not-re-rendering-when-values-change></a></h2><p>From my understanding, this is how SolidJS works: everything is based around the "signal" as the underlying reactive primitive, with function calls as the way to subscribe to those signals:</p><pre class=syntax-highlighting><code class=language-jsx><span class=keyword>export</span> <span class=keyword>const</span> <span class=constructor>Component</span> <span class=operator>=</span> <span class=punctuation>(</span><span class=variable>props</span><span class=punctuation>)</span> <span class=operator>=></span> <span class=punctuation>{</span>
  <span class=keyword>const</span> <span class=punctuation>[</span><span class=variable>name</span><span class=punctuation>,</span> <span class=variable>setName</span><span class=punctuation>]</span> <span class=operator>=</span> <span class=function>createSignal</span><span class=punctuation>(</span><span class=string>""</span><span class=punctuation>);</span>
  <span class=keyword>const</span> <span class=function>title</span> <span class=operator>=</span> <span class=punctuation>()</span> <span class=operator>=></span> <span class=string>`</span><span class=punctuation>${</span><span class=variable>props</span><span class=punctuation>.</span><span class=property>greeting</span><span class=punctuation>}</span><span class=string>, </span><span class=punctuation>${</span><span class=function>name</span><span class=punctuation>()}</span><span class=string>`</span><span class=punctuation>;</span>
  <span class=keyword>return</span> <span class=punctuation>(</span>
    <span class=operator>&lt;</span><span class=variable>form</span><span class=operator>></span>
      <span class=operator>&lt;</span><span class=constructor>Title</span> <span class=property>text</span><span class=operator>=</span><span class=punctuation>{</span><span class=function>title</span><span class=punctuation>()}</span> />
      <span class=operator>&lt;</span><span class=variable>input</span> <span class=property>type</span><span class=operator>=</span><span class=string>"text"</span> <span class=property>value</span><span class=operator>=</span><span class=punctuation>{</span><span class=function>name</span><span class=punctuation>()}</span> <span class=property>onChange</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>e</span> <span class=operator>=></span> <span class=function>setName</span><span class=punctuation>(</span><span class=variable>e</span><span class=punctuation>.</span><span class=property>target</span><span class=punctuation>.</span><span class=property>value</span><span class=punctuation>)}</span> />
    &lt;/<span class=variable>form</span><span class=operator>></span>
  <span class=punctuation>);</span>
<span class=punctuation>}</span></code></pre><p>When <code>title()</code> is called, it will then call both <code>name()</code> (obviously) and <code>props.greeting</code> (non-obviously, since it's a special getter, which is why <a href=https://docs.solidjs.com/concepts/components/props#destructuring-props>you can't destructure <code>props</code></a>). By calling these functions, anything that calls <code>title()</code> will then know to re-render whenever either <code>name()</code> or <code>props.greeting</code> changes. So, <code>&lt;Title /></code> will re-render when <code>props.greeting</code> changes, and both <code>&lt;Title /></code> and <code>&lt;input></code> will re-render when <code>name()</code> changes.</p><p>However, if you <em>don't</em> make <code>title</code> a function, then <code>name()</code> and <code>props.greeting</code> will be called "too early", and <code>&lt;Title /></code> will never re-render; the body of <code>Component</code> isn't hooked up to any reactivity, just special places like the return value & <code>createEffect</code>.</p><p>This whole approach makes fine-grained reactivity quite simple, but also makes actually writing components a bit complex. You have to hook up all the reactive parts yourself!! It's very easy to get this wrong (I did many times), and the tooling (eslint) isn't helpful at detecting when it happens.</p><h2 id=heading-react-may-over-render-but-at-least-its-easier-to-conceptualize>React may over-render, but at least it's easier to conceptualize<a aria-label="Link to heading 'React may over-render, but at least it's easier to conceptualize'" data-heading-content="React may over-render, but at least it's easier to conceptualize" class=anchor href=#react-may-over-render-but-at-least-its-easier-to-conceptualize></a></h2><p>Here's the equivalent React example; it's just minor syntactical differences:</p><pre class=syntax-highlighting><code class=language-jsx><span class=keyword>export</span> <span class=keyword>const</span> <span class=constructor>Component</span> <span class=operator>=</span> <span class=punctuation>({</span> greeting <span class=punctuation>})</span> <span class=operator>=></span> <span class=punctuation>{</span>
  <span class=keyword>const</span> <span class=punctuation>[</span><span class=variable>name</span><span class=punctuation>,</span> <span class=variable>setName</span><span class=punctuation>]</span> <span class=operator>=</span> <span class=function>useState</span><span class=punctuation>(</span><span class=string>""</span><span class=punctuation>);</span>
  <span class=keyword>const</span> <span class=variable>title</span> <span class=operator>=</span> <span class=string>`</span><span class=punctuation>${</span><span class=variable>greeting</span><span class=punctuation>}</span><span class=string>, </span><span class=punctuation>${</span><span class=variable>name</span><span class=punctuation>}</span><span class=string>`</span><span class=punctuation>;</span>
  <span class=keyword>return</span> <span class=punctuation>(</span>
    <span class=operator>&lt;</span><span class=variable>form</span><span class=operator>></span>
      <span class=operator>&lt;</span><span class=constructor>Title</span> <span class=property>text</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>title</span><span class=punctuation>}</span> />
      <span class=operator>&lt;</span><span class=variable>input</span> <span class=property>type</span><span class=operator>=</span><span class=string>"text"</span> <span class=property>value</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>name</span><span class=punctuation>}</span> <span class=property>onChange</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>e</span> <span class=operator>=></span> <span class=function>setName</span><span class=punctuation>(</span><span class=variable>e</span><span class=punctuation>.</span><span class=property>target</span><span class=punctuation>.</span><span class=property>value</span><span class=punctuation>)}</span> />
    &lt;/<span class=variable>form</span><span class=operator>></span>
  <span class=punctuation>);</span>
<span class=punctuation>}</span></code></pre><p>How React works is, any time the component props/<code>useState</code> values change, it runs the component body again, as well as any children, recursively. This gets us a lot of neat benefits over SolidJS, like:</p><ul><li>you can destructure <code>props</code></li><li>not everything has to be functions</li><li>if something changes in state, it is guaranteed to cause a re-render that will display the change.</li></ul><p>That approach would be expensive if, when a re-render happened, we had to remove all the old HTML elements from the page and add back the new ones, so instead React uses something called "Virtual DOM Diffing" to only call browser APIs for elements/attributes that need changing. Some unavoidable downsides are the fact you need to keep a virtual copy of the whole tree in-memory, as well as the pretty complex algorithm to do the diffing.</p><p>SolidJS cuts thru all this implementation complexity & potential runtime wastefulness by composing a simple + powerful primitive, signals. Unfortunately, it's a bit <em>too</em> simple + powerful, ending up finnicky to use as a result, like a Lisp, perhaps.</p><p>If you're making a non-trivial Javascript-powered application, just use React. The <a href=https://react.dev/learn/react-compiler>React Compiler</a> takes care of a lot of the problems React has, effectively providing fine-grained reactivity "for free" (bit more compile-time cost oop). <a href=https://preactjs.com/>Preact</a> is also available if you care about bundle sizes; the only reason I didn't choose it initially was because I wanted to learn a new framework. Still, sometimes u just gotta go back to ol' reliable.</p>]]></content>
      <author>
        <name>PolyWolf</name>
        <uri>https://wolf.girl.technology/</uri>
      </author>
    </entry>
  
    <entry>
      <title><![CDATA[Some Reasons Your React App Might Be Slow]]></title>
      <link rel="alternate" href="https://wolfgirl.dev/blog/5801824/" />
      <id>https://wolfgirl.dev/blog/5801824/</id>
      <published>2024-05-02T01:29:01Z</published>
      <updated>2024-05-02T01:29:01Z</updated>
      
      <content type="html"><![CDATA[<p>Originally published on Cohost in 2 parts.</p><hr><p><a href=https://mastodon.social/@danluu/112368308357914085>Dan Luu, on Mastodon</a>:</p><blockquote><p>Naive question: why do React apps in the real world tend to be slow?</p><p>I tried doing a React tutorial and the result was quite fast (w.r.t. latency & CPU utilization on low-end devices) until the tutorial has you replace "manual" / "low-level" react calls with commonly used libraries, e.g., using TanStack Query instead of useEffect plus a manually instantiated cache.</p><p>Is the main issue that libraries tend to be big and slow or is there another major cause of React app slowness?</p></blockquote><p>wow, a question i actually have a smidgen of expertise on!</p><p>At work, we have a very big React app. Here are some things which were causing slowness, which we know for certain because fixing them improved performance:</p><ol><li>Excessive use of react-redux and redux-saga. These are libraries which have you put everything you want to share between unrelated (not direct children) components in global state, and then runs "selectors" (arbitrary javascript functions to get specific data out of the global state) every time any of that state changes. Across thousands of selectors and many state changes per second, this really adds up. We have good reason to believe we're one of the largest users of Redux, so this might not be an issue for everyone.</li><li>Excessive rerenders, and non-memoized rerenders. You can have excessive rerenders when your component gets non-memoized objects (or, functions, arrays, etc.) as props. A child can do all the proper useCallback and useMemo it wants, but it's on the parent to not mess up. Which happens often since it's a pain to wrap every single arrow function in useCallback everywhere (React docs explicitly say to not do this until performance is a concern, even!). And even then, if a parent rerenders for whatever reason, it will rerender all its children, even if their props are all the same, unless the child is memoized, which is its own problem if the heirarchy is very deep.</li><li>Too Many DOM nodes. This is a general problem with component-like frameworks, encouraging a kind of abstraction leading to far too many wrapper divs since they're so easy to add. All these extra DOM nodes come at a surprising performance penalty, especially in browsers like Safari.</li></ol><p>Not quite an exhaustive "things that make your React app slow", but hopefully useful nonetheless :P</p><hr><h1 id=heading-other-reasons-your-very-big-website-might-be-very-slow>Other Reasons Your Very Big Website Might Be Very Slow:<a aria-label="Link to heading 'Other Reasons Your Very Big Website Might Be Very Slow:'" data-heading-content="Other Reasons Your Very Big Website Might Be Very Slow:" class=anchor href=#other-reasons-your-very-big-website-might-be-very-slow></a></h1><ol start=4><li>Animations. The designer loves to go ham and make absolutely everything animated, but eventually yeah those start to add up too, especially when animating massive DOM trees and causing big layout shifts. Combined with (3) above, it sometimes takes a solid <strong>2 seconds</strong> to open a certain problematic side panel, on a good machine. Good luck on a little dinky machine. This problem is also exacerbated by using libraries like framer-motion, which have lots of cool animation curves, not all of them implemented efficiently! Using CSS animations where possible also lets you quickly turn them off with <code>animation: none !important; transition: none !important;</code></li><li>Opacity. The designer also loves using <code>rgba(255, 255, 255, 0.05)</code> for component backgrounds, because it layers nicely over almost any other background. Unfortunately, they require more work by the browser to render properly, which can amount to a surprising drop in performance on weaker clients. Especially when combined with animated transparencies🙀 Making an option to change to all flat colors was a noticeable gain. And this is all to say nothing of blur, which tanks performance even harder (we've almost got rid of it entirely now)</li><li>Excessive Repaints From Weird Layouts. Still haven't quite gotten to the bottom of this so can't say for sure, but it seems that certain layout things (CSS grid? Weird stacking contexts?) cause browsers to think they need to repaint more than what's actually changed, leading to a drop in performance. Saying "hey change the size of this one thing slightly" makes it go "oh i should repaint the whole screen, got it", even when there's no actual layout shift. This is once again exacerbated by animations, which cause this many times a second.</li></ol><p>Once again, these are just problems we've identified, and there may be more, I am by no means a Web Performance Expert. yet.</p>]]></content>
      <author>
        <name>PolyWolf</name>
        <uri>https://wolf.girl.technology/</uri>
      </author>
    </entry>
  
    </feed>
  