<?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: nayem</title>
    <description>The latest articles on DEV Community by nayem (@nayemalam).</description>
    <link>https://dev.to/nayemalam</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%2F4225430%2Fb11a9e18-039e-40b4-a542-94b102adcda2.jpeg</url>
      <title>DEV Community: nayem</title>
      <link>https://dev.to/nayemalam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nayemalam"/>
    <language>en</language>
    <item>
      <title>this kept breaking my code, so i learned binding</title>
      <dc:creator>nayem</dc:creator>
      <pubDate>Thu, 05 Nov 2026 05:35:10 +0000</pubDate>
      <link>https://dev.to/nayemalam/this-kept-breaking-my-code-so-i-learned-binding-45k1</link>
      <guid>https://dev.to/nayemalam/this-kept-breaking-my-code-so-i-learned-binding-45k1</guid>
      <description>&lt;p&gt;Early in my first React job, I built a class component with a button. Click the button, the counter goes up. Very simple, maybe 20 lines. I clicked it and got &lt;code&gt;TypeError: Cannot read properties of undefined&lt;/code&gt;. I stared at that error for a solid 40 minutes before the senior dev on my team walked over, typed &lt;code&gt;.bind(this)&lt;/code&gt; in the constructor, and walked away like a surgeon closing an incision. I had no idea why it worked. I just shipped it and moved on.&lt;/p&gt;

&lt;p&gt;For about two years after that, I treated binding like a superstition. See a class method, bind it, ask questions never. (It worked every time, which is the most dangerous kind of not understanding.) Anyways, this post is the explanation I wish someone had given me back then, plus the arrow-function-era update.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one rule about &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; is decided by how a function is &lt;em&gt;called&lt;/em&gt;, not where it is written. Read that again, because it is the whole post. When you write &lt;code&gt;user.greet()&lt;/code&gt;, JavaScript sees the dot at the call site and sets &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; to &lt;code&gt;user&lt;/code&gt;. When you write &lt;code&gt;const f = user.greet; f()&lt;/code&gt;, there is no dot at the call site, so &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; is &lt;code&gt;undefined&lt;/code&gt; (in strict mode, which is every module you will write in 2026). Detaching a method from its object strips its &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; off. That is it. That is the bug, every time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;the-trap.js&lt;/strong&gt;&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;user&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="s1"&gt;Nayem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;greet&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi, I am &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&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="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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// "Hi, I am Nayem". The dot does the work.&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greetAlone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&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="nf"&gt;greetAlone&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// TypeError. No dot, no this.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Live: the same function, two call styles&lt;/strong&gt;&lt;br&gt;
Try it live on &lt;a href="https://nayemalam.com/blog/this-binding-javascript" rel="noopener noreferrer"&gt;nayemalam.com&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Try it. Same function, same object. One button calls it with the dot, one calls it without. The error message is the one that used to ruin my afternoon.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; bites you in real code
&lt;/h2&gt;

&lt;p&gt;It is almost never the object-literal case that gets you. It is the callback case, where the function gets detached from its object silently, three files away from where the bug shows up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Event handlers.&lt;/strong&gt;&lt;code&gt;onClick={this.handleClick}&lt;/code&gt; hands React the function without the object. React calls it later, dotless.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Timers.&lt;/strong&gt;&lt;code&gt;setTimeout(user.save, 1000)&lt;/code&gt; does the same thing, just with a delay, so the stack trace arrives when you have already forgotten what you wrote.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Array methods.&lt;/strong&gt;&lt;code&gt;items.map(this.renderItem)&lt;/code&gt; passes the method along and &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; never makes the trip.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice the pattern. You never wrote a bad function. You wrote a good function and then handed it to someone else, and the handoff dropped the &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt;. (Like lending a friend your car without the keys. The car is fine.)&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;&lt;code&gt;bind&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;call&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;apply&lt;/code&gt;&lt;/em&gt; (the old school fixes)
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;bind&lt;/code&gt;&lt;/em&gt; returns a new function with &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; locked in. &lt;em&gt;&lt;code&gt;call&lt;/code&gt;&lt;/em&gt; and &lt;em&gt;&lt;code&gt;apply&lt;/code&gt;&lt;/em&gt; run the function immediately with the &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; you hand them (&lt;em&gt;&lt;code&gt;apply&lt;/code&gt;&lt;/em&gt; takes an array of arguments, &lt;em&gt;&lt;code&gt;call&lt;/code&gt;&lt;/em&gt; takes them one by one). The full reference lives on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;, but here is the 30-second version:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;bind-call-apply.js&lt;/strong&gt;&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;user&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="s1"&gt;Nayem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&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="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;, I am &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// bind: lock this in, call the result whenever&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;bound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "Hello, I am Nayem"&lt;/span&gt;

&lt;span class="c1"&gt;// call: run it once, right now, with this set&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&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="s1"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "Hey, I am Alex"&lt;/span&gt;

