<?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: BlackJosh007</title>
    <description>The latest articles on DEV Community by BlackJosh007 (@blackjosh007).</description>
    <link>https://dev.to/blackjosh007</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3427851%2Fac26d1b8-8dfb-4674-b681-30075d5eb552.png</url>
      <title>DEV Community: BlackJosh007</title>
      <link>https://dev.to/blackjosh007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blackjosh007"/>
    <language>en</language>
    <item>
      <title>I Was the Only User, So I Never Hit the Bug I Saw</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Wed, 04 Nov 2026 17:09:20 +0000</pubDate>
      <link>https://dev.to/blackjosh007/i-was-the-only-user-so-i-never-hit-the-bug-i-saw-46ng</link>
      <guid>https://dev.to/blackjosh007/i-was-the-only-user-so-i-never-hit-the-bug-i-saw-46ng</guid>
      <description>&lt;p&gt;It's been a while since my last post. I've been building connections, mostly commenting on other people's posts, because I noticed it did more for my views and impression on these platforms than I expected. But I've also been learning, so here's what I picked up this week.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned, and how I made it stick
&lt;/h2&gt;

&lt;p&gt;This week was &lt;strong&gt;data manipulation in React&lt;/strong&gt;: searching, filtering, sorting and pagination. Following a tutorial is one thing. Building something with the concept is another, and building is what made it stick.&lt;/p&gt;

&lt;p&gt;So I built a small &lt;strong&gt;Product Explorer&lt;/strong&gt; with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;category filtering&lt;/li&gt;
&lt;li&gt;text search&lt;/li&gt;
&lt;li&gt;sorting&lt;/li&gt;
&lt;li&gt;pagination&lt;/li&gt;
&lt;li&gt;derived state&lt;/li&gt;
&lt;li&gt;reset behavior when filters change&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My brain went blank first
&lt;/h2&gt;

&lt;p&gt;I had learnt the syntax &lt;em&gt;and&lt;/em&gt; the concept. Then I was handed the exercise and my mind went blank.&lt;/p&gt;

&lt;p&gt;The reason was simple: I hadn't broken the task into subtasks. "Build a Product Explorer" is not a task, it's a pile of tasks. Once I thought of it as a pipeline, I knew where to start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MOCK_PRODUCTS → filter (category + search) → sort → paginate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I built it in that order: search logic first, then sorting, then pagination. Each stage takes the output of the one before it, which is why I could build them one at a time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;productSearched&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;MOCK_PRODUCTS&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;selectedCategory&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;all&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;selectedCategory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortedProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;productSearched&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalPages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sortedProducts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;productsPerPage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;sortedProducts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastIndex&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* render cards */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that nothing here is stored in state except the &lt;em&gt;inputs&lt;/em&gt; (search term, category, sort option, current page). Everything else is derived on every render. That's the derived state idea, and it's why the app can't get out of sync with itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug I was stuck on
&lt;/h2&gt;

&lt;p&gt;At some point my search stopped working. I traced the code and had two theories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Maybe I can't access &lt;code&gt;product.category&lt;/code&gt; directly because it's an array of objects.&lt;/li&gt;
&lt;li&gt;Maybe &lt;code&gt;productSearched&lt;/code&gt; gets saved before anything is searched.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I killed the second one myself. The variable is recalculated on every render, and every keystroke triggers a render, so that couldn't be it. The first was wrong too: dot notation on objects inside &lt;code&gt;.filter()&lt;/code&gt; is exactly right.&lt;/p&gt;

&lt;p&gt;I was convinced the problem was in my search logic. It wasn't.&lt;/p&gt;

&lt;p&gt;It was here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;selectedCategory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSelectedCategory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My dropdown's default option was &lt;code&gt;value="all"&lt;/code&gt;, and my filter checked &lt;code&gt;selectedCategory === 'all'&lt;/code&gt;. On the first render, &lt;code&gt;'' === 'all'&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, so every product was filtered out. Blank screen.&lt;/p&gt;

&lt;p&gt;The bug was in an initial &lt;em&gt;state value&lt;/em&gt;, nowhere near the logic I'd been staring at. I was looking in the right area of the code for the wrong kind of mistake.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part I'm slightly embarrassed about
&lt;/h2&gt;

&lt;p&gt;Here's the honest bit: &lt;strong&gt;I had actually noticed this blank screen before.&lt;/strong&gt; I just didn't dig into it.&lt;/p&gt;

&lt;p&gt;Why? Because I was the only user. I knew that picking a category from the dropdown made everything appear, so I just did that and moved on. It escaped my mind because it never got in &lt;em&gt;my&lt;/em&gt; way.&lt;/p&gt;

&lt;p&gt;That's the real lesson for me. When you're the developer, you only walk the paths you already know. You never see the bug a first-time user hits on their first second.&lt;/p&gt;

&lt;h2&gt;
  
  
  What my reviewers caught
&lt;/h2&gt;

