<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sabesan Sathananthan</title>
    <description>The latest articles on Forem by Sabesan Sathananthan (@thesabesan).</description>
    <link>https://forem.com/thesabesan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F289881%2F02ef1352-e9a6-4c87-8448-002f4993d0a4.jpg</url>
      <title>Forem: Sabesan Sathananthan</title>
      <link>https://forem.com/thesabesan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thesabesan"/>
    <language>en</language>
    <item>
      <title>Five methods for JavaScript to detect mobile browsers</title>
      <dc:creator>Sabesan Sathananthan</dc:creator>
      <pubDate>Fri, 15 Oct 2021 08:34:44 +0000</pubDate>
      <link>https://forem.com/thesabesan/five-methods-for-javascript-to-detect-mobile-browsers-1jhc</link>
      <guid>https://forem.com/thesabesan/five-methods-for-javascript-to-detect-mobile-browsers-1jhc</guid>
      <description>&lt;p&gt;If you are a front-end developer you need to know whether the user is using a mobile browser or a desktop browser. Based on &lt;a href="https://stackoverflow.com/questions/11381673/detecting-a-mobile-browser" rel="noopener noreferrer"&gt;StackOverflow&lt;/a&gt; , this article sorts out five methods for JavaScript to detect mobile browsers.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgv60x65dkj0olgo0p7he.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgv60x65dkj0olgo0p7he.jpg" alt="Photo by Jenny Smith on Unsplash" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1. &lt;code&gt;navigator.userAgent&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The easiest way is to analyze the user agent string of the browser, which contains the device information.&lt;br&gt;
JS gets this string through the &lt;code&gt;navigator.userAgent&lt;/code&gt; property, as long as it contains keywords such as &lt;code&gt;mobi&lt;/code&gt;, &lt;code&gt;android&lt;/code&gt;, &lt;code&gt;iphone&lt;/code&gt;, etc. It can be identified as a mobile device.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;


&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Mobi|Android|iPhone/i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAgent&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The current device is a mobile device&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Another way of writing&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAgent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Mobi/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
  &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAgent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Android/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
  &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAgent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/iPhone/i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The current device is a mobile device&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The advantage of this method is simple and convenient, but the disadvantage is that it is unreliable. The user can modify this string to make the mobile browser pretend to be a desktop browser.&lt;/p&gt;

&lt;p&gt;Chromium browsers also have a &lt;code&gt;navigator.userAgentData&lt;/code&gt; property, which has a similar effect. The difference is that it parses the user agent string into an object and the &lt;code&gt;mobile&lt;/code&gt; attribute of the object returns a Boolean value indicating whether the user is using a mobile device.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isMobile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAgentData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mobile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that Apple's Safari browser and Firefox browser do not support this attribute. you can check the &lt;a href="https://caniuse.com/mdn-api_navigator_useragentdata" rel="noopener noreferrer"&gt;CanIUse website&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;In addition, there is an abolished &lt;a href="https://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today" rel="noopener noreferrer"&gt;&lt;code&gt;navigator.platform&lt;/code&gt;&lt;/a&gt; property, which is supported by all browsers, so it can also be used. It returns a string that represents the user's operating system.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Android|iPhone|iPad|iPod/i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The current device is a mobile device&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;2. &lt;code&gt;window.screen&lt;/code&gt;, &lt;code&gt;window.innerWidth&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Another method is to determine whether it is a mobile phone by the width of the screen.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;window.screen&lt;/code&gt; object returns the screen information of the user device. The &lt;code&gt;width&lt;/code&gt; property of this object is the width of the screen (in pixels).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The current device is a mobile device&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example, if the screen width &lt;code&gt;window.screen.width&lt;/code&gt; is less than 500 pixels, it is considered a mobile phone. The disadvantage of this method is that if the mobile phone is used horizontally, it cannot be recognized.&lt;/p&gt;