&lt;span class="c1"&gt;// apply: same as call, but arguments arrive as an array&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&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="s1"&gt;Alex&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Yo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// "Yo, I am Alex"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why arrow functions made most of &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; go away
&lt;/h2&gt;

&lt;p&gt;Arrow functions have no &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; of their own. They borrow it from where they are written, permanently. So &lt;code&gt;onClick={() =&amp;gt; this.handleClick()}&lt;/code&gt; keeps the dot at the call site, and &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; survives. Same with &lt;code&gt;setTimeout(() =&amp;gt; user.save(), 1000)&lt;/code&gt;. This is why the whole binding problem quietly faded when everyone moved to function components and hooks: we just stopped writing the code that triggers it.&lt;/p&gt;

&lt;p&gt;One gotcha survives, though. Arrow functions make bad object methods, because their &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; comes from the surrounding scope, not the object:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;arrow-method-trap.js&lt;/strong&gt;&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;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&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;// arrow function: this is the module scope, not counter&lt;/span&gt;
  &lt;span class="na"&gt;tick&lt;/span&gt;&lt;span class="p"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&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="c1"&gt;// TypeError. Surprise.&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// regular method: the dot does the work, as usual&lt;/span&gt;
  &lt;span class="nf"&gt;tickFixed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&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="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 rule of thumb I actually use: arrow functions for callbacks, regular methods for objects. Each one puts &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; where you expect it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The React version of this story
&lt;/h2&gt;

&lt;p&gt;This is where my 40-minute stare-down happened. Class components need every handler bound, because &lt;code&gt;onClick={this.handleClick}&lt;/code&gt; is exactly the detaching-a-method case from above. The classic fix is one line in the constructor:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;class-component-bind.jsx&lt;/strong&gt;&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;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&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;// the line that haunted me for two years&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&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;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="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&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;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&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;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times
      &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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The shorter modern version is an arrow function as a class property, which keeps &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; as the component automatically because arrow functions borrow it from where they were written:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;class-property-arrow.jsx&lt;/strong&gt;&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;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&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;// arrow function, so this is the component. No bind needed.&lt;/span&gt;
  &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&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;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="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&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;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&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;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times
      &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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  So, what have I learnt?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Look at the call, not the definition. If there is a dot before the function name when it runs, &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; is the thing before the dot. If not, &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; is gone.&lt;/li&gt;
&lt;li&gt;A method passed as a callback arrives with its &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; stripped off. That handoff is where almost every one of these bugs lives.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;bind&lt;/code&gt;&lt;/em&gt; locks &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; in for later, &lt;em&gt;&lt;code&gt;call&lt;/code&gt;&lt;/em&gt; and &lt;em&gt;&lt;code&gt;apply&lt;/code&gt;&lt;/em&gt; run once with the &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; you give them, and arrow functions borrow &lt;em&gt;&lt;code&gt;this&lt;/code&gt;&lt;/em&gt; from where they were written.&lt;/li&gt;
&lt;li&gt;I cargo-culted &lt;code&gt;.bind(this)&lt;/code&gt; in constructors for two years without knowing what it did, and the code worked the whole time. Understanding it did not make my code better. It just means I can read old class components without that lowkey dread, which, honestly, is the entire payoff.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://nayemalam.com/blog/this-binding-javascript" rel="noopener noreferrer"&gt;nayemalam.com/blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>easily understand flexbox</title>
      <dc:creator>nayem</dc:creator>
      <pubDate>Thu, 05 Nov 2026 05:30:41 +0000</pubDate>
      <link>https://dev.to/nayemalam/easily-understand-flexbox-p4h</link>
      <guid>https://dev.to/nayemalam/easily-understand-flexbox-p4h</guid>
      <description>&lt;p&gt;For the first three years I wrote CSS, &lt;em&gt;&lt;code&gt;flexbox&lt;/code&gt;&lt;/em&gt; and I had an arrangement. I would type &lt;code&gt;display: flex&lt;/code&gt;, then guess at &lt;em&gt;&lt;code&gt;justify-content&lt;/code&gt;&lt;/em&gt; and &lt;em&gt;&lt;code&gt;align-items&lt;/code&gt;&lt;/em&gt; until the boxes landed somewhere reasonable. It worked about 70 percent of the time. The other 30 percent I sat in devtools toggling properties like a slot machine, hoping for three boxes in a row. (You know the feeling. Everyone knows the feeling.)&lt;/p&gt;

&lt;p&gt;Turns out the whole thing is two axes and a handful of properties. This is the post I wish existed back then, with pictures, because flexbox is one of those things that clicks visually or not at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  The only mental model you need
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;flexbox&lt;/code&gt;&lt;/em&gt; lays things out along two axes: the main axis and the cross axis. Every property you are about to learn answers one of two questions. Where do things go along the main axis? Where do they go along the cross axis? That is genuinely all of it.&lt;/p&gt;

