<?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: imad benmadi</title>
    <description>The latest articles on DEV Community by imad benmadi (@imadbenmadi).</description>
    <link>https://dev.to/imadbenmadi</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%2F1154910%2Fba5f8ab0-4008-4d2a-88d3-d62a3c541ac4.jpeg</url>
      <title>DEV Community: imad benmadi</title>
      <link>https://dev.to/imadbenmadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imadbenmadi"/>
    <language>en</language>
    <item>
      <title>Loading Images ReactJs</title>
      <dc:creator>imad benmadi</dc:creator>
      <pubDate>Sat, 23 Mar 2024 01:52:26 +0000</pubDate>
      <link>https://dev.to/imadbenmadi/loading-images-reactjs-4o0n</link>
      <guid>https://dev.to/imadbenmadi/loading-images-reactjs-4o0n</guid>
      <description>&lt;p&gt;Hello Dev community &lt;br&gt;
*&lt;em&gt;I want to share with you a simple Image Loading Using Simple userEffect and a promise&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Fist of All , we create a component with a basic user interface &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we import ower images from the Public folder &lt;br&gt;
I have an example in here&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import bernuilt from "../../../public/bernuilt.jpg";
import benomial from "../../../public/benomial.jpg";
import uniformDiscret from "../../../public/uniformDiscret.jpg";
import HyperGeomitrique from "../../../public/HyperGeomitrique.jpg";
import Geomitrique from "../../../public/Geomitrique.jpg";
import Poisson from "../../../public/Poisson.jpg";

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

&lt;/div&gt;


&lt;p&gt;and then this is the useEffect function that does the word for us&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [imagesLoaded, setImagesLoaded] = useState(false);
useEffect(() =&amp;gt; {
        const imageLoader = new Promise((resolve, reject) =&amp;gt; {
            const images = [bernuilt, benomial, uniformDiscret, HyperGeomitrique, Geomitrique, Poisson];
            let loadedCount = 0;

            images.forEach((imageSrc) =&amp;gt; {
                const img = new Image();
                img.onload = () =&amp;gt; {
                    loadedCount++;
                    if (loadedCount === images.length) {
                        resolve();
                    }
                };
                img.onerror = () =&amp;gt; {
                    reject();
                };
                img.src = imageSrc;
            });
        });

        imageLoader.then(() =&amp;gt; {
            setImagesLoaded(true);
        }).catch(() =&amp;gt; {
            console.error("Error loading images");
        });
    }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then Lastly to integrate between the Loading and the UI we use this simple Logic&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{!imagesLoaded ? (
         &amp;lt;div className="w-full h-full flex items-center justify-center"&amp;gt;
            &amp;lt;span className="loader"&amp;gt;&amp;lt;/span&amp;gt;
         &amp;lt;/div&amp;gt;
       ) : (
           // you code here
      )}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and for the &lt;strong&gt;.loader&lt;/strong&gt; CSS class We use this CSS animation Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.loader {
    width: 40px;
    height: 40px;
    border: 5px solid #353535;
    border-bottom-color: transparent;
    border-radius: 50%;
    display: inline-block;
    box-sizing: border-box;
    animation: rotation 1s linear infinite;
}

@keyframes rotation {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And That's it , We Done it !&lt;br&gt;
a simple React image Loader with out any external library &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0p24qlad5fbgz8cmu90y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0p24qlad5fbgz8cmu90y.png" alt="Image description" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fun6c6n0xuo0py961srj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fun6c6n0xuo0py961srj1.png" alt="Image description" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hi This is the first Post</title>
      <dc:creator>imad benmadi</dc:creator>
      <pubDate>Fri, 03 Nov 2023 15:37:21 +0000</pubDate>
      <link>https://dev.to/imadbenmadi/hi-this-is-the-first-post-4h3l</link>
      <guid>https://dev.to/imadbenmadi/hi-this-is-the-first-post-4h3l</guid>
      <description>&lt;p&gt;I'm a new programmer and i'm gald i Found this website , i think it is COOL&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