&lt;p&gt;Another property window.innerWidth returns the width of the visible part of the webpage in the browser window, which is more suitable for specifying the style of the webpage under different widths.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBrowserWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;991&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1199&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;md&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;3. &lt;code&gt;window.orientation&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The third method is to detect the orientation of the screen. The phone screen can change its orientation (horizontal or vertical) at any time, but it cannot be done on desktop devices.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;window.orientation&lt;/code&gt; property is used to get the current orientation of the screen. Only mobile devices have this property. Desktop devices will return &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orientation&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The current device is a mobile device&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note that the iPhone’s Safari browser does not support this attribute.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;4. touch event&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The fourth method is that the DOM element of the mobile browser can specify the listener function for the touch event through the ontouchstart attribute. Desktop devices do not have this attribute.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isMobile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ontouchstart&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Alternative way of writing&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isMobile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TouchEvent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;5. &lt;code&gt;window.matchMedia()&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The last method is to judge with CSS.&lt;/p&gt;

&lt;p&gt;CSS uses media query (media query) to specify responsive styles for web pages.If a certain media query statement for a mobile phone takes effect, the current device can be considered as a mobile device.The &lt;code&gt;window.matchMedia()&lt;/code&gt; method accepts a CSS media query statement as a parameter to determine whether this statement is valid.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isMobile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matchMedia&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;only screen and (max-width: 760px)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example, the parameter of &lt;code&gt;window.matchMedia()&lt;/code&gt; is a CSS query statement, which means that it only takes effect on devices with a screen width of no more than 760 pixels. It returns an object whose &lt;code&gt;matches&lt;/code&gt; attribute is a boolean value. If it is &lt;code&gt;true&lt;/code&gt;, it means that the query is valid and the current device is a mobile phone.&lt;/p&gt;

&lt;p&gt;In addition to judging by the screen width, you can also judge by the accuracy of the pointer.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isMobile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matchMedia&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;(pointer:coarse)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example, the CSS statement &lt;code&gt;pointer:coarse&lt;/code&gt; indicates that the pointer of the current device is imprecise. Since the mobile phone does not support the mouse, it only supports touch, so it meets this condition.&lt;/p&gt;

&lt;p&gt;Some devices support multiple pointers, such as mouse and touch at the same time. &lt;code&gt;pointer:coarse&lt;/code&gt; is only used to determine the primary pointer, and there is also an &lt;code&gt;any-pointer&lt;/code&gt; command to determine all pointers.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isMobile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matchMedia&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;(any-pointer:coarse)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example, &lt;code&gt;any-pointer:coarse&lt;/code&gt; means that among all the pointers. As long as one pointer is inaccurate, it meets the query conditions.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;6. Toolkit&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In addition to the above methods, you can also use a toolkit written by others.Recommended here is &lt;a href="https://www.npmjs.com/package/react-device-detect" rel="noopener noreferrer"&gt;react-device-detect&lt;/a&gt;, which supports device detection with multiple granularities.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isMobile&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-device-detect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isMobile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The current device is a mobile device&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Handle ES6 modules in Node.Js</title>
      <dc:creator>Sabesan Sathananthan</dc:creator>
      <pubDate>Fri, 23 Oct 2020 10:14:07 +0000</pubDate>
      <link>https://forem.com/thesabesan/how-to-handle-es6-modules-in-node-js-hdn</link>
      <guid>https://forem.com/thesabesan/how-to-handle-es6-modules-in-node-js-hdn</guid>
      <description>&lt;p&gt;Learning the JavaScript language, you will find that it has two types of modules.&lt;/p&gt;

&lt;p&gt;One is the ES6 module, referred to as ESM; the other is Node.js dedicated CommonJS module, referred to as CJS. The two modules are not compatible.&lt;/p&gt;

&lt;p&gt;Many people use Node.js and only &lt;code&gt;require()&lt;/code&gt; load modules, but don't know what to do when they encounter ES6 modules. This article will talk about how to use ES6 modules in Node.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flda7clcx7t96q61c2sa2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flda7clcx7t96q61c2sa2.jpg" alt="Alt Text" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;
Source: https://www.zymr.com/es6-impact-node-js/ 






&lt;h2&gt;
  
  
  &lt;strong&gt;The difference between the two modules&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There is a big difference between ES6 modules and CommonJS modules. The syntax of the module being loaded is different. CJS modules are loaded with &lt;code&gt;require()&lt;/code&gt; and exported with &lt;code&gt;module.exports&lt;/code&gt;. But ESM modules are loaded with &lt;code&gt;import&lt;/code&gt; and exported with &lt;code&gt;export&lt;/code&gt;. &lt;br&gt;
&lt;code&gt;require()&lt;/code&gt; is doing synchronous loading, the following statements must wait for this statement to be executed before it is executed. But &lt;code&gt;import&lt;/code&gt; is doing asynchronous loading, to be precise it has a static code analysis and dependency resolution phase.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnxkx0lgjvlzksv4i2yd8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnxkx0lgjvlzksv4i2yd8.jpg" alt="Alt Text" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;
Source: https://twitter.com/Manz/status/1265341200007036929






&lt;h2&gt;
  
  
  &lt;strong&gt;The distinction of Node.js&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Node.js requires ES6 modules to use the &lt;code&gt;.mjs&lt;/code&gt; file extension. In other words, as long as the &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;export&lt;/code&gt; is used in the script file, the &lt;code&gt;.mjs&lt;/code&gt; file extension should be used. When Node.js encounters a &lt;code&gt;.mjs&lt;/code&gt; file, it considers it to be an ES6 module, and strict mode is enabled by default, and it is not necessary to specify it at the top of each module file &lt;code&gt;"use strict"&lt;/code&gt;. If you do not want to change the file extension to &lt;code&gt;.mjs&lt;/code&gt;, you can specify the &lt;code&gt;type&lt;/code&gt; field as a &lt;code&gt;module&lt;/code&gt; in the project file's  &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JSON
{
   "type": "module"
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once set, the JS scripts in the directory will be interpreted as ES6 modules.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 bash
# Interpreted as ES6 Module
$ node my-app.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you still want to use the CommonJS module at this time, you need to change the file extension of the CommonJS script to &lt;code&gt;.cjs&lt;/code&gt;.  If there is no &lt;code&gt;type&lt;/code&gt; field, or the &lt;code&gt;type&lt;/code&gt; field is &lt;code&gt;commonjs&lt;/code&gt;, the &lt;code&gt;.js&lt;/code&gt; script will be interpreted as a CommonJS module.&lt;br&gt;
&lt;strong&gt;Summarized in one sentence&lt;/strong&gt;: &lt;code&gt;.mjs&lt;/code&gt; files are always loaded as ES6 modules, &lt;code&gt;.cjs&lt;/code&gt; files are always loaded as CommonJS modules, and the loading of &lt;code&gt;.js&lt;/code&gt; files depends on the settings of the &lt;code&gt;type&lt;/code&gt; field inside the &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note&lt;/em&gt;&lt;/strong&gt;: Try not to mix ES6 modules and CommonJS modules. The &lt;code&gt;require()&lt;/code&gt; cannot load the &lt;code&gt;.mjs&lt;/code&gt; file, and an error will be reported. Only the &lt;code&gt;import&lt;/code&gt; can load the &lt;code&gt;.mjs&lt;/code&gt; file. Conversely, &lt;code&gt;require()&lt;/code&gt; cannot be used in &lt;code&gt;.mjs&lt;/code&gt; files, they should use &lt;code&gt;import&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;CommonJS module loads ES6 modules&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;require()&lt;/code&gt; cannot load ES6 modules and an error will be reported in the CommonJS module. You can only use the &lt;code&gt;import()&lt;/code&gt; method to load ESM.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
(async () =&amp;gt; {
  await import('./my-app.mjs');
})();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code can be run in the CommonJS module.&lt;/p&gt;

&lt;p&gt;One reason why &lt;code&gt;require()&lt;/code&gt; does not support ES6 modules is that, it is loaded synchronously, and the top-level &lt;code&gt;await&lt;/code&gt; statement can be used inside ES6 modules, which would cause the module not to be able to be loaded synchronously.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;ES6 modules load CommonJS modules&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the ES6 module, &lt;code&gt;import&lt;/code&gt; can load CommonJS modules, but the CJS module should be loaded altogether, and can not just load a specific single exported object.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
// Correct
import packageMain from 'commonjs-package';

// Report an error
import { method } from 'commonjs-package';


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is because ES6 modules need to support static code analysis, and the export interface of CommonJS modules is &lt;code&gt;module.exports&lt;/code&gt;, which is an object that cannot be statically analyzed, so it can only be loaded as a whole.&lt;br&gt;
To load a specific single exported object, it can be written as follows.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
import packageMain from 'commonjs-package';
const { method } = packageMain;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl5tyuc00v836gn9ra7jx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl5tyuc00v836gn9ra7jx.png" alt="Alt Text" width="800" height="689"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Support two formats of modules at the same time&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;It is easy for a module to support both CommonJS and ES6 formats. If the original module is in ES6 format, you need to give an overall output interface, such as &lt;code&gt;export default obj&lt;/code&gt;, so that CommonJS can be loaded with &lt;code&gt;import()&lt;/code&gt;.&lt;br&gt;
If the original module is in CommonJS format, a packaging layer can be added.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
import cjsModule from '../index.js';
export const foo = cjsModule.foo; 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code first enters the CommonJS module as a whole and then exports the named interface as needed. You can change the extension of this file to &lt;code&gt;.mjs&lt;/code&gt;, or put it in a subdirectory, and put a separate &lt;code&gt;package.json&lt;/code&gt; file in this subdirectory, specifying &lt;code&gt;{type: "module" }&lt;/code&gt;.&lt;br&gt;
Another approach is to specify the respective loading entry of the two format modules in the &lt;code&gt;exports&lt;/code&gt; field of the &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="s2"&gt;"exports"&lt;/span&gt;&lt;span class="err"&gt;：&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"require"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./index.js"&lt;/span&gt;&lt;span class="err"&gt;，&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"import"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./esm/wrapper.js"&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code specifies &lt;code&gt;require()&lt;/code&gt; and &lt;code&gt;import&lt;/code&gt;, and loading the module will automatically switch to a different entry file.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>devops</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Embed Dev.to as a blog on your React Website</title>
      <dc:creator>Sabesan Sathananthan</dc:creator>
      <pubDate>Mon, 17 Aug 2020 19:53:11 +0000</pubDate>
      <link>https://forem.com/thesabesan/embed-dev-to-as-a-blog-on-your-react-website-3l8c</link>
      <guid>https://forem.com/thesabesan/embed-dev-to-as-a-blog-on-your-react-website-3l8c</guid>
      <description>&lt;p&gt;I have already implemented Medium as a blog on my &lt;a href="https://sabesansathananthan.now.sh/blog" rel="noopener noreferrer"&gt;website&lt;/a&gt;. In the beginning, I used jQuery to fetch JSON objects. I have mentioned that method in my &lt;a href="https://medium.com/datadriveninvestor/embed-medium-as-a-blog-on-your-site-54a1b49cbe16" rel="noopener noreferrer"&gt;medium article&lt;/a&gt;. But I am not satisfied with that method. I don’t like that user interface. So I thought about using React for better &lt;a href="https://react-medium-blog.firebaseapp.com/" rel="noopener noreferrer"&gt;user experience&lt;/a&gt;. Then I avoided jQuery and implemented it in pure JavaScript and &lt;a href="https://medium.com/datadriveninvestor/embedded-medium-as-a-blog-on-your-react-website-f01be289e151" rel="noopener noreferrer"&gt;React Js&lt;/a&gt;. Here you could see the live &lt;a href="https://react-dev-blog.web.app/" rel="noopener noreferrer"&gt;demo&lt;/a&gt; of this react application. This is my 2nd dev.to article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzxw5motyotsoym0olnhg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzxw5motyotsoym0olnhg.jpg" alt="Alt Text" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I downloaded the &lt;a href="https://designrevision.com/downloads/shards-dashboard-lite-react/?ref=madewithreact" rel="noopener noreferrer"&gt;shards dashboard lite&lt;/a&gt; and was impressed by its blog page. Then I thought, why can’t I implement that in my blog page. After that, I come up with a solution. But that didn’t work. Then I tried a new method that worked. Now I finished that and have a repo in GitHub. In this article, I am going to say how to start from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step1: Setup React Project
&lt;/h2&gt;

&lt;p&gt;First, you need to create a react application. For that run the following command in your shell/terminal in a specific folder (e.g., desktop )&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx create-react-app dev.to-post


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;A new folder will be created, and it will be named as a dev.to-post. From this step, our application is bootstrapped with &lt;a href="https://github.com/facebook/create-react-app" rel="noopener noreferrer"&gt;Create React App&lt;/a&gt;. Then open that project in your IDE. I am personally using the VS Code IDE.&lt;/p&gt;


&lt;h2&gt;
  
  
  Step2: Delete Unwanted Files
&lt;/h2&gt;

&lt;p&gt;You need to delete some files and organize the files for the development after you open the folder in your IDE. Therefore you need to go to the src folder and delete Logo.svg, App.css, index.css, and App.test.js files. And create the following folders inside the src folder named components, assets, helpers, and utils, then move your serviceWorker.js into the helper’s folder. App.js file into the Components folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0jodj1zqcfkaawepzdn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0jodj1zqcfkaawepzdn8.png" alt="Alt Text" width="326" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now open the index.js file and delete the following snippet in index.js file.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
import ‘./index.css’;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then modify the App.js and serviceWorker.js files paths in index.js import like following.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
import App from ‘./components/App’;
import * as serviceWorker from ‘./helpers/serviceWorker’;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Go to the Components folder and open the App.js. Delete the return part of the App function. Now your file organization will be like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh7vynex95pzo1ljxvdke.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh7vynex95pzo1ljxvdke.png" alt="Alt Text" width="327" height="458"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 3: Install Shards React package
&lt;/h2&gt;

&lt;p&gt;You can install Shards React via NPM.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm i shards-react


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Once the package is installed, open the index.js and add the following snippet to the top of the code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
import ‘bootstrap/dist/css/bootstrap.min.css’;
import ‘shards-ui/dist/css/shards.min.css’;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then you need to add this &lt;a href="https://github.com/sabesansathananthan/react-dev.to-blog/blob/master/src/assets/shards-dashboards.1.1.0.min.css"&gt;CSS&lt;/a&gt; file into the assets folder. Once you add the file inside the assets folder, import the CSS file in index.js.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 JavaScript
import ‘./assets/shards-dashboards.1.1.0.min.css’;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Step4: Fetch data from Dev.to
&lt;/h2&gt;

&lt;p&gt;Then create a new file named slider.js inside the components folder. Add the following code in the slider.js file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;replace the &lt;code&gt;thepracticaldev&lt;/code&gt; with your dev.to username. Then create the utils folder and create the Totext.js file inside the utils folder. Add the following snippet in the Totext.js file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Then create the ShortenText.js file inside the utils folder and add the following snippet in the ShortenText.js file&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Create BlogCard.js file inside the Component folder and add the following snippet in the BlogCard.js file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally, Render the Slider component in App.js, as shown below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Here I have showcased four steps to embed dev.to as a blog on your React Website. If you use this, you won’t spend money to embed dev.to. However, when you follow these methods, you will be unable to show your related dev.to posts section in your blog. You can clone the Repo from this &lt;a href="https://github.com/sabesansathananthan/react-dev.to-blog"&gt;link&lt;/a&gt;. If this repo is useful for you don't forget to give a star ⭐️. &lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/sabesansathananthan" rel="noopener noreferrer"&gt;
        sabesansathananthan
      &lt;/a&gt; / &lt;a href="https://github.com/sabesansathananthan/react-dev.to-blog"&gt;
        react-dev.to-blog
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      React app for fetch dev.to posts and render in cards
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;React Dev Blog&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;
Aim of this project is embed Dev as a blog on your React website
&lt;/p&gt;

&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/sabesansathananthan/react-dev.to-blog./docs/Screenshot.png"&gt;&lt;img alt="UI" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fsabesansathananthan%2Freact-dev.to-blog.%2Fdocs%2FScreenshot.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
&lt;a href="https://github.com/sabesansathananthan/react-dev.to-blog/blob/master/.github/LICENSE"&gt;
      &lt;img alt="licence" src="https://camo.githubusercontent.com/95c99450c1e52509bd0b5ae6e93bcf0cff5d798e08a5287b621cc3a9d5affa84/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7361626573616e73617468616e616e7468616e2f72656163742d6465762e746f2d626c6f67"&gt;
    &lt;/a&gt;&lt;a href="https://github.com/sabesansathananthan/react-dev.to-blog"&gt;
      &lt;img alt="GitHub package.json version" src="https://camo.githubusercontent.com/054e50af4328a4fad894aa0b073e10b680cdcae210f764a7ae07667517cd7eaf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7061636b6167652d6a736f6e2f762f7361626573616e73617468616e616e7468616e2f72656163742d6465762e746f2d626c6f67"&gt;
    &lt;/a&gt;&lt;a href="https://github.com/sabesansathananthan/react-dev.to-blog"&gt;
      &lt;img alt="GitHub repo size" src="https://camo.githubusercontent.com/be5291d85309fefae544089033b859789350cd5cc7af7d2740995138afc50ca9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7265706f2d73697a652f7361626573616e73617468616e616e7468616e2f72656163742d6465762e746f2d626c6f673f636f6c6f723d666636396234"&gt;
    &lt;/a&gt;&lt;a href="https://twitter.com/intent/tweet?text=Wow,%20I%20used%20react-dev.to-blog.%20That%20is%20excellent.%20Thank%20you%20@TheSabesan" rel="nofollow"&gt;
      &lt;img alt="Twitter URL" src="https://camo.githubusercontent.com/a558ddf3f12dcceae74c44a03eaeb6d83ecbafaf5d9503cd802bfdc2441d0948/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c3f7374796c653d736f6369616c2675726c3d6874747073253341253246253246747769747465722e636f6d2532465468655361626573616e"&gt;
    &lt;/a&gt;
&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;🛠️ Built with&lt;/h2&gt;
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/" rel="nofollow noopener noreferrer"&gt;React JS&lt;/a&gt; - Front-End JavaScript library&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://designrevision.com/docs/shards-react/getting-started" rel="nofollow noopener noreferrer"&gt;Shards React&lt;/a&gt; - React UI Framework&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Installation&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;To setup the app for development on your local machine, please follow the instructions below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Clone the repo to your machine&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;git clone https://github.com/sabesansathananthan/react-dev.to-blog.git
&lt;span class="pl-c1"&gt;cd&lt;/span&gt; react-dev.to-blog&lt;/pre&gt;

&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install packages&lt;/p&gt;
&lt;p&gt;If you use &lt;code&gt;npm&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;npm install&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;If you use &lt;code&gt;yarn&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;yarn&lt;/pre&gt;

&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Change username&lt;/p&gt;
&lt;p&gt;Use your dev.to username👤 instead of &lt;code&gt;thepracticaldev&lt;/code&gt;. in &lt;a href="https://github.com/sabesansathananthan/react-dev.to-blog./src/components/Slider.js"&gt;Slider.js&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight highlight-source-js notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-s1"&gt;blogURL&lt;/span&gt; &lt;span class="pl-c1"&gt;=&lt;/span&gt; &lt;span class="pl-s"&gt;"https://api.rss2json.com/v1/api.json?rss_url=https://dev.to/feed/thepracticaldev"&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the development server&lt;/p&gt;
&lt;p&gt;If you use &lt;code&gt;npm&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;npm start&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;If you use &lt;code&gt;yarn&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;yarn start&lt;/pre&gt;

&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Visit &lt;a href="http://localhost:3000" rel="nofollow noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;&lt;a href="https://react-dev-blog.web.app/" rel="nofollow noopener noreferrer"&gt;Live Demo&lt;/a&gt;&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;Don't forget to give a star ⭐ for this repo 🙂&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://raw.githubusercontent.com/matiassingers/awesome-readme/master/icon.png"&gt;&lt;img alt="Article" height="25px" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fmatiassingers%2Fawesome-readme%2Fmaster%2Ficon.png"&gt;&lt;/a&gt; Related Article&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://dev.to/thesabesan/embed-dev-to-as-a-blog-on-your-react-website-3l8c" rel="nofollow"&gt;Embed Dev.to as a blog on your React Website&lt;/a&gt; - &lt;em&gt;Sabesan Sathananthan&lt;/em&gt;&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Similar Projects&lt;/h2&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/sabesansathananthan/React-Medium-Blog" rel="noopener noreferrer"&gt;React-Medium-Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/sabesansathananthan/material-ui-medium-blog" rel="noopener noreferrer"&gt;Material-UI-Medium-Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/sabesansathananthan/react-medium-blog-v2" rel="noopener noreferrer"&gt;react-medium-blog-v2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;📄 License&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;This project is licensed under the MIT License…&lt;/p&gt;
&lt;/div&gt;


&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/sabesansathananthan/react-dev.to-blog"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Happy Coding 😊 !!!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Beautify Your GitHub Profile README</title>
      <dc:creator>Sabesan Sathananthan</dc:creator>
      <pubDate>Sat, 15 Aug 2020 16:58:02 +0000</pubDate>
      <link>https://forem.com/thesabesan/beautify-your-github-profile-readme-10cf</link>
      <guid>https://forem.com/thesabesan/beautify-your-github-profile-readme-10cf</guid>
      <description>&lt;p&gt;I don't know how many of you know about adding a GitHub profile README. GitHub recently released a feature that allows users to create a profile-level README to display prominently on their GitHub profile. The GitHub profile-level README feature allows more content than the bio profile, supports markdown, which means you can play more visually with the content, and the README becomes considerably more accessible as it is positioned above pinned repositories and takes up as much space above the web page fold as you want. In this article, I am going to instruct you on how to create a GitHub profile README and how to take it to the next level. You are going to add an automated feed of your blog posts and GitHub stats.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsydhzlwpyp77rd6d6cl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsydhzlwpyp77rd6d6cl.png" alt="Alt Text" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbtpf3eu98jcnpo8kien6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbtpf3eu98jcnpo8kien6.png" alt="Alt Text" width="800" height="580"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  First things first
&lt;/h1&gt;

&lt;p&gt;Customizing your GitHub profile page with a README file is as simple as creating a repo that is named the same as your username. For example, my GitHub username is &lt;a href="https://github.com/sabesansathananthan" rel="noopener noreferrer"&gt;sabesansathananthan&lt;/a&gt; so I created a new repository with the name sabesansathananthan. &lt;em&gt;Note: at the time of this writing, in order to access the profile README feature, the letter-casing must match your GitHub username and your GitHub repository visibility should be in public.&lt;/em&gt; Now, you are going to create a folder on your computer named the same as your GitHub username. Then you have that open in vs code. Then you create a README.md file inside that folder. README.md is a markdown file. The point of this article isn't to teach about markdown. I am going to assume that you have a basic knowledge of markdown and if not just copy and paste my code and modify it to fit your needs. push your changes to your repository.Need to brush up on Markdown Syntax? &lt;a href="https://guides.github.com/pdfs/markdown-cheatsheet-online.pdf" rel="noopener noreferrer"&gt;Check out this Markdown Cheatsheet&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Add List of Blog Posts
&lt;/h1&gt;

&lt;p&gt;To add list of your blog posts you are going to use a GitHub action called &lt;a href="https://github.com/gautamkrishnar/blog-post-workflow" rel="noopener noreferrer"&gt;blog-post-workflow&lt;/a&gt; and this was created by &lt;a href="https://github.com/gautamkrishnar" rel="noopener noreferrer"&gt;Gautam krishna R&lt;/a&gt;. You need to add &lt;code&gt;&amp;lt;!-- MEDIUM:START --&amp;gt; &amp;lt;!-- MEDIUM:END --&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;!-- DEVTO:START --&amp;gt; &amp;lt;!-- DEVTO:END --&amp;gt;&lt;/code&gt; comment to your README file if you have medium and dev.to. I already added those comments in the above gist. so the github action workflow will add the actual blog posts in between these comments. Now go to your repository and click the actions tab and click on the "set up a workflow yourself' link.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8zxccef2b2bzgnq8e63y.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8zxccef2b2bzgnq8e63y.JPG" alt="Alt Text" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now It will redirect to a new page and you can see the window below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F75yrstt1ud5s1m4baoeo.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F75yrstt1ud5s1m4baoeo.JPG" alt="Alt Text" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;change the file name as &lt;code&gt;blog-post-workflow.yml&lt;/code&gt; then copy and paste the below code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the &lt;code&gt;feed-list&lt;/code&gt; option replace the &lt;code&gt;username&lt;/code&gt; with your dev.to and medium username and click the button &lt;code&gt;start commit&lt;/code&gt; and then click the button &lt;code&gt;Commit new file.&lt;/code&gt; click the &lt;code&gt;Actions&lt;/code&gt; tab again and click the &lt;code&gt;Latest blog post workflow&lt;/code&gt;. If you click the &lt;code&gt;Run workflow&lt;/code&gt; button there will be a popup open and click the Green color &lt;code&gt;Run workflow&lt;/code&gt; button in the popup window. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn179fhu5ivtai13mhuiv.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn179fhu5ivtai13mhuiv.JPG" alt="Alt Text" width="800" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Add GitHub Stats
&lt;/h1&gt;

&lt;p&gt;Lastly, you are going to add GitHub stats. For this we are going to use &lt;a href="https://github.com/anuraghazra/github-readme-stats" rel="noopener noreferrer"&gt;github-readme-stats&lt;/a&gt; and this is from &lt;a href="https://github.com/anuraghazra" rel="noopener noreferrer"&gt;anuraghazra&lt;/a&gt;. This is a very simple method. Go back to your vs code and pull all the changes. and then in the README, you are going to add it right after the blog post. Therefore you are going to add the below image tags and Replace &lt;code&gt;sabesansathananthan&lt;/code&gt; with your GitHub username.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 HTML
&amp;lt;a href="https://github.com/sabesansathananthan"&amp;gt;
&amp;lt;img align="center" alt="sabesan's Github Stats" src="https://github-readme-stats.codestackr.vercel.app/api?username=sabesansathananthan&amp;amp;show_icons=true&amp;amp;hide_border=true&amp;amp;count_private=true&amp;amp;include_all_commits=true&amp;amp;theme=radical" /&amp;gt;&amp;lt;/a&amp;gt;

&amp;lt;a href="https://github.com/sabesansathananthan"&amp;gt;
  &amp;lt;img align="center" src="https://github-readme-stats.anuraghazra1.vercel.app/api/top-langs/?username=sabesansathananthan&amp;amp;layout=compact&amp;amp;theme=radical" /&amp;gt;
&amp;lt;/a&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now your profile is pretty cool and awesome. &lt;/p&gt;

</description>
      <category>github</category>
      <category>markdown</category>
      <category>devops</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
