What Is React Concurrent Mode And Why You Will Love It?
Tags: react, javascript, webdev
Published: 2019-12-19 13:50:00

<p>Not long ago, Facebook introduced <a href="https://pl.reactjs.org/docs/concurrent-mode-intro.html">Concurrent Mode</a> (on the day of writing this article it is still experimental).</p>
<p>Concurrent Mode has been built with user and development experience in mind, but this time it looks like it really has a lot to offer, and is about to be a huge step forward for React.</p>
<p>There are many new use cases coming up almost every day, so if you found any functionality I didn’t mention in this article, add it in a comment. </p>
<h2><b>What is Concurrent Mode?<b/></h2>
<p>Concurrent Mode is a set of features that makes React faster and even more user (and development) friendly. </p>
<p>The plan is to enable React apps to be even more responsive by adjusting to the users’ device capabilities and the speed of the network.</p>
<p>React makers also proudly declare to make the developers’ life easier and help them <em>[…]make it as easy as possible to build apps that start fast and stay fast[…], no matter how big they grow.</em></p>
<img src="https://blog.pagepro.co/wp-content/uploads/2019/11/api-1024x576.png" alt="apis that make react concurrent diagram" class="wp-image-2044"/><figcaption> <a href="https://medium.com/well-red/get-ready-for-concurrent-rendering-in-react-120c2fdcd7a9">https://medium.com/well-red/get-ready-for-concurrent-rendering-in-react-120c2fdcd7a9</a>
<h2><b>Rendering</b></h2>
<p>If you think of the most important factor in user experience (from the development point of view) it will be rendering. The faster the rendering, the better the feeling of an app is. </p>
<p>But since a simple user may not feel the big difference between milliseconds, for the developer it means quite a lot, as he has to deal with much more rendering “behind the scenes”.</p>
<p>React became already popular thanks to Virtual DOM - it scans and detects only the changes in the DOM, without a need to build the entire DOM structure from the scratch.</p>
<p>However, when you want to render any update, you cannot interrupt it, and you have to wait with new updates until the previous rendering is finished.</p>
<p>Well, not anymore.</p>
<p>In <strong>Concurrent Mode</strong>, rendering is interruptible.</p>
<img src="https://blog.pagepro.co/wp-content/uploads/2019/11/react-concurrent-mode.png" alt="difference between react sync and react concurrent mode" class="wp-image-1909"/><figcaption>https://blog.frontity.org/seo-for-headless-wordpress-themes/react-concurrent-mode/</figcaption>
<p>Simply speaking, concurrent rendering is when you can render component trees without any harm and blocks in the main UI thread.</p>
<p>This enables you to start the new UI updates before the old one is finished.</p>
<h2><b>Suspense for Data Fetching</b></h2>
<p>One of the most exciting things in Concurrent Mode is <a href="https://reactjs.org/docs/concurrent-mode-suspense.html#what-is-suspense-exactly">Suspense </a>for Data Fetching. </p>
<p>Now you can use &lt;Suspense&gt; component to wait not only for a code to load but actually, for everything else you want to fetch, declaratively. </p>
<p>In other words, you don’t have to wait for the response to come back before you start rendering. You can now start rendering and fetching at the same time.</p>
<p>It can also solve many race conditions, improve the general user experience, and enable new features that weren’t possible before.</p>
<h2><b>Summary</b></h2>
<p>Concurrent Mode already opens many interesting doors, and I suppose this is just the beginning. </p>
<p>The goal of <a href="https://blog.pagepro.co/2019/11/22/7-reasons-why-you-should-be-using-react/">making the web faster and more user-friendly</a> is the same since always, but this time, <a href="https://blog.pagepro.co/2019/11/15/reasons-to-use-react-in-2020/">the future becomes incredibly interesting</a> thanks to React and its features.</p>
<p>For more usage tips go to this <a href="https://pl.reactjs.org/docs/concurrent-mode-patterns.html">webpage</a>. </p>