<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Byte-Sized Coding]]></title><description><![CDATA[Byte-Sized Coding]]></description><link>https://bytesizedcoding.com</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Apr 2026 10:08:52 GMT</lastBuildDate><atom:link href="https://bytesizedcoding.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Install NPM Package from a Git Repo]]></title><description><![CDATA[Did you know you can npm install from a Git Repository?
I didn't.
I started out as a backend Java engineer before transitioning to full-stack as my role with my company at the time grew.  Today, I am heavily front-end leaning. Actually, I work almost...]]></description><link>https://bytesizedcoding.com/install-npm-package-from-a-git-repo</link><guid isPermaLink="true">https://bytesizedcoding.com/install-npm-package-from-a-git-repo</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[npm]]></category><category><![CDATA[Yarn]]></category><dc:creator><![CDATA[Zachary Wagner]]></dc:creator><pubDate>Tue, 08 Dec 2020 22:46:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1607467115445/I7arpMeP-.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Did you know you can <code>npm install</code> from a Git Repository?</p>
<p>I didn't.</p>
<p>I started out as a backend Java engineer before transitioning to full-stack as my role with my company at the time grew.  Today, I am heavily front-end leaning. Actually, I work almost exclusively on the front-end.</p>
<p>During my transition to full-stack I was introduced to the npm package manager, but only what I needed to know at the time - <code>npm install</code>, <code>npm uninstall</code>, <code>npm update</code> -- heck, even installing specific versions!  Year in and year out, that was all I really needed.</p>
<h2 id="what-changed">What Changed?</h2>
<p>At work, we were transitioning to React single-page applications.  Initially, we only needed one.  Then, we were on our third and we planned to create more.  Our process for starting a new one was to copy an existing SPA and then strip out the app-specific code to get back to a base setup, then start building.</p>
<p>Not ideal.</p>
<p>In my free time, I set out to create a base project repo that we could clone for our new SPAs.  Creating the base project was easy enough, but why stop there?  I decided to take it a step further and create a custom CLI for generating base projects.</p>
<p>Now, I had to make this CLI available to engineers at the company.  But how?  I couldn't publish it to the npm registry as the base project it used contained proprietary code.  So, now what?</p>
<h2 id="npm-install-to-the-rescue">npm install to the rescue</h2>
<p>A wise colleague of mine mentioned I could just <code>npm install</code> from the private CLI repo!  After learning this, I finally dug into the documentation, specifically for <a href="https://docs.npmjs.com/cli/install" target="_blank">npm install</a>.  From there we see:</p>
<pre><code>npm <span class="hljs-keyword">install</span> (<span class="hljs-keyword">with</span> <span class="hljs-keyword">no</span> args, <span class="hljs-keyword">in</span> <span class="hljs-keyword">package</span> dir)
npm <span class="hljs-keyword">install</span> [&lt;@<span class="hljs-keyword">scope</span>&gt;/]&lt;<span class="hljs-keyword">name</span>&gt;
npm <span class="hljs-keyword">install</span> [&lt;@<span class="hljs-keyword">scope</span>&gt;/]&lt;<span class="hljs-keyword">name</span>&gt;@&lt;tag&gt;
npm <span class="hljs-keyword">install</span> [&lt;@<span class="hljs-keyword">scope</span>&gt;/]&lt;<span class="hljs-keyword">name</span>&gt;@&lt;<span class="hljs-keyword">version</span>&gt;
npm <span class="hljs-keyword">install</span> [&lt;@<span class="hljs-keyword">scope</span>&gt;/]&lt;<span class="hljs-keyword">name</span>&gt;@&lt;<span class="hljs-keyword">version</span> <span class="hljs-keyword">range</span>&gt;
npm <span class="hljs-keyword">install</span> &lt;git-host&gt;:&lt;git-<span class="hljs-keyword">user</span>&gt;/&lt;repo-<span class="hljs-keyword">name</span>&gt;
npm <span class="hljs-keyword">install</span> &lt;git repo <span class="hljs-keyword">url</span>&gt;
npm <span class="hljs-keyword">install</span> &lt;tarball <span class="hljs-keyword">file</span>&gt;
npm <span class="hljs-keyword">install</span> &lt;tarball <span class="hljs-keyword">url</span>&gt;
npm <span class="hljs-keyword">install</span> &lt;folder&gt;

