<?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>react Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/react/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/react/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Sun, 29 Oct 2023 04:42:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>react Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/react/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>React State Management Basics</title>
		<link>https://www.codejourney.net/react-state-management-basics/</link>
					<comments>https://www.codejourney.net/react-state-management-basics/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 29 Oct 2023 04:39:10 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4591</guid>

					<description><![CDATA[<p>If I were to point out a single concept that is critical to understand in React development, it would be state management. So what is React state management? How is it different from managing data in C# or Java? Let&#8217;s find out! 🙂 State If you are a backend or desktop developer, you may have&#8230;</p>
<p>The post <a href="https://www.codejourney.net/react-state-management-basics/">React State Management Basics</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If I were to point out a single concept that is critical to understand in React development, it would be state management.</p>



<p>So what is React state management? How is it different from managing data in C# or Java? Let&#8217;s find out! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4591"></span>



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



<p>If you are a backend or desktop developer, you may have never come across the term of state. At least, I never used it before learning frontend web development. Surely not in the context it is used in frameworks like React. The term more familiar to me at that time was <em>data binding</em> known from <a href="https://learn.microsoft.com/en-us/dotnet/desktop/wpf/overview">WPF</a> or <a href="https://learn.microsoft.com/en-us/dotnet/desktop/winforms/overview">WinForms</a>.</p>



<p>This is what <a href="https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data">WPF documentation</a> says:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>
<cite>Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically</cite></blockquote>



<p>In case of WPF, the crucial part to me is <em><strong>If the binding has the correct settings and the data provides the proper notifications</strong></em> <strong><em>(&#8230;)</em></strong>. If you have ever worked with WPF, you know how frustrating can these <em>notifications</em> be (any <code>INotifyPropertyChanged</code> fans here?).</p>



<p>WPF&#8217;s state management it not that bad. However, React has something better <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>React introduces us to a concept of <em>state</em>. In React components, you do not set anything directly, like calling some code to update an input&#8217;s value (this is possible, we&#8217;re still in <a href="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/">JavaScript world</a>, but it&#8217;s not a <em>React way</em> of doing stuff). You don&#8217;t directly bind your UI controls with data like you would in WPF or WinForms. Instead, you keep the data in memory, which is a current <em>state </em>of the UI (the whole app or a piece of it). </p>



<p><strong>When the state changes, your UI is updated by React</strong>. In other words &#8211; <strong>in React, UI is a function of state</strong>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-markup code-embed-pre"  data-start="1" data-line-offset="0"><code class="language-markup code-embed-code">UI = fn(state)</code></pre> <div class="code-embed-infos"> </div> </div></div>



<p>It means that your React app (or its individual pieces called <em>components</em>) take some state and output the actual, rendered UI.</p>



<p>This is React state management in a nutshell. Let&#8217;s explore it a bit more <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p><em>Small disclaimer:</em> <em>I will focus on state in React functional componets. I consider <a href="https://react.dev/reference/react/Component">class components somewhat legacy</a>. I think this meme summarizes it best <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" />:</em></p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://www.freecodecamp.org/news/functional-setstate-is-the-future-of-react-374f30401b6b/"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="500" height="381" data-attachment-id="4669" data-permalink="https://www.codejourney.net/react-state-management-basics/react-setstate-meme/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?fit=500%2C381&amp;ssl=1" data-orig-size="500,381" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="react-setstate-meme" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?fit=500%2C381&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?fit=500%2C381&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?resize=500%2C381&#038;ssl=1" alt="" class="wp-image-4669"/></a></figure>
</div>


<h3 class="wp-block-heading">The Naive Way</h3>



<p>Let&#8217;s try to approach React state management as C# developer. In React, the basic piece of UI we create is a component. Let&#8217;s add one:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import Button from &quot;react-bootstrap/Button&quot;;<br/><br/>export const NaiveComponent = () =&gt; {<br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;I am a naive component&lt;/div&gt;<br/>      &lt;Button variant=&quot;success&quot;&gt;Click me!&lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">NaiveComponent.tsx</span> </div> </div></div>



<p>For now, we render some text and a button which does nothing. What we want is to have some data displayed, like a number, which is incremented on every button click. Let&#8217;s try to implement that naively:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const NaiveComponent = () =&gt; {<br/>  let myCount = 0;<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;I am a naive component&lt;/div&gt;<br/>      &lt;div&gt;Current count: {myCount}&lt;/div&gt;<br/>      &lt;Button<br/>        variant=&quot;success&quot;<br/>        onClick={() =&gt; {<br/>          myCount＋＋;<br/>        }}<br/>      &gt;<br/>        Click me!<br/>      &lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">NaiveComponent.tsx &#8211; the naive counter incrementing</span> </div> </div></div>



<p>Now, let&#8217;s check if it works:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" decoding="async" width="536" height="285" data-attachment-id="4602" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_wkzlxfcktf/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?fit=536%2C285&amp;ssl=1" data-orig-size="536,285" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_wkZlXfCKTf" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?fit=536%2C285&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?fit=536%2C285&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?resize=536%2C285&#038;ssl=1" alt="" class="wp-image-4602"/></figure>
</div>


<p></p>



<p>It looks nothing happens <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f979.png" alt="🥹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> To investigate that, we can add a <code>console.log</code> (the most common debugging technique in JavaScript world <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f605.png" alt="😅" class="wp-smiley" style="height: 1em; max-height: 1em;" />) to the <code>onClick</code> event:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">onClick={() =&gt; {<br/>          myCount＋＋;<br/>          console.log(myCount);<br/>        }}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">onClick with console.log</span> </div> </div></div>



<p>How does that look in the console now?</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" decoding="async" width="1071" height="591" data-attachment-id="4603" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_g62ao8n7fw/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?fit=1071%2C591&amp;ssl=1" data-orig-size="1071,591" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_g62ao8n7FW" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?fit=1071%2C591&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?fit=1071%2C591&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?resize=1071%2C591&#038;ssl=1" alt="React state management - naive way (like C# dev), not working" class="wp-image-4603" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?w=1071&amp;ssl=1 1071w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?resize=768%2C424&amp;ssl=1 768w" sizes="(max-width: 1071px) 100vw, 1071px" /></figure>
</div>


<p></p>



<p>Woooow, something is really going wrong here! The data is updated in memory, but not reflected in the UI <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Why does that happen? Because <code>myCount</code> is not a state. It&#8217;s just some variable we created hoping that it will work fine as part of the UI. Remember what we have discussed in the previous section &#8211; we need to start <em>thinking in React</em>. This is <em>The</em> <em>React way</em>, not <em>Java way </em>or <em>C# way </em>anymore.</p>



<h3 class="wp-block-heading">The React Way</h3>



<p>So, how to use React state management properly in our example? We need to make <code>myCount</code> be a part of component&#8217;s state. The simplest way to do that looks as follows:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2,11" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const TheReactWayComponent = () =&gt; {<br/>  const [myCount, setMyCount] = useState(0);<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;I am a React Way component&lt;/div&gt;<br/>      &lt;div&gt;Current count: {myCount}&lt;/div&gt;<br/>      &lt;Button<br/>        variant=&quot;success&quot;<br/>        onClick={() =&gt; {<br/>          setMyCount(myCount ＋ 1);<br/>        }}<br/>      &gt;<br/>        Click me!<br/>      &lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">TheReactWayComponent.tsx &#8211; The React Way</span> </div> </div></div>



<p>First, instead of declaring a &#8220;normal&#8221; variable for storing our UI-related data, we used the <code>useState</code> <a href="https://react.dev/reference/react#state-hooks">hook</a>. As you can see, it returns an array with two things: our state variable (<code>myCount) </code>and a function to update its value<code> </code>(<code>setMyCount</code>). We also set the initial value to <code>0</code>. A good practice is to always use a construct like <code>[myCount, setMyCount]</code> to destructure what <code>useState</code> returns into named, separate objects.</p>



<p>Now we are managing our component&#8217;s state <em>The React Way</em>. Does it work? Let&#8217;s see:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="464" height="215" data-attachment-id="4624" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_ojmhjudmmd/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?fit=464%2C215&amp;ssl=1" data-orig-size="464,215" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_OJMhjuDMmd" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?fit=464%2C215&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?fit=464%2C215&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?resize=464%2C215&#038;ssl=1" alt="React state management - The React Way" class="wp-image-4624"/></figure>
</div>


<p></p>



<p>Great! We have just learned how the basic form of state management works in React. That was easy, wasn&#8217;t it? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>



<p>Now, let us discuss one more aspect related to the state itself.</p>



<h3 class="wp-block-heading">Reference vs value</h3>



<p>We know the React Way for state management now. We feel confident. However, we are also used to how data is managed in C# or Java, especially in terms of <a href="https://www.codejourney.net/net-internals-02-stack-and-heap-net-data-structures/">handling reference and value types</a>. This knowledge will come very handy now <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Let&#8217;s try to create a bit more complex component:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">type Person = {<br/>  id: number;<br/>  name: string;<br/>  age: number;<br/>};<br/><br/>export const ObjectHolderComponent = () =&gt; {<br/>  const johnInitialValue: Person = {<br/>    id: 1,<br/>    name: &quot;John Doe&quot;,<br/>    age: 30,<br/>  };<br/><br/>  const [john, setJohn] = useState&lt;Person&gt;(johnInitialValue);<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;ID: {john.id}&lt;/div&gt;<br/>      &lt;div&gt;Name: {john.name}&lt;/div&gt;<br/>      &lt;div&gt;Age: {john.age}&lt;/div&gt;<br/>      &lt;Button<br/>        variant=&quot;success&quot;<br/>        onClick={() =&gt; {<br/>          const currentJohnObject = john;<br/>          currentJohnObject.age = currentJohnObject.age ＋ 1;<br/>          console.log(currentJohnObject);<br/>          setJohn(currentJohnObject);<br/>        }}<br/>      &gt;<br/>        Make John older<br/>      &lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ObjectHolderComponent.tsx</span> </div> </div></div>



<p>Now, instead of storing a single value in our component&#8217;s state, we store a whole object of type <code>Person</code>. The object has a few properties: <code>id</code>, <code>name</code> and <code>age</code>.</p>



<p>There is also a button. We want this button to take the current <code>john</code> object, increment the value of <code>age</code> on it and update the state. We do it The React Way using <code>setJohn</code> function returned by <code>useState</code>. Just to be sure, we also have the <code>console.log(john)</code> just before updating the state. Does that work as we expected?</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="873" height="656" data-attachment-id="4626" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_hlgfththbt/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?fit=873%2C656&amp;ssl=1" data-orig-size="873,656" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_hlgFthtHBT" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?fit=873%2C656&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?fit=873%2C656&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?resize=873%2C656&#038;ssl=1" alt="Changing an object's (reference) internal variable does not trigger a re-render, even we use useState" class="wp-image-4626" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?w=873&amp;ssl=1 873w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?resize=768%2C577&amp;ssl=1 768w" sizes="auto, (max-width: 873px) 100vw, 873px" /></figure>
</div>


<p></p>



<p>Eh, what&#8217;s happening here this time? We can clearly see that <code>age</code> is updated on the object on each click, but why isn&#8217;t the UI re-rendered? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>As I mentioned before, it&#8217;s critical to understand the difference between reference and value types here. If you don&#8217;t know it, <a href="https://www.codejourney.net/net-internals-02-stack-and-heap-net-data-structures/">go and make up for it now</a>.</p>



<p>The reason it does not work is that we haven&#8217;t updated the actual content of <code>john</code> state variable by calling <code>setJohn(currentJohnObject)</code>. <strong>What is really being hold in <code>john</code> variable is the reference (pointer, address in memory) to the actual object</strong>. And this is what React tracks. We didn&#8217;t really update it, because we assigned the same instance of the object to the state. It doesn&#8217;t matter that we updated its internal property <code>age</code> &#8211; the reference itself wasn&#8217;t updated.</p>



<p>How do we solve that? Well, in current solution we&#8217;d need to create a copy of <code>john</code> object before using <code>setJohn</code> to alter the state. In effect, we&#8217;ll get a new instance of the object in memory and <code>john</code> variable will hold a new reference (address) to it.</p>



<p>So, if we only change our <code>onClick</code> implementation to this one:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">onClick={() =&gt; {<br/>          const johnCopy = { ...john };<br/>          johnCopy.age = john.age ＋ 1;<br/>          setJohn(johnCopy);<br/>        }}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ObjectHolderComponent.tsx &#8211; onClick implementation with copying the original object</span> </div> </div></div>



<p>Everything starts to work as expected:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="333" height="236" data-attachment-id="4627" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_yfmhz6z0xn/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?fit=333%2C236&amp;ssl=1" data-orig-size="333,236" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_yfmHz6z0Xn" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?fit=333%2C236&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?fit=333%2C236&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?resize=333%2C236&#038;ssl=1" alt="Changing a reference state by creating a copy of it works fine in React" class="wp-image-4627"/></figure>
</div>


<p></p>



<p>I think you now understand how React state management works. I also hope that you see the issues managing state in such a way can bring. Even in this trivial example, we create a new instance of an object on every button click. Apart from memory usage considerations, this just looks ugly. Imagine manually creating copies of much more complex objects, like classes storing arrays or dictionaries. This gets pretty wild <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>We will see how this issue can be addressed by the end of this article. But first, let&#8217;s discuss <code>props</code> &#8211; another concept critical to really understand React state management.</p>



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



<p>Apart from <code>state</code>, we also use <code>props</code> to control the state of our React components. The name is a short for <em>properties</em>, which quite well describes its purpose. <code>Props</code> are the data input provided to a component from outside (from a parent component). They are immutable and cannot be changed inside the component which receives them. Let&#8217;s see some examples.</p>



<h3 class="wp-block-heading">Sharing state between components</h3>



<p>The most common usage for <code>props</code> is sharing state between components. Imagine that you have an input, where the user manually enters some data. You want to use the current value of this input in two other components. This is where you need to share your component&#8217;s state (the value of the input) with these two other components. We often call it <em>lifting the state up</em> &#8211; you keep your state higher in the components tree, so the child components can use it.</p>



<p>Let&#8217;s try to see an example of connecting <code>state</code> with <code>props</code>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="4,9,27,28" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const PersonData = () =&gt; {<br/>  const minimumAge = 5;<br/>  const maximumAge = 100;<br/>  const [age, setAge] = useState&lt;number&gt;(minimumAge);<br/><br/>  const handleAgeChange = (event: React.ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {<br/>    const enteredAge = parseInt(event.target.value);<br/>    if (enteredAge &gt;= minimumAge &amp;&amp; enteredAge &lt;= maximumAge) {<br/>      setAge(enteredAge);<br/>    }<br/>  };<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;Form&gt;<br/>        &lt;Form.Group controlId=&quot;age&quot;&gt;<br/>          &lt;Form.Label&gt;Your age&lt;/Form.Label&gt;<br/>          &lt;Form.Control<br/>            type=&quot;number&quot;<br/>            value={age}<br/>            onChange={handleAgeChange}<br/>            min={minimumAge}<br/>            max={maximumAge}<br/>          /&gt;<br/>        &lt;/Form.Group&gt;<br/>      &lt;/Form&gt;<br/>      &lt;DaysLiving currentAge={age} /&gt;<br/>      &lt;YearsUntilCentenarian currentAge={age} /&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">PersonData.tsx &#8211; state and props in practice</span> </div> </div></div>



<p>As you can see, at lines 4 and 9 we manage the state variable <code>age</code>. This is the old stuff we have already seen.</p>



<p>However, at lines 27 and 28 we are passing the <code>age</code> state variable into <code>DaysLiving</code> and <code>YearsUntilCentenarian</code> children components as <code>currentAge</code> prop. This is how the <code>DaysLiving</code> component is implemented:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">type DaysLivingProps = {<br/>  currentAge: number;<br/>};<br/><br/>export const DaysLiving = (props: DaysLivingProps) =&gt; {<br/>  const daysLived = props.currentAge * 365;<br/><br/>  return (<br/>    &lt;div&gt;<br/>      &lt;p&gt;You have lived for {daysLived} days.&lt;/p&gt;<br/>    &lt;/div&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">DaysLiving.tsx</span> </div> </div></div>



<p>Notice that <code>props</code> is simply a typed object you pass to a function component. To make things easier, you can also use object destructuring and accept the props in the following way:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const DaysLiving = ({currentAge}: DaysLivingProps) =&gt; {<br/>  const daysLived = currentAge * 365;<br/>  /// ... rest of the code</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">DaysLiving.tsx &#8211; props with object destructuring</span> </div> </div></div>



<p>That way, you don&#8217;t need to write <code>props.currentAge</code> every time you want to use this property inside the component.</p>



<h3 class="wp-block-heading">Does changing props always trigger a re-render?</h3>



<p>Well, generally the answer to this question is <em>yes</em>. Every time a prop of a component changes, this component will get re-rendered. However, this is not <em>entirely</em> true <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>To make this belief true, we need to assume that this prop is changed <em>in a React way</em>. So, to take things literally, consider this component we saw before:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const DaysLiving = ({currentAge}: DaysLivingProps) =&gt; {<br/>  const daysLived = currentAge * 365;<br/><br/>  return (<br/>    &lt;div&gt;<br/>      &lt;p&gt;You have lived for {daysLived} days.&lt;/p&gt;<br/>    &lt;/div&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">DaysLiving.tsx</span> </div> </div></div>



<p>From its perspective only, it accepts a <code>currentAge</code> property. For this component to re-render, it&#8217;s not enough that the <code>currentAge</code> property changes in any way. <strong>Its value must be changed by mutating the state in the parent component</strong>.</p>



<p>So, if the value provided as <code>currentAge</code> to <code>DaysLiving</code> component changes in the parent component by properly mutating the state (with <code>setAge</code> mutation callback, in our case):</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="7,14" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const PersonData = () =&gt; {<br/>  // ...<br/>  const [age, setAge] = useState&lt;number&gt;(minimumAge);<br/><br/>  const handleAgeChange = (event: React.ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {<br/>      // ...<br/>      setAge(enteredAge);<br/>    }<br/>  };<br/><br/>  return (<br/>    &lt;&gt;<br/>      // ...<br/>      &lt;DaysLiving currentAge={age} /&gt;<br/>      // ...<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">PersonData.tsx &#8211; mutating the state for children components</span> </div> </div></div>



<p>the child component (<code>DaysLiving</code>) will get re-rendered.</p>



<p>This is because when a parent component changes, React by default re-renders the parent component itself and all of its children.</p>



<p>If you modified the <code>age</code> variable in the parent compont directly, without using <code>setAge</code> (which we already know it&#8217;s incorrect), the <code>currentAge</code> prop would technically change, but the <code>DaysLiving</code> component will not get re-rendered.</p>



<h3 class="wp-block-heading">state vs props</h3>



<p>This is a question that people oftern ask: what&#8217;s the difference between <code>props</code> and <code>state</code> in React? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>I would start with addressing this differently: what do <code>props</code> and <code>state</code> have in common?</p>



<p>As you already know, both are used for React state management. Both of them influence when the components will be re-rendered by React. Props and state are the absolute foundations of React.</p>



<p>The main difference is that <code>state</code> is used internally in a component, while <code>props</code> are used to pass information from parent to children components. <code>State</code> is also naturally mutable within a component where it&#8217;s defined, but <code>props</code> are immutable and cannot be changed in a component that receives and uses them.</p>



<h2 class="wp-block-heading">React state management libraries</h2>



<p>That&#8217;s basically everything you need to know to be able to comfortably work with React state management. However, I mentioned before that managing state using only built-in React mechanisms actually sucks <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /> That&#8217;s why I&#8217;d like to make a short point on state management libraries.</p>



<p>Maybe you heard about <a href="https://redux.js.org/">Redux</a>, <a href="https://github.com/pmndrs/zustand">Zustand</a> or <a href="https://mobx.js.org/README.html">MobX</a>. All of them are state management libraires for React. But why would you even need one? And should you learn one now?</p>



<p>In my opinion, you can easily start working with React using the built-in state management mechanisms we discussed today. It&#8217;s good to know how it works in practice. However, the truth is that as your application grows and you want to keep it scalable and maintenable, you will need a state management library at some point. As soon as your components tree gets bigger, sharing data and state will not be as easy as &#8220;passing props to a children component&#8221;. You will need to share state between multiple components in the tree, sometimes not only between parent and children, but also with another components defined on the same level or even in a totally different place of the tree.</p>



<p>This is where React state management libraries come very handy. They solve many of the React&#8217;s issues that come to the surface as your state gets complex. But even if you use one, you will still always need <code>state</code> and <code>props</code>. That&#8217;s why it&#8217;s critical to understand these two basic concepts first and play with them for some time.</p>



<p>If you&#8217;re starting out, it&#8217;s good to know that state management libraries exist and that one day you will have to get your hands on one (or more <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" />) of them. I hope to share my thoughts on state management libraries in a separate article one day.</p>



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



<p>That&#8217;s it! Now you should know how React manages state of its components. Of course, we didn&#8217;t cover everything related to state management in React, but I hope you got the basics <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>You can find all of the source code used in this article <a href="https://github.com/dsibinski/codejourney/tree/main/react-state-management">here</a>.</p>



<p><strong>How do you manage state in your React apps? Do you use a state management library? If yes, which one and why? Let me know in the comments below!</strong></p>
<p>The post <a href="https://www.codejourney.net/react-state-management-basics/">React State Management Basics</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/react-state-management-basics/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4591</post-id>	</item>
		<item>
		<title>Adding Meatballs Menu To React-Table Rows</title>
		<link>https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/</link>
					<comments>https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 06 Feb 2023 08:16:17 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[react-table]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4302</guid>

					<description><![CDATA[<p>Meatballs menu (⋯), also called three horizontal dots menu, is a great way of providing contextual options for grid rows. In this article, I will show you how to add the meatballs menu to a table built with @tanstack/react-table. After reading this article, you will know how to add such a menu to your React&#8230;</p>
<p>The post <a href="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/">Adding Meatballs Menu To React-Table Rows</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Meatballs menu (⋯), also called <em>three horizontal dots menu</em>, is a great way of providing contextual options for grid rows. In this article, I will show you how to add the meatballs menu to a table built with <a href="https://www.npmjs.com/package/@tanstack/react-table">@tanstack/react-table</a>.</p>



<p>After reading this article, you will know how to add such a menu to your React app. The end result will look as in the highlighted picture of this article <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4302"></span>



<h2 class="wp-block-heading">Creating a table with row selection support</h2>



<p>First, let&#8217;s define a type of data that we want to display. For our example, we will display a list of <code>Car</code>s:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export type Car = {<br/>  id: string;<br/>  brand: string;<br/>  model: string;<br/>  productionYear: number;<br/>  isAvailable: boolean;<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">Car.ts</span> </div> </div></div>



<p>Next, we create a new component called <code>CarsTable</code>, responsible for rendering the table. We will use <a href="https://www.npmjs.com/package/@tanstack/react-table">@tanstack/react-table</a> for the table behavior and <a href="https://www.npmjs.com/package/react-bootstrap">react-bootstrap</a> for UI elements.</p>



<p>I implemented the <code>CarsTable</code> component in <a href="https://tanstack.com/table/v8/docs/examples/react/basic">quite a standard way according to react-table docs</a>, so I won&#8217;t copy-paste it here. You can check the whole component&#8217;s code <a href="https://github.com/dsibinski/react-table-context-menu/blob/faccde72b4be060c9dbca3d59d7b1506bc9f9b01/src/components/CarsTable.tsx">here</a>. What&#8217;s interesting is that I added support for row selection in a way that makes our table a <a href="https://reactjs.org/docs/forms.html#controlled-components">controlled React component</a>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2-3,10,14,16" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">type CarsTableProps = {<br/>  selectedCar: Car | null;<br/>  onCarSelected: (car: Car) =&gt; void;<br/>};<br/>export const CarsTable = (props: CarsTableProps) =&gt; {<br/>  return (<br/>    // ...<br/>    &lt;tbody&gt;<br/>        {table.getRowModel().rows.map((row) =&gt; {<br/>          const isActive = row.original.id === props.selectedCar?.id;<br/>          return (<br/>            &lt;tr<br/>              key={row.id}<br/>              style={isActive === true ? { backgroundColor: &quot;#3a7a11&quot; } : undefined}<br/>              onClick={() =&gt; {<br/>                props.onCarSelected(row.original);<br/>              }}<br/>            &gt;<br/>              {row.getVisibleCells().map((cell) =&gt; (<br/>                &lt;td key={cell.id}&gt;<br/>                  {flexRender(cell.column.columnDef.cell, cell.getContext())}<br/>                &lt;/td&gt;<br/>              ))}<br/>            &lt;/tr&gt;<br/>          );<br/>        })}<br/>      &lt;/tbody&gt;<br/>    // ...<br/>  )<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; controlled row selection</span> </div> </div></div>



<p>As you can see, <code>selectedCar</code> and <code>onCarSelected</code> are managed from outside. Line 14 shows how the row color gets changed for the currently selected car. I recently had to deal with such a case in one of my projects.</p>



<p>So far, so good. This is how it looks, populated with sample data:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="991" height="440" data-attachment-id="4312" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/1_initialtable/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?fit=991%2C440&amp;ssl=1" data-orig-size="991,440" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1_InitialTable" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?fit=991%2C440&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?fit=991%2C440&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?resize=991%2C440&#038;ssl=1" alt="react-table table with row selection support" class="wp-image-4312" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?w=991&amp;ssl=1 991w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?resize=768%2C341&amp;ssl=1 768w" sizes="auto, (max-width: 991px) 100vw, 991px" /></figure>
</div>


<h2 class="wp-block-heading">Adding meatballs menu</h2>



<p>Ok, we have a table. Now we want to add the meatballs menu. We need a three dots icon and a dropdown menu to open on clicking it.</p>



<p>After quickly <a href="https://react-bootstrap.github.io/components/dropdowns/">going through the react-bootstrap docs</a>, let&#8217;s create a new component for that:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import { Dropdown } from &quot;react-bootstrap&quot;;<br/>import { Car } from &quot;../types/Car&quot;;<br/>import CustomDivToggle from &quot;./CustomDivToggle&quot;;<br/>import { BsThreeDots } from &quot;react-icons/bs&quot;;<br/><br/>export const CarRowContextMenu = ({ carRow }: { carRow: Car }) =&gt; {<br/>  return (<br/>    &lt;Dropdown key={carRow.id}&gt;<br/>      &lt;Dropdown.Toggle as={CustomDivToggle} style={{ cursor: &quot;pointer&quot; }}&gt;<br/>        &lt;BsThreeDots /&gt;<br/>      &lt;/Dropdown.Toggle&gt;<br/>      &lt;Dropdown.Menu&gt;<br/>        &lt;Dropdown.Item&gt;Option 1&lt;/Dropdown.Item&gt;<br/>        &lt;Dropdown.Item&gt;Option 2&lt;/Dropdown.Item&gt;<br/>      &lt;/Dropdown.Menu&gt;<br/>    &lt;/Dropdown&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarRowContextMenu.tsx</span> </div> </div></div>



<p>As we want our dropdown toggle to have custom style, I had to provide <code>CustomDivToggle</code> as a <a href="https://react-bootstrap.github.io/components/dropdowns/#custom-dropdown-components">custom dropdown component</a>. It&#8217;s nothing very interesting, but you can check its implementation <a href="https://github.com/dsibinski/react-table-context-menu/blob/c98687413a28eecfc5fa4175a4b55a79dee8300f/src/components/CustomDivToggle.tsx">here</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Next, as we&#8217;d like our meatballs menu to be an additional column in the grid, it seems natural to <a href="https://tanstack.com/table/v8/docs/guide/column-defs#column-def-types">use <code>react-table</code>&#8216;s display column</a>. Let&#8217;s try adding it:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">columnHelper.display({<br/>      id: &quot;context-menu&quot;,<br/>      cell: (cellContext) =&gt; {<br/>        const row = cellContext.row.original;<br/>        return &lt;CarRowContextMenu carRow={row} /&gt;;<br/>      },<br/>    }),</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; adding meatballs menu with display-type column</span> </div> </div></div>



<p>It looks we have it:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="984" height="442" data-attachment-id="4316" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/3_contextmenuaddedv1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?fit=984%2C442&amp;ssl=1" data-orig-size="984,442" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3_ContextMenuAddedV1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?fit=984%2C442&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?fit=984%2C442&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?resize=984%2C442&#038;ssl=1" alt="Meatballs menu added to the table with react-tabe's display column" class="wp-image-4316" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?w=984&amp;ssl=1 984w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?resize=768%2C345&amp;ssl=1 768w" sizes="auto, (max-width: 984px) 100vw, 984px" /></figure>
</div>


<p>However, after clicking through it for a while, it seems we have an issue. The toggle only opens on every 2nd click:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="498" data-attachment-id="4317" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/4_contextmenudoubleclickissue/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?fit=1153%2C504&amp;ssl=1" data-orig-size="1153,504" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4_ContextMenuDoubleClickIssue" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?fit=1153%2C504&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?fit=1153%2C504&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?resize=1140%2C498&#038;ssl=1" alt="Meatballs menu with react-table's display column. Double-click issue" class="wp-image-4317" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?w=1153&amp;ssl=1 1153w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?resize=768%2C336&amp;ssl=1 768w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>
</div>


<p>Why is that? The reason is our controlled <code>CarsTable</code> component. On clicking a new row, the change event occurs, which triggers the re-render of the <code>CarsTable</code> component (because <code>selectedCar</code> <em>actually</em> changes). It makes <code>react-table</code> re-render the table, with the meatballs menu in its default state (collapsed). On clicking the menu in the same row again, the change event occurs, but the <code>selectedCar</code> <em>does not actually change</em>, which <em>does not</em> trigger the re-render. Initially, it took me a while to figure that out <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Fixing double-click issue</h2>



<p>In our case, a fix for the double click issue is quite simple. Instead of adding the column with the menu using <code>columnHelper.display()</code> function from <code>react-table</code>, we can render it <em>manually</em>. To do that, we should simply add a new <code>&lt;td&gt;</code> to each row of the table:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="17-19" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">&lt;tbody&gt;<br/>        {table.getRowModel().rows.map((row) =&gt; {<br/>          const isActive = row.original.id === props.selectedCar?.id;<br/>          return (<br/>            &lt;tr<br/>              key={row.id}<br/>              style={isActive === true ? { backgroundColor: &quot;#3a7a11&quot; } : undefined}<br/>              onClick={() =&gt; {<br/>                props.onCarSelected(row.original);<br/>              }}<br/>            &gt;<br/>              {row.getVisibleCells().map((cell) =&gt; (<br/>                &lt;td key={cell.id}&gt;<br/>                  {flexRender(cell.column.columnDef.cell, cell.getContext())}<br/>                &lt;/td&gt;<br/>              ))}<br/>              &lt;td&gt;<br/>                &lt;CarRowContextMenu carRow={row.original} /&gt;<br/>              &lt;/td&gt;<br/>            &lt;/tr&gt;<br/>          );<br/>        })}<br/>&lt;/tbody&gt;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; context menu added as a separate &lt;td&gt;</span> </div> </div></div>



<p>Additionally, to make it work, we need an additional table&#8217;s header placeholder:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="14-15" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">&lt;thead&gt;<br/>        {table.getHeaderGroups().map((headerGroup) =&gt; (<br/>          &lt;tr key={headerGroup.id}&gt;<br/>            {headerGroup.headers.map((header) =&gt; (<br/>              &lt;th key={header.id}&gt;<br/>                {header.isPlaceholder<br/>                  ? null<br/>                  : flexRender(<br/>                      header.column.columnDef.header,<br/>                      header.getContext()<br/>                    )}<br/>              &lt;/th&gt;<br/>            ))}<br/>            {/* placeholder header for context menu */}<br/>            &lt;th&gt;&lt;/th&gt;<br/>          &lt;/tr&gt;<br/>        ))}<br/>&lt;/thead&gt;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; placeholder &lt;th&gt; for context menu</span> </div> </div></div>



<p>That&#8217;s it! Our `react-table` table with row selection support and the meatballs menu works like a charm now:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="493" data-attachment-id="4322" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/5_contextmenufinalversion-1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?fit=1150%2C497&amp;ssl=1" data-orig-size="1150,497" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="5_ContextMenuFinalVersion-1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?fit=1150%2C497&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?fit=1150%2C497&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?resize=1140%2C493&#038;ssl=1" alt="react-table table with row selection and meatballs menu - final version" class="wp-image-4322" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?w=1150&amp;ssl=1 1150w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?resize=768%2C332&amp;ssl=1 768w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>
</div>


<h2 class="wp-block-heading">Meatballs menu with react-table &#8211; source code</h2>



<p>You can find the complete <a href="https://github.com/dsibinski/react-table-context-menu">source code here</a>. I hope you find it useful! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-1" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1767134900" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-1" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->
<p>The post <a href="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/">Adding Meatballs Menu To React-Table Rows</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4302</post-id>	</item>
		<item>
		<title>10 VS Code Extensions I Couldn&#8217;t Live Without</title>
		<link>https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/</link>
					<comments>https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Thu, 19 Jan 2023 18:57:07 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[vscode]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4232</guid>

					<description><![CDATA[<p>I love VS Code 😍 There&#8217;s no better web code editor out there for me. Today, I&#8217;m going to share with you the 10 VS Code extensions that make my life easier. I can&#8217;t imagine coding without them. Let&#8217;s dive into it! Prettier My number one. I was a bit skeptical initially, but today I&#8230;</p>
<p>The post <a href="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/">10 VS Code Extensions I Couldn&#8217;t Live Without</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>I love VS Code <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60d.png" alt="😍" class="wp-smiley" style="height: 1em; max-height: 1em;" /> There&#8217;s no better web code editor out there for me. Today, I&#8217;m going to share with you the 10 VS Code extensions that make my life easier. I can&#8217;t imagine coding without them. Let&#8217;s dive into it!</p>



<span id="more-4232"></span>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode">Prettier</a></h2>



<p>My number one. I was a bit skeptical initially, but today I can&#8217;t imagine my life without Prettier <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /> For those unfamiliar with the tool, Prettier is an <em>opinionated</em> code formatted. Opinionated, because it allows for almost no configuration. It reformats your code automatically based on its own rules.</p>



<p>It may sound scary and limiting, but it really frees your mind. Suddenly you forget about all those <em>spaces vs tabs wars</em> and discussions on which column shall the code be wrapped. Prettier does that all automatically. It can even be done on saving the file! Just take a look, how beautiful that is:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="917" height="637" data-attachment-id="4236" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/1_vscode_prettier/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?fit=917%2C637&amp;ssl=1" data-orig-size="917,637" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1_VSCode_Prettier" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?fit=300%2C208&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?fit=917%2C637&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?resize=917%2C637&#038;ssl=1" alt="Gif presenting Prettier auto-formatting on saving the file in VS Code" class="wp-image-4236"/></figure>
</div>


<p></p>



<p>It is also very easy to <a href="https://prettier.io/docs/en/precommit.html">execute prettier formatting with pre-commit git hook</a>, so the checked-in code is always formatted, even if some team members don&#8217;t use it in their IDE.</p>



<p>Never again without Prettier! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets">ES7+ React/Redux/React-Native snippets</a></h2>



<p>This is one of the first VS Code extensions I installed. It&#8217;s simple, yet very powerful! This plugin adds multiple JavaScript/React snippets. I create many new components every day. Snippets make it very easy:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="932" height="487" data-attachment-id="4239" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/2_vscode_react_snippets/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?fit=932%2C487&amp;ssl=1" data-orig-size="932,487" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2_VSCode_React_snippets" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?fit=300%2C157&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?fit=932%2C487&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?resize=932%2C487&#038;ssl=1" alt="One of the VS Code extensions that adds React snipets" class="wp-image-4239"/></figure>
</div>


<p></p>



<p>I recommend trying this extension out and exploring its built-in snippets.</p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=adrianwilczynski.csharp-to-typescript">C# to TypeScript</a></h2>



<p>This one is a time-saver for .NET developers working with TypeScript. It allows generating TypeScript interfaces from C# classes. You can simply copy a C# <code>class</code> and paste is as a TypeScript&#8217;s <code>interface</code> in Visual Studio Code:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1005" height="431" data-attachment-id="4241" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/3_vscode_csharp_to_typescript-1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?fit=1005%2C431&amp;ssl=1" data-orig-size="1005,431" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3_VSCode_CSharp_to_TypeScript-1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?fit=300%2C129&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?fit=1005%2C431&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?resize=1005%2C431&#038;ssl=1" alt="" class="wp-image-4241"/></figure>
</div>


<p></p>



<p>You can also paste using keyboard shortcuts. There&#8217;s even <a href="https://www.nuget.org/packages/CSharpToTypeScript.CLITool/">a CLI tool</a> available in case you&#8217;d want to automate this process. This extension doesn&#8217;t support all scenarios (especially no support for <code>records</code> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f62d.png" alt="😭" class="wp-smiley" style="height: 1em; max-height: 1em;" />), but it still covers most simple copy-paste cases. By the way, synchronizing C# with TypeScript types is one of the challenges for full stack .NET developers.</p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=mattpocock.ts-error-translator">Total TypeScript</a></h2>



<p>From all VS Code extensions I use, this is the only one that actually helps me learn on the job. It was called <em>TypeScript Error Translator</em> before, and this is what I initially used it for. It basically provides more meaningful, crowdsourced translations for often enigmatic TypeScript errors:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="821" height="280" data-attachment-id="4243" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/4_vscode_errorstranslator/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?fit=821%2C280&amp;ssl=1" data-orig-size="821,280" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4_VSCode_ErrorsTranslator" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?fit=300%2C102&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?fit=821%2C280&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?resize=821%2C280&#038;ssl=1" alt="TypeScript error translator shows nice explanation of TS error" class="wp-image-4243" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?w=821&amp;ssl=1 821w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?resize=300%2C102&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?resize=768%2C262&amp;ssl=1 768w" sizes="auto, (max-width: 821px) 100vw, 821px" /></figure>
</div>


<p></p>



<p>When a translation is missing, you can always contribute one. Additionally, as its new name suggests, it also lets you learn programming concepts directly inside your code. As soon as it finds a new concept in the code, a brief explanation is shown:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="485" data-attachment-id="4244" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/5_vscode_totaltypescriptlearning/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?fit=2082%2C986&amp;ssl=1" data-orig-size="2082,986" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="5_VSCode_TotalTypescriptLearning" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?fit=300%2C142&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?fit=1024%2C485&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=1024%2C485&#038;ssl=1" alt="" class="wp-image-4244" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=1024%2C485&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=300%2C142&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=768%2C364&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=1536%2C727&amp;ssl=1 1536w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=2048%2C970&amp;ssl=1 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p></p>



<p>It will stop highlighting this particular type of concept as soon as you mark it as learned. I think it&#8217;s a quite interesting way of learning on the job <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">ESLint</a></h2>



<p>This is the extension for integrating the most well-known linter for JavaScript into Visual Studio Code. <a href="https://eslint.org/">ESLint</a> is a static analyzer of your source code which helps you find problems quickly. It will highlight issues in your code for all loaded files from your workspace. I don&#8217;t feel there&#8217;s much to write about here &#8211; just <a href="https://eslint.org/">check their website</a> and give this extension a try <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client">Thunder Client</a></h2>



<p>Thunder Client is like <a href="https://www.postman.com/">Postman</a>, but built into VS Code. It lets you easily play with HTTP APIs. You can create your own collections of requests and export them to JSON, which can be checked into the source control.</p>



<p>Yet another very useful extension! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f44d.png" alt="👍" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=zenclabs.previewjs">Preview.js</a></h2>



<p>This is one of my recent discoveries. Preview.js allows to live-preview React, Solid or Vue components directly in VS Code. It also generates the initial props data, sometimes even for complex objects:</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="593" data-attachment-id="4261" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/6_vscode_previewjs-2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?fit=1467%2C763&amp;ssl=1" data-orig-size="1467,763" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="6_VSCode_PreviewJS-2" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?fit=1467%2C763&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?fit=1467%2C763&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?resize=1140%2C593&#038;ssl=1" alt="" class="wp-image-4261" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?w=1467&amp;ssl=1 1467w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?resize=768%2C399&amp;ssl=1 768w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>



<p></p>



<p>The plugin is also configurable for custom scenarios like external dependencies. You can find more information <a href="https://previewjs.com/docs/config">in the documentation</a>.</p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-2" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1767134900" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-2" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=WakaTime.vscode-wakatime">WakaTime</a></h2>



<p>If you like analytics, you should check this VS Code extension. WakaTime measures how much time you spend actually writing code. It shows exactly in which language you programmed for how much time. It also divides time entries into projects you worked on:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="1018" data-attachment-id="4264" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/7_wakatime_samplestats/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?fit=2062%2C1842&amp;ssl=1" data-orig-size="2062,1842" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="7_WakaTime_SampleStats" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?fit=2062%2C1842&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?fit=2062%2C1842&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=1140%2C1018&#038;ssl=1" alt="" class="wp-image-4264" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?w=2062&amp;ssl=1 2062w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=768%2C686&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=1536%2C1372&amp;ssl=1 1536w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=2048%2C1829&amp;ssl=1 2048w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>
</div>


<p></p>



<p>It&#8217;s interesting to get such insights &#8220;for free&#8221; and see how little time we spend <em>actually</em> coding <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" />Try it for yourself and let me know your results!</p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=zhuangtongfa.Material-theme">One Dark Pro</a></h2>



<p>Who never started a new project by installing fancy tools, text editors or choosing the best color theme for your IDE? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f604.png" alt="😄" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Of course &#8211; good colors = smooth -and pleasant &#8211; coding <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /> One Dark Pro is a must-have theme for my Visual Studio Code. I love it, it makes source code much more readable for me. What&#8217;s your favorite color theme for VS Code? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=vscode-icons-team.vscode-icons">vscode-icons</a></h2>



<p>Last but not least, a good friend to the One Dard Pro theme, legendary VS Code icons set. It has over 13M installs! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92f.png" alt="🤯" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Apart from getting +100 to fanciness <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" />, it makes icons more explicit in the explorer:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="599" height="612" data-attachment-id="4265" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/8_vscodeicons/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?fit=599%2C612&amp;ssl=1" data-orig-size="599,612" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="8_VSCodeIcons" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?fit=599%2C612&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?fit=599%2C612&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?resize=599%2C612&#038;ssl=1" alt="" class="wp-image-4265" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?w=599&amp;ssl=1 599w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?resize=50%2C50&amp;ssl=1 50w" sizes="auto, (max-width: 599px) 100vw, 599px" /></figure>
</div>


<p></p>



<p>As you can see, it even guesses the folder type based on its name &#8211; see <code>types</code> or <code>utils</code> folders&#8217; icons above.</p>



<p>Not critical, yet helpful. Undeniably adds a million to your professionalism as a web developer <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



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



<p>So, that&#8217;s it! The list of my 10 favorite Visual Studio Code extensions. </p>



<p>What about you? What are your must-have VS Code extensions? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/">10 VS Code Extensions I Couldn&#8217;t Live Without</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4232</post-id>	</item>
		<item>
		<title>How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</title>
		<link>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/</link>
					<comments>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Fri, 30 Apr 2021 04:33:47 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[npm ERR! enoent ENOENT]]></category>
		<category><![CDATA[npm error]]></category>
		<category><![CDATA[npm install package]]></category>
		<category><![CDATA[react]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3927</guid>

					<description><![CDATA[<p>I recently struggled for a while with an npm error thrown when executing npm install of some package. The error message was npm ERR! enoent ENOENT: no such file or directory, rename 'D:\\WebApp\\node_modules\\lz-string' -&#62; 'D:\\WebApp\\node_modules.lz-string.DELETE' Finally, I found a solution and a reason for that issue. The error occurred when I was trying to install&#8230;</p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/">How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[


<p>I recently struggled for a while with an <code>npm</code> error thrown when executing <code>npm install</code> of some package. The error message was <code>npm ERR! enoent ENOENT: no such file or directory, rename 'D:\\WebApp\\node_modules\\lz-string' -&gt; 'D:\\WebApp\\node_modules.lz-string.DELETE'</code></p>



<p>Finally, I found a solution and a reason for that issue.</p>



<span id="more-3927"></span>



<p>The error occurred when I was trying to install <code>@testing-library/react</code> npm package. It looked like that:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="404" data-attachment-id="3928" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/npm_rename_error/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=1360%2C536&amp;ssl=1" data-orig-size="1360,536" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="npm_rename_error" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=300%2C118&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=1024%2C404&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=1024%2C404&#038;ssl=1" alt="npm ERR! enoent ENOENT error in Visual Studio Code" class="wp-image-3928" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=1024%2C404&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=300%2C118&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=768%2C303&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=676%2C266&amp;ssl=1 676w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?w=1360&amp;ssl=1 1360w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>


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



<p>If you ever get this error, the hotfix is to follow these steps:</p>



<ul class="wp-block-list"><li>delete <code>node-modules</code> folder</li><li>run command <code>npm cache clean --force</code></li><li>run command <code>npm install</code></li><li>install the package again with <code>npm install your-package-name</code></li></ul>



<p>It should all work fine after that. </p>



<p>If these commands still don&#8217;t solve your issue, and you use <code>git</code> as a source control system, you can try the unbeatable <code>git clean -fdx</code> command. After that, run <code>npm install</code> again. Beware &#8211; it removes all files not checked in to git. It may also remove your IDE settings etc., so use with care.</p>



<p>But it&#8217;s only a hotfix, a solution <em>for now</em> to unblock you.</p>



<h2 class="wp-block-heading">Coldfix (solution)</h2>



<p>The real reason for this issue in my case turned out to be related to <code>jest</code>. However, not to the testing library itself, but to <a href="https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest" target="_blank" rel="noreferrer noopener">jest extension for Visual Studio Code</a>.</p>



<p>The reason for the issue is the jest tests runner working in the background. You can see that in the VS Code bottom bar:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="320" height="158" data-attachment-id="3929" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/vscode_jest_runner/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=320%2C158&amp;ssl=1" data-orig-size="320,158" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="vsCode_jest_runner" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=300%2C148&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=320%2C158&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?resize=320%2C158&#038;ssl=1" alt="Visual Studio Code - jest runner" class="wp-image-3929" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?w=320&amp;ssl=1 320w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?resize=300%2C148&amp;ssl=1 300w" sizes="auto, (max-width: 320px) 100vw, 320px" /></figure>
</div>


<p>The real solution is to disable jest runner when installing new packages. You can do it with a <code>Jest: Stop Runner</code> command in Ctrl+Shift+P:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="777" height="246" data-attachment-id="3930" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/vscode_jest_stop_runner/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=777%2C246&amp;ssl=1" data-orig-size="777,246" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="VSCode_jest_stop_runner" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=300%2C95&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=777%2C246&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=777%2C246&#038;ssl=1" alt="npm ERR! enoent ENOENT fix in Visual Studio by stopping jest runner" class="wp-image-3930" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?w=777&amp;ssl=1 777w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=300%2C95&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=768%2C243&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=676%2C214&amp;ssl=1 676w" sizes="auto, (max-width: 777px) 100vw, 777px" /></figure>
</div>


<p>I don&#8217;t know exactly why this is an issue. I guess jest runner is blocking some files in <code>node-modules</code>, so they cannot be renamed/processed. I hope it helps you too <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/">How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/feed/</wfw:commentRss>
			<slash:comments>13</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3927</post-id>	</item>
	</channel>
</rss>