&lt;p&gt;I sent the code to my "teachers" (ChatGPT and Gemini 😂) and they pointed out edge cases I would not have found on my own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wrong &lt;code&gt;totalPages&lt;/code&gt;.&lt;/strong&gt; I calculated it from &lt;code&gt;MOCK_PRODUCTS.length&lt;/code&gt; instead of the filtered results. Search for something with 2 matches and the pagination still thinks there are 3 pages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Missing page reset.&lt;/strong&gt; If you're on page 3 and then type a search that leaves 2 results, &lt;code&gt;currentPage&lt;/code&gt; is still 3 and the grid goes blank. The fix is to call &lt;code&gt;setCurrentPage(1)&lt;/code&gt; whenever search or category changes:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&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="nf"&gt;setSearchTerm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;setCurrentPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Missing &lt;code&gt;key&lt;/code&gt; prop.&lt;/strong&gt; I see this warning all the time and never treated it as serious. It is. React uses &lt;code&gt;key&lt;/code&gt; to track which item is which between renders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A &lt;code&gt;.filter()&lt;/code&gt; that returned &lt;code&gt;product&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;&lt;/strong&gt; instead of a boolean. It worked, but only by accident of truthiness.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Same pattern as before: every one of these was invisible to me &lt;em&gt;because I only tested the happy path&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'm taking away
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Every part of the code matters.&lt;/strong&gt; Warnings I scrolled past turned out to be real.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Test the unhappy paths on purpose.&lt;/strong&gt; Being on page 3 and then searching. Starting the app and touching nothing. Those are the cases the developer skips and real users hit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. AI is useful for review, but it isn't a replacement for testing.&lt;/strong&gt; It read my code and spotted problems fast, which is a real advantage. But for a real product, I'd still need actual testers and real users. What AI can do is make the review step cheaper.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Use AI for the boring parts, not the understanding.&lt;/strong&gt; I didn't want to spend an hour fiddling with CSS layout and static HTML, so I prompted AI to generate the layout with all the React state, variables and event handlers stripped out and replaced with &lt;code&gt;// TODO&lt;/code&gt; placeholders. The logic, which is what I was actually there to learn, I wrote myself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;It's genuinely fun to learn this, because search, sort, filter and pagination show up in almost every application you use. And the thing that made it click wasn't the tutorial. It was getting blank-screened by my own code.&lt;/p&gt;

&lt;p&gt;If you're learning React right now: build the thing, then try to break it like a stranger would.&lt;/p&gt;

&lt;p&gt;What's the most embarrassing "I was the only user" bug you've shipped? 👇&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Fetch Hook Bug Taught Me When (Not) to Use 6 React Concepts</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Tue, 06 Oct 2026 17:05:55 +0000</pubDate>
      <link>https://dev.to/blackjosh007/a-fetch-hook-bug-taught-me-when-not-to-use-6-react-concepts-414p</link>
      <guid>https://dev.to/blackjosh007/a-fetch-hook-bug-taught-me-when-not-to-use-6-react-concepts-414p</guid>
      <description>&lt;p&gt;This week I stopped learning React APIs and started learning their &lt;strong&gt;boundaries&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Knowing what &lt;code&gt;useContext&lt;/code&gt; does is easy. Knowing when it's the wrong tool is the real skill. So this post covers six concepts, each with a "use it when" and a "not when". We'll start with the one that humbled me.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Custom hooks (and when a plain helper is better)
&lt;/h2&gt;

&lt;p&gt;A custom hook reuses &lt;strong&gt;logic&lt;/strong&gt;, not UI. Instead of repeating the same state and effect logic across components, you pull it into a function that starts with &lt;code&gt;use&lt;/code&gt; and import it where you need it.&lt;/p&gt;

&lt;p&gt;One thing that's easy to get wrong: hooks don't magically share state. Every call to a hook creates its own state. Two components calling &lt;code&gt;useProduct(1)&lt;/code&gt; each get their own separate &lt;code&gt;product&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use a custom hook when&lt;/strong&gt; the logic calls React hooks (&lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useEffect&lt;/code&gt;, &lt;code&gt;useContext&lt;/code&gt;...) and you need it in more than one place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use a plain helper function when&lt;/strong&gt; the logic calls no hooks. Two quick cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Formatting &lt;code&gt;1500&lt;/code&gt; as &lt;code&gt;"₦1,500"&lt;/code&gt; is a &lt;strong&gt;helper&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Reading the current theme from Context to pick a button color is a &lt;strong&gt;custom hook&lt;/strong&gt;. It has no state of its own, but it calls &lt;code&gt;useContext&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the test isn't "does it have state?" but "does it call a hook?". A helper also works outside React (in a backend or a test), and unlike a hook it can be called inside loops and conditions.&lt;/p&gt;

&lt;p&gt;To practise, I wrote &lt;code&gt;useProduct&lt;/code&gt;, a hook that fetches a product by ID. It worked. Then I traced it line by line.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. AbortController (and the two bugs I found by tracing)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;AbortController&lt;/code&gt; cancels an async operation such as a &lt;code&gt;fetch&lt;/code&gt;. You create a controller, pass &lt;code&gt;controller.signal&lt;/code&gt; to &lt;code&gt;fetch&lt;/code&gt;, and call &lt;code&gt;controller.abort()&lt;/code&gt; to cancel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use it when&lt;/strong&gt; a request can become irrelevant before it finishes, like a user moving to another page, or switching from product 1 to product 2 mid-request. Without it, the old request keeps running and can still update your UI.&lt;/p&gt;

&lt;p&gt;Here was my first version's &lt;code&gt;finally&lt;/code&gt; block:&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bug 1: a stale request touching state.&lt;/strong&gt; I changed &lt;code&gt;productId&lt;/code&gt; from 1 to 2 while request 1 was in flight. The cleanup aborted request 1, but its &lt;code&gt;finally&lt;/code&gt; still ran &lt;code&gt;setLoading(false)&lt;/code&gt; while request 2 was loading. The UI said "done" while it was still working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bug 2: I never checked &lt;code&gt;response.ok&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;fetch&lt;/code&gt; only rejects when it gets no response at all (a network failure or an abort). A 404 &lt;em&gt;resolves normally&lt;/em&gt;, so I was storing an error body as a "product".&lt;/p&gt;

&lt;p&gt;While fixing bug 2, I found out that &lt;code&gt;response.statusText&lt;/code&gt; is empty over HTTP/2, so matching on it silently depends on the protocol. &lt;code&gt;response.status&lt;/code&gt;, the number, is the reliable signal.&lt;/p&gt;

&lt;p&gt;Here's the version that handles both:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useProduct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setProduct&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setProduct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AbortController&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setProduct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="s2"&gt;`https://dummyjson.com/products/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;httpError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Request failed with status &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nx"&gt;httpError&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;httpError&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nf"&gt;setProduct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;AbortError&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Product not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server error occurred&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;An error occurred while fetching the product&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aborted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three decisions in there:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An aborted request does nothing to state.&lt;/strong&gt; An abort always comes from the cleanup, so whatever triggered it (a new &lt;code&gt;productId&lt;/code&gt;, a &lt;code&gt;null&lt;/code&gt; ID, or an unmount) owns what happens to &lt;code&gt;loading&lt;/code&gt; next.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I attach &lt;code&gt;status&lt;/code&gt; to the error itself.&lt;/strong&gt; &lt;code&gt;fetch&lt;/code&gt; doesn't throw on a 404, so I throw it myself and carry the number to &lt;code&gt;catch&lt;/code&gt;, instead of matching on message strings that can drift apart.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I reset &lt;code&gt;product&lt;/code&gt; to &lt;code&gt;null&lt;/code&gt; on every fetch.&lt;/strong&gt; That means the UI flashes empty between products. I chose that over showing the previous product's data while the new one loads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Not when:&lt;/strong&gt; aborting stops you from &lt;em&gt;waiting&lt;/em&gt; for a response. It doesn't undo something the server already did, so it's not a safety net for a &lt;code&gt;POST&lt;/code&gt; that has already reached the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The real lesson:&lt;/strong&gt; both bugs came from tracing the order of events, not from knowing the API. I need to be able to simulate how my code runs, step by step, to catch behavior I didn't intend. I still need to work on that.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Composition (vs extracting components)
&lt;/h2&gt;

&lt;p&gt;Composition means wrapping a component around other content. The parent doesn't need to know what's inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"rounded-lg bg-gray-200 p-2"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserProfile&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule I registered: &lt;strong&gt;does this piece have a distinct responsibility that would make the parent easier to understand if extracted?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use it when&lt;/strong&gt; the answer is yes. &lt;strong&gt;Not when&lt;/strong&gt; it's no, because then you're only scattering code across files for the sake of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Derived state (vs stale state)
&lt;/h2&gt;

&lt;p&gt;Two rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Store the source of truth, then derive everything else from it.&lt;/li&gt;
&lt;li&gt;If a value can be calculated from current props or state during render, derive it instead of storing it.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Stale-prone: a copy that must be kept in sync by hand&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setItems&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Derived: always correct&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setItems&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stale state is what you get when a copy of a value stops matching the original.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use state when&lt;/strong&gt; the value is a source of truth, such as user input or fetched data. &lt;strong&gt;Not when&lt;/strong&gt; it can be computed from something you already have.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Context (vs props)
&lt;/h2&gt;

&lt;p&gt;For a long time I wondered: if a deeply nested component needs some data, must I pass it as props through every component in between, even the ones that don't use it? Context solves that. It lets you share data across a component subtree without passing it through every intermediate component.&lt;/p&gt;

&lt;p&gt;I first called it "global state", but that's wrong. It's a way to &lt;em&gt;share&lt;/em&gt; data, not to manage it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use it when&lt;/strong&gt; many components at different depths need the same data and it changes rarely, like the theme or the current user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not when&lt;/strong&gt; the data updates frequently, such as high-frequency input like typing in a form. Every component consuming a context re-renders whenever the provider's value changes, so a value that changes on every keystroke makes every consumer re-render on every keystroke. And if the data only travels a level or two, props are fine.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. useReducer (vs useState)
&lt;/h2&gt;

&lt;p&gt;Instead of scattering state logic across event handlers, &lt;code&gt;useReducer&lt;/code&gt; centralizes it in one function.&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;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;userReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;set_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;reset&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;userState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userDispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// in a handler&lt;/span&gt;
&lt;span class="nf"&gt;userDispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;set_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;useState&lt;/code&gt; when&lt;/strong&gt; state is simple and its updates are simple. &lt;strong&gt;Consider &lt;code&gt;useReducer&lt;/code&gt; when&lt;/strong&gt; several related state changes happen together and the update logic benefits from living in one place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap: concepts learnt this week
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Custom hooks&lt;/strong&gt; reuse logic and each call gets its own state; &lt;strong&gt;helpers&lt;/strong&gt; are for logic with no hooks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AbortController&lt;/strong&gt; cancels async work; an aborted request shouldn't touch state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composition&lt;/strong&gt; lets a parent wrap content it doesn't know about; extract only when the piece has a distinct responsibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Derived state&lt;/strong&gt;: store the source of truth, derive the rest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context&lt;/strong&gt; shares data across a subtree; avoid it for fast-changing values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;useReducer&lt;/strong&gt; centralizes related state updates; &lt;code&gt;useState&lt;/code&gt; is enough for simple ones.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What I'm enjoying most is that I'm not just learning new React APIs, I'm learning their boundaries: when to reach for something, and when not to. I can't wait to start building realistic frontend applications.&lt;/p&gt;

&lt;p&gt;What's a rule you wish you'd learnt earlier? Tell me in the comments.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React Router Finally Clicked --- Because I Stopped Treating It Like a List of APIs</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Sun, 20 Sep 2026 20:30:58 +0000</pubDate>
      <link>https://dev.to/blackjosh007/the-test-is-working-going-deep-on-react-router-3bdk</link>
      <guid>https://dev.to/blackjosh007/the-test-is-working-going-deep-on-react-router-3bdk</guid>
      <description>&lt;p&gt;A few weeks ago, I set up an experiment for myself:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Could I learn React while building without turning the process into&lt;br&gt;
AI-assisted copy-pasting?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The rule was simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Learn the overview of a concept first.&lt;/li&gt;
&lt;li&gt; Attempt the feature myself.&lt;/li&gt;
&lt;li&gt; Only ask AI for a hint after getting genuinely stuck.&lt;/li&gt;
&lt;li&gt; Follow a pre-planned concept sequence instead of randomly jumping
between topics.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I said I'd test it for a few weeks and switch to a structured course if&lt;br&gt;
it wasn't producing real understanding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict so far: it's holding up.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This week was React Router, and I reached a point I care about more than&lt;br&gt;
simply getting the code working:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I can explain &lt;strong&gt;why&lt;/strong&gt; the pieces exist and how they fit together.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  My initial assumption about routing
&lt;/h2&gt;

&lt;p&gt;Going in, I assumed routing would mean more setup and friction than&lt;br&gt;
using normal HTML links.&lt;/p&gt;

&lt;p&gt;It turned out to be almost the opposite.&lt;/p&gt;

&lt;p&gt;A normal &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag tells the browser to navigate to another document,&lt;br&gt;
which causes a full page load. In a React Single Page Application, we&lt;br&gt;
generally want navigation to happen on the client so the app can change&lt;br&gt;
what it renders without loading an entirely new document.&lt;/p&gt;

&lt;p&gt;That's where React Router comes in.&lt;/p&gt;

&lt;p&gt;The basic pieces started making sense once I understood their jobs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;BrowserRouter&lt;/code&gt; --- provides the routing context for the
application.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;Routes&lt;/code&gt; --- contains the routes the application knows about.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;Route&lt;/code&gt; --- maps a URL pattern to what should be rendered.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;Link&lt;/code&gt; --- provides client-side navigation rather than relying on a
normal document navigation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the API stopped feeling like arbitrary React Router ceremony.&lt;/p&gt;

&lt;p&gt;Each piece had a reason for being there.&lt;/p&gt;
&lt;h2&gt;
  
  
  The first thing that really clicked: dynamic routes
&lt;/h2&gt;

&lt;p&gt;I practiced this by building a small blog.&lt;/p&gt;

&lt;p&gt;The blog had a list of posts, and clicking a post took me to something&lt;br&gt;
like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/blog/42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of creating a separate route for every possible post, I could&lt;br&gt;
define a dynamic route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/blog/:id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then &lt;code&gt;useParams()&lt;/code&gt; gives the component the value from that dynamic part&lt;br&gt;
of the URL.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/blog/42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes something conceptually like:&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="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;42&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I could then use that ID to find the corresponding post and render it.&lt;/p&gt;

&lt;p&gt;That was a useful shift in understanding.&lt;/p&gt;

&lt;p&gt;The URL wasn't just a destination anymore.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It could carry information that the application uses to decide what to&lt;br&gt;
render.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Nested routes + &lt;code&gt;Outlet&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Nested routing was another one that clicked once I saw the relationship&lt;br&gt;
between the parent and child routes.&lt;/p&gt;

&lt;p&gt;For example, imagine a dashboard with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/dashboard
/dashboard/profile
/dashboard/settings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dashboard can provide the shared layout, while the child route gets&lt;br&gt;
rendered into the location provided by &lt;code&gt;Outlet&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dashboard
├── shared navigation
├── shared layout
│
└── &amp;lt;Outlet /&amp;gt;
       ↓
   Profile / Settings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That made much more sense to me than thinking of every route as&lt;br&gt;
completely separate pages.&lt;/p&gt;
&lt;h2&gt;
  
  
  Protected routes --- mental model only
&lt;/h2&gt;

&lt;p&gt;This is one area where I'm deliberately &lt;strong&gt;not&lt;/strong&gt; pretending I've&lt;br&gt;
implemented more than I have.&lt;/p&gt;

&lt;p&gt;I haven't built authentication yet.&lt;/p&gt;

&lt;p&gt;What I did understand was the basic routing mental model: a protected&lt;br&gt;
page such as a dashboard can have a condition that determines whether&lt;br&gt;
the user should be allowed to see it.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             /dashboard
                 ↓
          ┌─────────────┐
          │ Auth check  │
          └─────────────┘
            ↓         ↓
          YES         NO
           ↓           ↓
      Dashboard       Login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the level I reached this week.&lt;/p&gt;

&lt;p&gt;And there's an important distinction I'll need to go deeper into when I&lt;br&gt;
actually learn authentication:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A frontend route guard controls what the user can access in the UI; it&lt;br&gt;
isn't, by itself, a security boundary.&lt;/strong&gt; Actual authorization needs to&lt;br&gt;
be enforced by the backend/server as well.&lt;/p&gt;

&lt;p&gt;For now, I'm happy with understanding the routing side of the mental&lt;br&gt;
model.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bigger lesson wasn't actually routing
&lt;/h2&gt;

&lt;p&gt;The more interesting result from this week wasn't learning &lt;code&gt;useParams&lt;/code&gt;,&lt;br&gt;
&lt;code&gt;Outlet&lt;/code&gt;, or &lt;code&gt;Link&lt;/code&gt; individually.&lt;/p&gt;

&lt;p&gt;It was seeing the learning method work.&lt;/p&gt;

&lt;p&gt;I'm not trying to collect React APIs just so I can say I've used them.&lt;/p&gt;

&lt;p&gt;I'm trying to reach the point where, when I see a piece of React code, I&lt;br&gt;
can explain:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is this here?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What problem does it solve?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does it connect to the rest of the application?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With React Router, I'm starting to get there.&lt;/p&gt;

&lt;p&gt;So the experiment continues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next stop: custom hooks.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>I finished the Tenzies "toy" and immediately hit a wall</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Mon, 14 Sep 2026 13:11:36 +0000</pubDate>
      <link>https://dev.to/blackjosh007/i-finished-the-tenzies-toy-and-immediately-hit-a-wall-20o7</link>
      <guid>https://dev.to/blackjosh007/i-finished-the-tenzies-toy-and-immediately-hit-a-wall-20o7</guid>
      <description>&lt;p&gt;So I wrapped up the basics course, played around with (and extended) the Tenzies app — a tutorial toy, not a real problem, as I keep reminding myself — and figured I was ready to build something real. Something that actually solves a problem, not just proves I can follow along.&lt;/p&gt;

&lt;p&gt;I had an idea. I streamlined it. I felt good about it.&lt;/p&gt;

&lt;p&gt;Then I did something that wrecked that feeling: I pulled up a proper list of React concepts and checked off what I'd actually covered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✅ derived state
✅ forms and controlled inputs
✅ (medium) state management
—  complex component architecture
—  reusable components
—  custom hooks
—  API calls
—  loading/error states
—  routing
—  optimistic updates
—  pagination/filtering/search
—  authentication
—  local persistence
—  performance considerations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three and a half out of fourteen. Omo.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fork in the road
&lt;/h2&gt;

&lt;p&gt;This left me with a real decision, not a vibes-based one: do I stop and take a structured advanced React course, or do I keep building and pick up the missing concepts as the project demands them?&lt;/p&gt;

&lt;p&gt;I was already skeptical of the second option going in — and for a specific reason. If I build while leaning on AI to fill every gap, I could ship something that &lt;em&gt;looks&lt;/em&gt; like my project while understanding almost none of the concepts that made it work. That's not learning, that's assembling.&lt;/p&gt;

&lt;p&gt;So before picking a side, I had to answer a harder question: &lt;strong&gt;what actually stops "learning while building" from turning into "pasting while building"?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's where I landed — the rule I'm holding myself to for this build:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get the overview of a concept first (with or without AI help) — not the implementation, just the shape of the idea.&lt;/li&gt;
&lt;li&gt;Attempt the feature that needs it, myself, before asking anything.&lt;/li&gt;
&lt;li&gt;If I'm stuck after real reasoning and research — not five minutes of staring — &lt;em&gt;then&lt;/em&gt; I bring Claude in, and only for a hint, not the answer.&lt;/li&gt;
&lt;li&gt;The overall sequence of what to learn when is planned out for me, since I don't yet know this territory well enough to sequence it myself.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That last point matters more than it sounds — it's the difference between "I'll learn as I go" (vague, no accountability) and having an actual curriculum sitting underneath an unstructured build.&lt;/p&gt;

&lt;p&gt;I'm treating this as a real test, not a permanent decision. If a few weeks in this path isn't producing real understanding, I'll stop and go take the structured course instead. No shame in switching — just data either way.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick contrast: learning Tailwind was the opposite of this
&lt;/h2&gt;

&lt;p&gt;Small aside, but it's stuck with me. I picked up Tailwind CSS purely by reading the documentation — no tutorials, no courses. It just clicked. If you're learning Tailwind: skip the tutorials, the docs are enough.&lt;/p&gt;

&lt;p&gt;React, at this depth, is not that. And honestly, that gap in difficulty is part of why I needed an actual system before diving in, instead of just winging it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;The project this is all in service of — a progress-tracking app for an accountability group I'm part of — deserves its own post, because the scoping conversation alone was a whole thing. That's coming separately.&lt;/p&gt;

&lt;p&gt;For now: if you've gone through this same fork — course first vs. learn-by-building — I'd genuinely like to hear which way you went and whether you'd choose the same again.&lt;/p&gt;




</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>My Timer Kept Doubling — and It Took Me a Real Bug to Actually Understand useEffect</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Fri, 04 Sep 2026 00:28:21 +0000</pubDate>
      <link>https://dev.to/blackjosh007/my-timer-kept-doubling-and-it-took-me-a-real-bug-to-actually-understand-useeffect-21o9</link>
      <guid>https://dev.to/blackjosh007/my-timer-kept-doubling-and-it-took-me-a-real-bug-to-actually-understand-useeffect-21o9</guid>
      <description>&lt;p&gt;I finished my Tenzies capstone project (built with React) and I want to talk about the one bug that taught me more than any tutorial did.&lt;/p&gt;

&lt;p&gt;🔗 Live demo: tenzies-six-silk.vercel.app&lt;br&gt;
🔗 Code: github.com/BlackJosh007/tenzies&lt;/p&gt;

&lt;p&gt;I was adding a timer — track how long it takes a player to win. Simple feature, I thought. I set up &lt;code&gt;useState&lt;/code&gt; for the elapsed time, &lt;code&gt;useRef&lt;/code&gt; for the interval ID, and a &lt;code&gt;useEffect&lt;/code&gt; to start the interval once the player held their first die.&lt;/p&gt;

&lt;p&gt;It worked... sometimes. Other times my timer would double up out of nowhere, ticking twice as fast. And at one point, after a few touches, it stopped running entirely.&lt;/p&gt;

&lt;p&gt;Here's why.&lt;/p&gt;

&lt;h3&gt;
  
  
  The wrong belief I was carrying
&lt;/h3&gt;

&lt;p&gt;I thought the cleanup function in &lt;code&gt;useEffect&lt;/code&gt; only ran once — when the component unmounted. That's what I'd taken away from the tutorial. Knowledge, filed away, never tested.&lt;/p&gt;

&lt;p&gt;What actually happens: the cleanup function runs before &lt;em&gt;every&lt;/em&gt; re-run of the effect, not just on unmount. Every time your dependency changes, React cleans up the previous effect first, then runs the new one.&lt;/p&gt;

&lt;p&gt;I didn't have that cleanup wired in properly. So every time my dependency array changed, a new &lt;code&gt;setInterval&lt;/code&gt; was created — without the old one being cleared. Two intervals running at once. Then three. My timer wasn't buggy, it was doing exactly what I told it to do.&lt;/p&gt;

&lt;h3&gt;
  
  
  It got worse before it got better
&lt;/h3&gt;

&lt;p&gt;My first instinct for triggering the timer was checking &lt;code&gt;diceNo.includes({ isHeld: true })&lt;/code&gt; inside the effect, with &lt;code&gt;seconds&lt;/code&gt; as my dependency array. That's a double mistake: &lt;code&gt;.includes&lt;/code&gt; doesn't do the deep comparison I wanted (switched to &lt;code&gt;.some(die =&amp;gt; die.isHeld)&lt;/code&gt; later), and using &lt;code&gt;seconds&lt;/code&gt; as the dependency meant the effect was re-running every single tick — re-arming a new interval on top of the old one, every second.&lt;/p&gt;

&lt;p&gt;I also had a second wrong belief here: I assumed that if I wrapped my interval logic in an &lt;code&gt;if&lt;/code&gt; statement inside the effect, only the code &lt;em&gt;inside&lt;/em&gt; the &lt;code&gt;if&lt;/code&gt; block counted as "the effect." Not true — the entire function body passed to &lt;code&gt;useEffect&lt;/code&gt; is the effect, condition or no condition. React doesn't care what's inside your &lt;code&gt;if&lt;/code&gt;; it re-runs the whole thing whenever the dependency changes.&lt;/p&gt;

&lt;p&gt;Once I fixed the dependency array (moved to &lt;code&gt;diceNo&lt;/code&gt;, gated by a &lt;code&gt;timerStarted&lt;/code&gt; ref so the interval only ever arms once) and got the cleanup returning &lt;code&gt;clearInterval(intervalRef.current)&lt;/code&gt; properly, the doubling stopped.&lt;/p&gt;

&lt;h3&gt;
  
  
  The bonus realization
&lt;/h3&gt;

&lt;p&gt;While staring at &lt;code&gt;setInterval(() =&amp;gt; setMilliSeconds(prev =&amp;gt; prev + 10), 10)&lt;/code&gt;, I genuinely wondered if React was re-rendering every 10ms because of it. Turns out — no. &lt;code&gt;setInterval&lt;/code&gt; is handed off to the browser, not React. React only re-renders when the state update inside the callback actually fires, and it's the state update — not the timer itself — that's on React's clock.&lt;/p&gt;

&lt;h3&gt;
  
  
  Knowledge vs. understanding
&lt;/h3&gt;

&lt;p&gt;I'd "learned" useEffect and its dependency array from a tutorial before this. I could've told you the definition. But I didn't &lt;em&gt;understand&lt;/em&gt; it until a real bug forced me to. That's the difference — knowledge is something you can repeat, understanding is something you can debug.&lt;/p&gt;

&lt;h3&gt;
  
  
  One thing I changed my mind on
&lt;/h3&gt;

&lt;p&gt;Last post, I said I wanted to add full keyboard navigation to this game. I'm not doing that anymore. Tenzies is an abstract, casual game — realistically, almost everyone playing it will be on a phone, not a desktop with a keyboard. Building out keyboard nav for a demographic that mostly won't use it didn't make sense once I actually thought about who's playing. Instead, I put that time into a timer, roll counter, restart button, and a highscore overlay — features that matter regardless of device.&lt;/p&gt;

&lt;p&gt;Onto the next build.&lt;/p&gt;

</description>
      <category>buildinpublic</category>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title># I Shipped a Working Win-Check... That Could Be Wrong 9 Times Out of 10</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Sat, 29 Aug 2026 20:40:51 +0000</pubDate>
      <link>https://dev.to/blackjosh007/-i-shipped-a-working-win-check-that-could-be-wrong-9-times-out-of-10-48b0</link>
      <guid>https://dev.to/blackjosh007/-i-shipped-a-working-win-check-that-could-be-wrong-9-times-out-of-10-48b0</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs3juzenj94p4lmzv7g3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs3juzenj94p4lmzv7g3w.png" alt=" " width="800" height="769"&gt;&lt;/a&gt;While building &lt;strong&gt;Tenzies&lt;/strong&gt; (my React capstone from freeCodeCamp), I hit a problem that looked simple on the surface: check if the player won.&lt;/p&gt;

&lt;p&gt;The rule is easy to say out loud: &lt;em&gt;all 10 dice must be held, and all 10 must show the same value.&lt;/em&gt; Turning that into code taught me more about array logic than any tutorial had so far — mostly because my first version had a bug I didn't even know to look for.&lt;/p&gt;

&lt;h2&gt;
  
  
  My First Attempt
&lt;/h2&gt;

&lt;p&gt;I reached for a &lt;code&gt;for&lt;/code&gt; loop and a counter:&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;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;isHeld&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;you won&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic, in my head: compare each die to the &lt;em&gt;next&lt;/em&gt; die. If they match in value and the current one is held, count it. With 10 dice, there are 9 pairs — so if all 9 comparisons pass, count hits 9, and that means everyone matches. Right?&lt;/p&gt;

&lt;p&gt;It worked. I tested it, saw "you won" print at the right time, and felt genuinely proud — until I looked at the instructor's solution and saw the whole thing done in three lines with &lt;code&gt;.every()&lt;/code&gt;. My first reaction was honestly "that feels like cheating." My second, more useful reaction was: &lt;em&gt;wait, is my version even correct?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bug I Almost Missed
&lt;/h2&gt;

&lt;p&gt;Look closely at the loop bounds: &lt;code&gt;i &amp;lt; diceNo.length - 1&lt;/code&gt;. With 10 dice, &lt;code&gt;i&lt;/code&gt; runs from 0 to 8. The &lt;code&gt;isHeld&lt;/code&gt; check only ever runs on &lt;code&gt;diceNo[i]&lt;/code&gt; — &lt;strong&gt;the last die, at index 9, never gets its &lt;code&gt;isHeld&lt;/code&gt; checked at all.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So picture this scenario: dice 0 through 8 are all held and all show a 4. Die 9 shows a 4 too, purely by chance, but the player never held it. My loop would still count 9 matches and declare a win — even though one die was never actually locked in by the player. The bug isn't loud; it only shows up in a specific edge case, which is exactly why I didn't catch it just by playing the game normally.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fix
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;gameWon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;die&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;die&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isHeld&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;die&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;die&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;diceNo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;gameWon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two separate, symmetric checks: &lt;em&gt;are all 10 held?&lt;/em&gt; and &lt;em&gt;do all 10 match the first die's value?&lt;/em&gt; Every die gets checked, every time, with no off-by-one gap hiding at the edges. That's the real reason this version is better — not brevity for its own sake, but that it's structurally impossible to skip a die.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Lesson
&lt;/h2&gt;

&lt;p&gt;The takeaway for me isn't "loops bad, array methods good." It's this: &lt;strong&gt;when a loop's bounds don't map 1:1 to "check every item," ask what you're actually excluding — and whether that exclusion is intentional.&lt;/strong&gt; &lt;code&gt;.every()&lt;/code&gt; forced correctness by construction. My loop left a gap I had to go looking for.&lt;/p&gt;

&lt;p&gt;Also — I've officially learned to check whether a built-in method already solves my problem &lt;em&gt;before&lt;/em&gt; I hand-roll a loop and a counter. Lesson paid for in stress, but paid.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;I'm planning to extend the game with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A timer + roll counter, to track how fast you can win&lt;/li&gt;
&lt;li&gt;Dice styled with actual pips instead of numbers&lt;/li&gt;
&lt;li&gt;Full keyboard navigation for accessibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Live demo:&lt;/strong&gt; &lt;a href="https://tenzies-six-silk.vercel.app/" rel="noopener noreferrer"&gt;https://tenzies-six-silk.vercel.app/&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Repo:&lt;/strong&gt; &lt;a href="https://github.com/BlackJosh007/tenzies" rel="noopener noreferrer"&gt;https://github.com/BlackJosh007/tenzies&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Wait, Square Brackets Aren't Just for Arrays? — A Controlled Component Gotcha</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Fri, 21 Aug 2026 15:21:08 +0000</pubDate>
      <link>https://dev.to/blackjosh007/wait-square-brackets-arent-just-for-arrays-a-controlled-component-gotcha-539m</link>
      <guid>https://dev.to/blackjosh007/wait-square-brackets-arent-just-for-arrays-a-controlled-component-gotcha-539m</guid>
      <description>&lt;p&gt;While building a meme generator with React (following freeCodeCamp), I hit a moment where my mental model of JavaScript syntax completely betrayed me — and untangling it taught me something I won't forget.&lt;/p&gt;

&lt;p&gt;I was building a controlled component: two text inputs (top text, bottom text) that update state as the user types. Here's the handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTarget&lt;/span&gt;

    &lt;span class="nf"&gt;setMemeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevMeme&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;prevMeme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;[name]: value&lt;/code&gt; line stopped me cold. In my head, square brackets meant one thing: arrays. So my brain tried to read this as some kind of array operation happening &lt;em&gt;inside&lt;/em&gt; an object spread, and none of it made sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was actually going on
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;[name]&lt;/code&gt; is a &lt;strong&gt;computed property name&lt;/strong&gt;. &lt;code&gt;name&lt;/code&gt; is a variable holding a string — in this case, either &lt;code&gt;"topText"&lt;/code&gt; or &lt;code&gt;"bottomText"&lt;/code&gt; (pulled from the input's &lt;code&gt;name&lt;/code&gt; attribute). Wrapping it in square brackets tells JavaScript: "don't use the literal word &lt;code&gt;name&lt;/code&gt; as the key — evaluate this variable and use &lt;em&gt;its value&lt;/em&gt; as the key instead."&lt;/p&gt;

&lt;p&gt;Without the brackets, &lt;code&gt;{ ...prevMeme, name: value }&lt;/code&gt; would spread the previous state and then literally add a property called &lt;code&gt;name&lt;/code&gt; set to whatever was typed. That's not what I wanted — I wanted the actual key to be &lt;code&gt;topText&lt;/code&gt; or &lt;code&gt;bottomText&lt;/code&gt;, depending on which input fired the event. The brackets are the difference between "the key is the word &lt;code&gt;name&lt;/code&gt;" and "the key is &lt;em&gt;whatever &lt;code&gt;name&lt;/code&gt; currently equals&lt;/em&gt;."&lt;/p&gt;

&lt;p&gt;One line, one small syntax convention, and suddenly one function could handle both inputs instead of writing a separate handler for each.&lt;/p&gt;

&lt;h2&gt;
  
  
  Full context — the input side
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"topText"&lt;/span&gt;
    &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;memeText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;topText&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;name="topText"&lt;/code&gt; on the JSX element is what flows into &lt;code&gt;event.currentTarget.name&lt;/code&gt; — which is then the value that &lt;code&gt;[name]&lt;/code&gt; computes from. Once I traced that full loop — JSX attribute → event object → destructured variable → computed key — it clicked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;If square brackets in an object literal ever throw you off, ask whether you're looking at a computed property name rather than an array. It's a small piece of syntax that does a lot of quiet work.&lt;/p&gt;

&lt;p&gt;Still working through the freeCodeCamp React course — challenge sections left before I move on to real project builds.&lt;/p&gt;

&lt;p&gt;🔗 Code: [&lt;a href="https://github.com/BlackJosh007/chef-claude-app" rel="noopener noreferrer"&gt;https://github.com/BlackJosh007/chef-claude-app&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The React Concept That Broke My Brain (And Why It Was Actually Simple)</title>
      <dc:creator>BlackJosh007</dc:creator>
      <pubDate>Tue, 28 Jul 2026 23:53:11 +0000</pubDate>
      <link>https://dev.to/blackjosh007/the-react-concept-that-broke-my-brain-and-why-it-was-actually-simple-22km</link>
      <guid>https://dev.to/blackjosh007/the-react-concept-that-broke-my-brain-and-why-it-was-actually-simple-22km</guid>
      <description>&lt;p&gt;I almost skipped past this. It looked like a small refactor. It ended up being the first time React's "one source of truth" idea actually clicked for me instead of just being a phrase I nodded along to.&lt;/p&gt;

&lt;p&gt;Here's the project: a grid of colored pads that toggle on/off when clicked. Simple UI, simple idea.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I started: derived state
&lt;/h2&gt;

&lt;p&gt;My first instinct was to create state based on data that lived somewhere else in the component tree — what's usually called &lt;em&gt;derived state&lt;/em&gt;. It technically worked. Buttons rendered, colors showed up. But something about it felt fragile, and I couldn't articulate why yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I ended up: one source of truth
&lt;/h2&gt;

&lt;p&gt;The fix was to move the actual state up into &lt;code&gt;App.jsx&lt;/code&gt;, and have everything else just receive data and functions as props. My folder looks like this:&lt;br&gt;
&lt;strong&gt;1. &lt;code&gt;App.jsx&lt;/code&gt;&lt;/strong&gt; — holds the single source of truth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;pads&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPads&lt;/span&gt;&lt;span class="p"&gt;]&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;padsData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setPads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevPads&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevPads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;idR&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;
    &lt;span class="p"&gt;}))&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonElements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pad&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Pad&lt;/span&gt;
      &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;toggleFunc&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;))&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"pad-container"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;buttonElements&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;pads.js&lt;/code&gt;&lt;/strong&gt; — the raw data the state is initialized from:&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#F18D8B&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#F5C280&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;Pad.jsx&lt;/code&gt;&lt;/strong&gt; — a dumb component that just renders and reports back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Pad&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toggleFunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
      &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;on&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;toggleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The part that actually confused me
&lt;/h3&gt;

&lt;p&gt;Not the state move itself — that part I could follow. What genuinely took time to reconcile was &lt;strong&gt;passing a function as a prop and tracking two different names for the same value&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;Pad.jsx&lt;/code&gt;: &lt;code&gt;onClick={() =&amp;gt; toggleFunc(id)}&lt;/code&gt; — here, &lt;code&gt;id&lt;/code&gt; is the &lt;em&gt;real&lt;/em&gt; value, the actual id of the button that got clicked.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;App.jsx&lt;/code&gt;: &lt;code&gt;function toggle(idR)&lt;/code&gt; — here, &lt;code&gt;idR&lt;/code&gt; is just a &lt;em&gt;placeholder name&lt;/em&gt;, a parameter waiting to receive whatever value gets passed in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I kept reading &lt;code&gt;idR&lt;/code&gt; like it meant something special, like it was doing extra work. It isn't. It's just a name. The moment I stopped trying to find hidden meaning in &lt;code&gt;idR&lt;/code&gt; and just traced the value — click happens → &lt;code&gt;id&lt;/code&gt; gets passed → &lt;code&gt;toggle&lt;/code&gt; receives it as &lt;code&gt;idR&lt;/code&gt; → &lt;code&gt;idR&lt;/code&gt; gets compared against each item in state — the whole thing clicked into place.&lt;/p&gt;

&lt;h3&gt;
  
  
  The full loop, in plain language
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;App.jsx&lt;/code&gt; holds &lt;code&gt;pads&lt;/code&gt; state, seeded from &lt;code&gt;padsData&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It maps over &lt;code&gt;pads&lt;/code&gt; and renders a &lt;code&gt;Pad&lt;/code&gt; for each one, passing down &lt;code&gt;color&lt;/code&gt;, &lt;code&gt;on&lt;/code&gt;, &lt;code&gt;id&lt;/code&gt;, and the &lt;code&gt;toggle&lt;/code&gt; function itself.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Pad.jsx&lt;/code&gt; attaches &lt;code&gt;toggle&lt;/code&gt; to its button's &lt;code&gt;onClick&lt;/code&gt;, wrapped in an arrow function so it can pass along &lt;em&gt;which&lt;/em&gt; button was clicked.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;toggle&lt;/code&gt; receives that id (as &lt;code&gt;idR&lt;/code&gt;), and calls &lt;code&gt;setPads&lt;/code&gt;, mapping over the previous state and flipping &lt;code&gt;on&lt;/code&gt; only for the item whose id matches.&lt;/li&gt;
&lt;li&gt;React re-renders with the updated state, and only the clicked button's color state changes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why this matters more than it looks like
&lt;/h3&gt;

&lt;p&gt;The bug I would've eventually hit with derived state is the classic one: two sources of truth drifting out of sync. Lifting the state up removes that possibility entirely — there's exactly one place the "real" data lives, and everything else is just a reflection of it.&lt;/p&gt;

&lt;p&gt;This is day-something of learning React in public, building toward real projects instead of just finishing tutorials. If you've hit this same wall with props/state, I'd like to hear how you reasoned through it.&lt;/p&gt;

&lt;p&gt;Repo's on GitHub if you want to see the full thing — link in my profile.&lt;/p&gt;

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