<span class="hljs-keyword">alias</span>: npm i
common options: [-P|<span class="hljs-comment">--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]</span>
</code></pre><p>As you can see, there are plenty of ways to use <code>npm install</code>.  I'd like to specifically point out:</p>
<pre><code>npm install <span class="hljs-tag">&lt;<span class="hljs-name">git</span> <span class="hljs-attr">repo</span> <span class="hljs-attr">url</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">git</span>&gt;</span>
</code></pre><p>Just beautiful.</p>
<p>Now, for an engineer at the company to install the CLI I created, they only needed to run:</p>
<pre><code>npm <span class="hljs-keyword">install</span> -g &lt;<span class="hljs-keyword">private</span> CLI git repo <span class="hljs-keyword">url</span>&gt;
</code></pre><p>or</p>
<pre><code>npx &lt;<span class="hljs-keyword">private</span> CLI git repo url&gt;
</code></pre><p>After that, they were free to use the CLI from any directory.  Easy!</p>
<h2 id="what-about-yarn">What About Yarn?</h2>
<p>This can also be done using Yarn via yarn add.</p>
<p>From the Yarn <a href="https://yarnpkg.com/en/docs/cli/add" target="_blank">docs</a>, we see:</p>
<pre><code>yarn <span class="hljs-keyword">add</span> &lt;git remote url&gt;
</code></pre><p>So, similarly, for an engineer to install my CLI via yarn, they need to run:</p>
<pre><code>yarn <span class="hljs-keyword">global</span> <span class="hljs-keyword">add</span> &lt;<span class="hljs-keyword">private</span> CLI git repo url&gt;
</code></pre><h2 id="and-thats-it">And that's it!</h2>
<p>It was interesting to learn that little bit of the npm CLI.  Poking around the documentation has shown me another thing or two.  If like me, you didn't know about some of these extra <code>npm install</code> powers, I hope you found this interesting and potentially useful.</p>
]]></content:encoded></item><item><title><![CDATA[React Higher-Order Component Using Refs]]></title><description><![CDATA[A higher-order component (HOC) is an advanced technique in React for reusing component logic.  Basically, a higher-order component is a function that takes a component and returns a new component.
I like to consider myself pretty comfortable with HOC...]]></description><link>https://bytesizedcoding.com/react-higher-order-component-using-refs</link><guid isPermaLink="true">https://bytesizedcoding.com/react-higher-order-component-using-refs</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Zachary Wagner]]></dc:creator><pubDate>Fri, 04 Dec 2020 20:33:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1607113817184/FVTo7ZKqP.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A higher-order component (HOC) is an advanced technique in React for reusing component logic.  Basically, a higher-order component is a function that takes a component and returns a new component.</p>
<p>I like to consider myself pretty comfortable with HOCs.  I've written quite a few of them in the years I've been using React.  However, I came across an interesting error recently.</p>
<h2 id="the-issue">The Issue</h2>
<p>Consider a HOC that, among other things, accesses the wrapped component via a ref.  The HOC works without issue... until it's used to wrap a stateless functional component.  Then, the following error is logged in the console:</p>
<pre><code>Stateless <span class="hljs-keyword">function</span> components cannot be given refs. Attempts <span class="hljs-keyword">to</span> <span class="hljs-keyword">access</span> this <span class="hljs-keyword">ref</span> will fail.
</code></pre><p>So, a component might need most of the functionality of a HOC, just not the ref related code, and it can't use it!</p>
<p>You might be thinking, "Simple fix.  I'd just make the component I'm wrapping a class-based component."  Yes, that's one way.  But how about a way where you can maintain your functional component?  There had to be another solution.</p>
<h2 id="the-solution">The Solution</h2>
<p>The solution lies in having the HOC be able to recognize a difference between a class-based component and a stateless functional component.  In this case, it's the fact that class-based components extend React's Component class.  Class-based components need to have a render method, so the existence of that method can determine whether or not a component is stateful.  Once that is known, the wrapped component can be conditionally given a ref.  See the code below for a stripped-down example of a HOC like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> EnhancedComponent =&gt; {
  <span class="hljs-keyword">return</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeHOC</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    <span class="hljs-keyword">constructor</span>() {
      <span class="hljs-built_in">super</span>();

      <span class="hljs-comment">// check the prototype of the wrapped component's prototype for render</span>
      <span class="hljs-comment">// if render is not undefined, then the component is stateful, and can have a ref</span>
      <span class="hljs-built_in">this</span>._isStateful = (EnhancedComponent.prototype.render !== <span class="hljs-literal">undefined</span>);
    }

    <span class="hljs-comment">/* Other HOC code would go here */</span>

    render() {
      <span class="hljs-keyword">const</span> allProps = {
        <span class="hljs-comment">// spread all passed in props</span>
        <span class="hljs-comment">// you'd also add any other props to pass to wrapped components here</span>
        ...this.props
      };

      <span class="hljs-comment">// if the component is stateful, give it a ref.  Yay!</span>
      <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>._isStateful) {
        allProps.ref = <span class="hljs-function"><span class="hljs-params">node</span> =&gt;</span> { <span class="hljs-built_in">this</span>._wrappedInstance = node; };
      }

      <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">EnhancedComponent</span>
          {<span class="hljs-attr">...allProps</span>}
        /&gt;</span></span>
      );
    }
  };
};
</code></pre>
<p>And there you have it!  I know this might seem like a pretty specialized case, and it is.  However, if you have a HOC that sometimes needs access to a ref while also being able to wrap stateless functional components, then this should work for you.</p>
]]></content:encoded></item></channel></rss>