<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaScript &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:27:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>JavaScript &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Webpack: Managing Tree Shaking</title>
		<link>https://myshittycode.com/2019/07/09/webpack-managing-tree-shaking/</link>
					<comments>https://myshittycode.com/2019/07/09/webpack-managing-tree-shaking/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 10 Jul 2019 00:41:52 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Webpack]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1108</guid>

					<description><![CDATA[<p>PROBLEM Sometimes, Webpack&#8217;s tree-shaking may accidentally eliminate imported code from import statements. For example, we may have a root JS file that imports a CSS file:- &#8230; and for some reason, the CSS file will never appear in the final bundle even if the Webpack config contains proper rules to handle CSS files. SOLUTION To [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/07/09/webpack-managing-tree-shaking/">Webpack: Managing Tree Shaking</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Sometimes, Webpack&#8217;s tree-shaking may accidentally eliminate imported code from <strong>import</strong> statements.</p>



<p>For example, we may have a root JS file that imports a CSS file:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [3]; title: ; notranslate">
import React from &#039;react&#039;;
import ReactDOM from &#039;react-dom&#039;;
import &#039;../css/index.css&#039;;

...
</pre></div>


<p>&#8230; and for some reason, the CSS file will never appear in the final bundle even if the Webpack config contains proper rules to handle CSS files.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To prevent Webpack from removing any &#8220;unreferenced&#8221; code (ex: global CSS files, JS polyfills, etc), list these side effects under <strong>package.json</strong>, for example:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [3,4,5]; title: ; notranslate">
{
  &quot;name&quot;: &quot;front-end-stack&quot;,
  &quot;sideEffects&quot;: &#x5B;
    &quot;*.css&quot;
  ]
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2019/07/09/webpack-managing-tree-shaking/">Webpack: Managing Tree Shaking</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2019/07/09/webpack-managing-tree-shaking/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1108</post-id>	</item>
		<item>
		<title>React: Debugging Layout Thrashing</title>
		<link>https://myshittycode.com/2019/04/15/react-debugging-layout-thrashing/</link>
					<comments>https://myshittycode.com/2019/04/15/react-debugging-layout-thrashing/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 16 Apr 2019 02:04:56 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[React]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1089</guid>

					<description><![CDATA[<p>PROBLEM When the React app grows larger over time, it is highly likely to run into situations where the component keeps re-rendering for no apparent reason. There are many reasons why this is happening&#8230; to name a few&#8230;. parent component re-renders, this.props or this.state has changed, etc. The key is to quickly find out what [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/15/react-debugging-layout-thrashing/">React: Debugging Layout Thrashing</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>When the React app grows larger over time, it is highly likely to run into situations where the component keeps re-rendering for no apparent reason.</p>



<p>There are many reasons why this is happening&#8230; to name a few&#8230;. parent component re-renders, <strong>this.props</strong> or <strong>this.state</strong> has changed, etc.</p>



<p>The key is to quickly find out what causes the component to re-render constantly.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>The simplest solution, in my opinion, is to paste the following block into the troublesome component:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
componentDidUpdate(prevProps, prevState) {
  const debug = (label, currObject = {}, prevObject = {}) =&amp;gt; {
    Object.entries(currObject).forEach((&#x5B;key, currValue]) =&amp;gt; {
      if (prevObject&#x5B;key] !== currValue) {
        console.log(`&#x5B;DEBUG] ${label} has changed: `, key);
        console.log(&#039;&#x5B;DEBUG] - BEFORE : &#039;, prevObject&#x5B;key]);
        console.log(&#039;&#x5B;DEBUG] -  AFTER : &#039;, currValue);
      }
    });
  };

  debug(&#039;Prop&#039;, this.props, prevProps);
  debug(&#039;State&#039;, this.state, prevState);
}
</pre></div>


<p>Adding this <strong>componentDidUpdate(..)</strong> lifecycle allows us to quickly find out which property or state has changed.</p>



<p>When running the app, the console may display something like this:-</p>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="311" height="174" src="https://myshittycode.com/wp-content/uploads/2019/04/screen-shot-2019-04-15-at-8.31.50-pm-1.png?x45560" alt="Console Log" class="wp-image-1090" srcset="https://myshittycode.com/wp-content/uploads/2019/04/screen-shot-2019-04-15-at-8.31.50-pm-1.png 311w, https://myshittycode.com/wp-content/uploads/2019/04/screen-shot-2019-04-15-at-8.31.50-pm-1-300x168.png 300w" sizes="(max-width: 311px) 100vw, 311px" /></figure>



<p>The key is to look for identical object or array displayed in both &#8220;BEFORE&#8221; and &#8220;AFTER&#8221; statements. This shows that while the values look similar, they fail on strict equality check (&#8216;===&#8217;), which causes the component to re-render.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/15/react-debugging-layout-thrashing/">React: Debugging Layout Thrashing</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2019/04/15/react-debugging-layout-thrashing/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1089</post-id>	</item>
		<item>
		<title>React + Recompose: Calling Multiple HOC Wrappers</title>
		<link>https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/</link>
					<comments>https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 17:37:35 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[ES6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[React]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1081</guid>

					<description><![CDATA[<p>PROBLEM Sometimes, wrapping a React component with multiple High Order Components (HOC) can get rather unwieldy and unreadable. For example:- SOLUTION To fix this, we can leverage recompose library. Now, we can rewrite the above example like this:- Keep in mind, the HOC order defined in compose(..) is important.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/">React + Recompose: Calling Multiple HOC Wrappers</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Sometimes, wrapping a React component with multiple High Order Components (HOC) can get rather unwieldy and unreadable.</p>



<p>For example:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [10]; title: ; notranslate">
import React from &#039;react&#039;;
import { withRouter } from &#039;react-router-dom&#039;;
import { withStyles } from &#039;material-ui/styles&#039;;
import withWidth from &#039;material-ui/utils/withWidth&#039;;

class MyComponent extends React.PureComponent {
	// ...
}

export default withRouter(withStyles(styles)(withWidth()(MyComponent)));
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p>To fix this, we can leverage <a href="https://github.com/acdlite/recompose" target="_blank" rel="noopener">recompose</a> library.</p>



<p>Now, we can rewrite the above example like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [5,11,12,13,14,15]; title: ; notranslate">
import React from &#039;react&#039;;
import { withRouter } from &#039;react-router-dom&#039;;
import { withStyles } from &#039;material-ui/styles&#039;;
import withWidth from &#039;material-ui/utils/withWidth&#039;;
import compose from &#039;recompose/compose&#039;;

class MyComponent extends React.PureComponent {
	// ...
}

export default compose(
  withRouter,
  withStyles(styles),
  withWidth(),
)(MyComponent);
</pre></div>


<p>Keep in mind, the HOC order defined in <strong>compose(..)</strong> is important.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/">React + Recompose: Calling Multiple HOC Wrappers</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2018/04/02/react-recompose-calling-multiple-hoc-wrappers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1081</post-id>	</item>
		<item>
		<title>Webpack + ESLint: Automatically Fix ESLint Errors</title>
		<link>https://myshittycode.com/2018/04/02/webpack-eslint-automatically-fix-eslint-errors/</link>
					<comments>https://myshittycode.com/2018/04/02/webpack-eslint-automatically-fix-eslint-errors/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 17:35:35 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[ESLint]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Webpack]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1079</guid>

					<description><![CDATA[<p>PROBLEM Given the following webpack.config.js&#8230; When running any Webpack command, ESLint may find violations and halt the entire process with the following error message:- SOLUTION Certain errors (ex: trailing commas, wrong indentation, extra semicolon) are easily fixable. There&#8217;s no need to halt the process and wait for developers to fix these obvious errors. To configure [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/04/02/webpack-eslint-automatically-fix-eslint-errors/">Webpack + ESLint: Automatically Fix ESLint Errors</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Given the following <strong>webpack.config.js</strong>&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
module.exports = {
  ...
  module: {
    rules: &#x5B;
      {
        enforce: &#039;pre&#039;,
        test: /\.js?$/,
        loader: &#039;eslint-loader&#039;,
        exclude: /node_modules/,
      },
	  ...
    ],
  },
  ...
};
</pre></div>


<p>When running any Webpack command, ESLint may find violations and halt the entire process with the following error message:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
/path/to/front-end-stack/src/js/components/home/Home.js
  43:11  error  Expected indentation of 6 space characters but found 10  react/jsx-indent
  44:14  error  Expected indentation of 6 space characters but found 13  react/jsx-indent

x 2 problems (2 errors, 0 warnings)
  2 errors, 0 warnings potentially fixable with the `--fix` option.
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p>Certain errors (ex: trailing commas, wrong indentation, extra semicolon) are easily fixable.</p>



<p>There&#8217;s no need to halt the process and wait for developers to fix these obvious errors.</p>



<p>To configure ESLint to automatically fix these &#8220;soft&#8221; errors, add the following <strong>options</strong> block to the above rule:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [10,11,12]; title: ; notranslate">
module.exports = {
  ...
  module: {
    rules: &#x5B;
      {
        enforce: &#039;pre&#039;,
        test: /\.js?$/,
        loader: &#039;eslint-loader&#039;,
        exclude: /node_modules/,
        options: {
          fix: true,
        },
      },
	  ...
    ],
  },
  ...
};
</pre></div>


<p>If you are using any VCS, remember to commit any file changes.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/04/02/webpack-eslint-automatically-fix-eslint-errors/">Webpack + ESLint: Automatically Fix ESLint Errors</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2018/04/02/webpack-eslint-automatically-fix-eslint-errors/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1079</post-id>	</item>
		<item>
		<title>ES6 + Mocha + Sinon: Mocking Imported Dependency</title>
		<link>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/</link>
					<comments>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 10 May 2017 20:46:35 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[ES6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Mocha]]></category>
		<category><![CDATA[Sinon]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1014</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we have the following 2 files:- apis.js service.js Let&#8217;s assume we want to test the logic in service.js without using nock to mock the HTTP call in apis.js. While proxyquireify allows us to mock out the apis.js dependency in service.js, sometimes it is a little more complicated than needed. SOLUTION A simpler [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/">ES6 + Mocha + Sinon: Mocking Imported Dependency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we have the following 2 files:-</p>



<p><b><b>apis.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import fetch from &#039;isomorphic-fetch&#039;;

export const logout = () =&gt; (
  fetch(&#039;/logout&#039;)
    .then(resp =&gt; resp.json())
    .catch(err =&gt; err)
);
</pre></div>


<p><b><b>service.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { logout } from &#039;./apis&#039;;

export const kickUserOut = activeSession =&gt; (
  activeSession ? logout() : undefined
);
</pre></div>


<p>Let&#8217;s assume we want to test the logic in <b>service.js</b> without using <a href="https://github.com/node-nock/nock" target="_blank" rel="noopener">nock</a> to mock the HTTP call in <b>apis.js</b>.</p>



<p>While <a href="https://github.com/thlorenz/proxyquireify" target="_blank" rel="noopener">proxyquireify</a> allows us to mock out the <b>apis.js</b> dependency in <b>service.js</b>, sometimes it is a little more complicated than needed.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>A simpler approach is to use <a href="https://github.com/sinonjs/sinon" target="_blank" rel="noopener">sinon</a> to stub out <b>logout()</b> defined in <b>apis.js</b>.</p>



<p><b><b>service-spec.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { beforeEach, afterEach, describe, it } from &#039;mocha&#039;;
import { expect } from &#039;chai&#039;;
import sinon from &#039;sinon&#039;;
import { kickUserOut } from &#039;./service&#039;;

// import everything as an object
import * as apis from &#039;./apis&#039;;

describe(&#039;service =&gt; kickUserOut&#039;, () =&gt; {
  let logoutStub;

  // before running each test, stub out `logout()`
  beforeEach(() =&gt; {
    logoutStub = sinon.stub(apis, &#039;logout&#039;).returns(&#039;success&#039;);
  });

  // after running each test, restore to the original method to
  // prevent &quot;TypeError: Attempted to wrap logout which is already wrapped&quot;
  // error when executing subsequent specs.
  afterEach(() =&gt; {
    apis.logout.restore();
  });

  it(&#039;given active session, should invoke logout API&#039;, () =&gt; {
    expect(kickUserOut(true)).to.deep.equal(&#039;success&#039;);
    expect(logoutStub.calledOnce).to.equal(true);
  });

  it(&#039;given expired session, should not invoke logout API&#039;, () =&gt; {
    expect(kickUserOut(false)).to.equal(undefined);
    expect(logoutStub.calledOnce).to.equal(false);
  });
});
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/">ES6 + Mocha + Sinon: Mocking Imported Dependency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1014</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk (Request-wide modification query)

Served from: myshittycode.com @ 2026-02-19 01:55:52 by W3 Total Cache
-->