&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%2F5e09c6pro05xpjx3u3xg.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%2F5e09c6pro05xpjx3u3xg.png" alt="Diagram of a flex container showing the horizontal main axis and the vertical cross axis" width="799" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;em&gt;&lt;code&gt;display&lt;/code&gt;&lt;/em&gt; switch
&lt;/h2&gt;

&lt;p&gt;One line turns any container into a flex container, and its children line up in a row, left to right:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;flex-basics.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;flex&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 is the on switch. Everything below only works once this line exists, which is worth saying because I once spent twenty minutes wondering why &lt;em&gt;&lt;code&gt;justify-content&lt;/code&gt;&lt;/em&gt; was doing nothing. It was doing nothing because I forgot the switch.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;&lt;code&gt;flex-direction&lt;/code&gt;&lt;/em&gt; flips the axes
&lt;/h2&gt;

&lt;p&gt;By default the main axis runs left to right (&lt;code&gt;row&lt;/code&gt;). Set &lt;em&gt;&lt;code&gt;flex-direction&lt;/code&gt;&lt;/em&gt; to &lt;code&gt;column&lt;/code&gt; and the main axis runs top to bottom instead. The axes flip, which means &lt;em&gt;&lt;code&gt;justify-content&lt;/code&gt;&lt;/em&gt; and &lt;em&gt;&lt;code&gt;align-items&lt;/code&gt;&lt;/em&gt; swap jobs. Forgetting this is why centering "randomly breaks" when you switch a layout from row to column.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;flex-direction.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* main axis is now vertical */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;em&gt;&lt;code&gt;justify-content&lt;/code&gt;&lt;/em&gt; runs the main axis
&lt;/h2&gt;

&lt;p&gt;This is the one everyone mixes up, so here are all six values doing the same job on the same three boxes:&lt;/p&gt;

&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%2Fdmy4m0iqxfmokloci2j4.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%2Fdmy4m0iqxfmokloci2j4.png" alt="Diagram showing the six justify-content values: flex-start, flex-end, center, space-between, space-around, space-evenly" width="800" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In practice you will use three of these for 95 percent of layouts: &lt;code&gt;flex-start&lt;/code&gt;, &lt;code&gt;center&lt;/code&gt;, and &lt;code&gt;space-between&lt;/code&gt;. Learn those cold, look up the rest when you need them.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;&lt;code&gt;align-items&lt;/code&gt;&lt;/em&gt; runs the cross axis
&lt;/h2&gt;

&lt;p&gt;Same idea, other direction. The default is &lt;code&gt;stretch&lt;/code&gt;, which is why your boxes fill the container height and you never questioned it:&lt;/p&gt;

&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%2Fuptztlo96tzqvw0so6fz.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%2Fuptztlo96tzqvw0so6fz.png" alt="Diagram showing the four align-items values: stretch, flex-start, flex-end, center" width="800" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The centering trick, finally explained
&lt;/h2&gt;

&lt;p&gt;The most googled CSS snippet of all time, and now you can read it instead of copying it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;center.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.center&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* center along the main axis */&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c"&gt;/* center along the cross axis */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Center on one axis, center on the other. Done. No absolute positioning, no transform hacks, no margin auto prayer.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;&lt;code&gt;gap&lt;/code&gt;&lt;/em&gt; killed the margin hacks
&lt;/h2&gt;

&lt;p&gt;Remember putting &lt;code&gt;margin-right&lt;/code&gt; on every child except the last one? &lt;em&gt;&lt;code&gt;gap&lt;/code&gt;&lt;/em&gt; does it properly, with even spacing that does not collapse or double up:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;gap.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;gap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* space between children, nothing else */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Try it live
&lt;/h2&gt;

&lt;p&gt;Same three boxes. Flip the direction, move them along each axis, watch what changes. This is faster than reading my explanation a second time:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Live: flexbox playground&lt;/strong&gt;&lt;br&gt;
Try it live on &lt;a href="https://nayemalam.com/blog/easily-understand-flexbox" rel="noopener noreferrer"&gt;nayemalam.com&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  So, what have I learnt?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;flexbox&lt;/code&gt;&lt;/em&gt; is two axes. Name the axis before you reach for a property and half the confusion disappears.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;justify-content&lt;/code&gt;&lt;/em&gt; is the main axis, &lt;em&gt;&lt;code&gt;align-items&lt;/code&gt;&lt;/em&gt; is the cross axis. Mixing those two up was 90 percent of my problem.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;flex-direction&lt;/code&gt;&lt;/em&gt;: &lt;code&gt;column&lt;/code&gt; flips the axes, which is why centering breaks when you change direction.&lt;/li&gt;
&lt;li&gt;Centering is two lines and now I know which two and why.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;gap&lt;/code&gt;&lt;/em&gt; over margins, every single time.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://nayemalam.com/blog/easily-understand-flexbox" rel="noopener noreferrer"&gt;nayemalam.com/blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
