DEV Community

Cover image for Simple Homemade Loading Screen in React
angelina-white
angelina-white

Posted on

1 1

Simple Homemade Loading Screen in React

Reference tutorial

In my last blog, I mentioned this tutorial by Tony Heimark that is super easy to follow. I'll be going off of that, but making the animation instead. He uses the package, react-spinners. Instead of that, I'll be using the flower icon I found off of flaticon.com.

Finding an image

I went into google and typed in "flower icon". I went to flaticon.com. I just right clicked the image for the image address. This gives an image with no background.

App.js

In App.js, I made a useEffect with a setTimeout inside. This will be how long the screen is loading for. This will load for 5 seconds.

  const [loading, setLoading] = useState(false);
  useEffect(() =>
  {
    setLoading(true)
    setTimeout(() =>
    {
      setLoading(false)
    }, 5000)
  }, [])
Enter fullscreen mode Exit fullscreen mode

Under return, I made a ternary statement with the loading state. If loading is true, show the loading screen. If loading is false, show the homepage.

  return (
    <div class="App">
      {loading ? 
        <Flower />
      :
      <div class="homePage">
Enter fullscreen mode Exit fullscreen mode

I made a separate component for the loading screen called, "Flower".

Flower.js

In Flower.js, I made a useEffect with the animation inside it.

  useEffect(() =>
  {
     gsap.to("#flower", 1.75, 
     {
        y: 75,
        yoyo: true,
        repeat: -1, 
        ease: "power1",
     });
 }, [])
Enter fullscreen mode Exit fullscreen mode

This animation makes the flower float up and down. 1.75 is the amount of time for the animation. The "y" makes it move up 75 pixels. The yoyo being true means the flower will yoyo up and down 75 pixels. The repeat being -1 means it will repeat forever. The ease is the kind of style it is moving.

Under return, I added the image and a tag that says it's loading.

  return (
    <div class="loadingScreen">
        <img className="bus" src="https://t4.ftcdn.net/jpg/01/76/26/25/360_F_176262588_5UBYLDB9mqLxdqavixo0d4XF4g6ydFpZ.jpg" />
        <h1 id="loadingText">Loading...</h1>
    </div>
  )
Enter fullscreen mode Exit fullscreen mode

I was playing around with it, and another animation idea would be for the flower to spin.

gsap.to("#flower", 2, 
{
    rotation: 360,
    repeat: -1, 
    ease: "slow",
 });
Enter fullscreen mode Exit fullscreen mode

This just rotates the flower 360 degrees forever with a "slow" style.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay