<?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>DEV Community: Firmansyah Yanuar</title>
    <description>The latest articles on DEV Community by Firmansyah Yanuar (@fyfirman).</description>
    <link>https://dev.to/fyfirman</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%2F549166%2F07413d2b-563f-4879-a5b8-e3699dd3a83a.jpeg</url>
      <title>DEV Community: Firmansyah Yanuar</title>
      <link>https://dev.to/fyfirman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fyfirman"/>
    <language>en</language>
    <item>
      <title>How to Fix FOUT (Flash of Unstyled Text) in React</title>
      <dc:creator>Firmansyah Yanuar</dc:creator>
      <pubDate>Fri, 01 Jan 2021 15:53:09 +0000</pubDate>
      <link>https://dev.to/fyfirman/how-to-fix-fout-flash-of-unstyled-text-in-react-1dl1</link>
      <guid>https://dev.to/fyfirman/how-to-fix-fout-flash-of-unstyled-text-in-react-1dl1</guid>
      <description>&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%2Fti1pf7vs90e8jw14ypjz.gif" 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%2Fti1pf7vs90e8jw14ypjz.gif" alt="FOUT"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Can you see font is changing from &lt;code&gt;default&lt;/code&gt; to expected font? That's called &lt;strong&gt;FOUT (Flash of Unstyled Text)&lt;/strong&gt;. This happend cause your browser is rendered element before your font has loaded. It makes your website look cheap and you look like a beginner. 😟&lt;/p&gt;

&lt;p&gt;Be grateful because in 2021 there is already an API to solve this problem. The name is &lt;strong&gt;FontFaceSet API&lt;/strong&gt;, you can check the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/" rel="noopener noreferrer"&gt;documentation here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;#0 FYI&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I am using &lt;code&gt;fontsource&lt;/code&gt; to import my external font. Just because, I am using the &lt;code&gt;gatsby&lt;/code&gt; framework. It doesn't matter what you use, because this method works on any framework. Including the non-react framework.&lt;br&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;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@fontsource/merriweather&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;#1 Create &lt;code&gt;state&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isReady&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsReady&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we create a &lt;code&gt;state&lt;/code&gt; to store the state whether the website is ready to render or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isReady&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Your component goes here */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to add a condition if the state shows it's not ready, then don't render the element.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;#2 Check &amp;amp; Load fonts&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To check the font we use &lt;code&gt;FontFaceSet&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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="nx"&gt;fonts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12px Merriweather&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setIsReady&lt;/span&gt;&lt;span class="p"&gt;(&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="p"&gt;[])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Merriweather&lt;/code&gt; is fontface name, you can change that. &lt;code&gt;12px&lt;/code&gt; is dummy &lt;code&gt;fontSize&lt;/code&gt; to check. The following line of code is a Promise so that we can add commands when the font has been successfully loaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;#3 And wallaaaaa...&lt;/strong&gt;
&lt;/h3&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%2Fs1kqc1n4lucw35slsj6s.gif" 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%2Fs1kqc1n4lucw35slsj6s.gif" alt="Result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for your time to read this post. This is my first post on &lt;code&gt;dev.to&lt;/code&gt;, i hope i can keep on writing on this platform 😆 . If you don't mind please give a reaction to support me. 💗 &lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
