Skip to content
LOCZH/安大略 · 加拿大待机OK/--:--:--EST
M4M4RK_YUportfolio
  • 项目
    项目Overview
    • 作品精选案例与项目记录
    • 游戏可玩原型与游戏开发日志
  • 影像
    影像Overview
    • 档案影像合集与视觉实验
    • 商店印刷品、海报和限量物件
  • 日志
    日志Overview
    • 博客长篇开发日志与现场笔记
    • 笔记短观察、链接与代码片段
  • 资源
    资源Overview
    • 工具38 款浏览器内开发工具
    • 链接每日使用的开发与设计书签
  • 关于
  • 联系
EN

同步 · dev.to / @markyu

3 Ways To Create Engaging React Loading Screens with Hooks

Creating engaging loading screens for your React applications can greatly enhance the user...

发布日期
May 3 '24
·
阅读时长
3 min read
·
点赞
7
reacthookswebdevfrontendchallenge
在 dev.to 查看

Creating engaging loading screens for your React applications can greatly enhance the user experience, especially during data fetching or other intensive operations. This article explores three dynamic methods to achieve this using React Hooks. These methods vary in complexity and offer different levels of customization, ensuring you can cater to your specific needs.

Image description

Type 1: Using react-loading

The react-loading library is a great choice for a straightforward and effective loading screen. It offers a variety of loading indicators that can be easily customized. The implementation is simple and does not require extensive setup.

  1. Setup: Create a new React component file, PreLoader1.js.

  2. Functional Component: Within this file, create a functional component and use two states:

    const [data, setData] = useState([]);
    const [done, setDone] = useState(false);
    • Data: Holds the data fetched from the API.
    • Done: A boolean that determines whether to display the pre-loader.
  3. Fetch Data:

    • Use the useEffect hook to fetch data from an API:
    useEffect(() => {
       setTimeout(() => {
          fetch("https://jsonplaceholder.typicode.com/posts")
             .then(response => response.json())
             .then(json => {
                setData(json);
                setDone(true);
             });
       }, 2000);
    }, []);
    • The setTimeout function adds a delay for showcasing the loading screen.
  4. Render:

    • If done is false, render the loading indicator. Otherwise, display the fetched data:
    return (
       <>
          {!done ? (
             <ReactLoading type="spin" color="#000" height={50} width={50} />
          ) : (
             <ul>
                {data.map(item => (
                   <li key={item.id}>{item.title}</li>
                ))}
             </ul>
          )}
       </>
    );

Type 2: Using react-lottie

For more sophisticated animations, the react-lottie library is an excellent choice. This method allows you to display engaging animations that align with your app's theme.

  1. Setup: Start by creating a file, PreLoader2.js, and importing react-lottie:

    import Lottie from "react-lottie";
  2. Download Animations: Obtain Lottie animations from lottiefiles.com and import them into your project:

    import * as location from "../1055-world-locations.json";
    import * as success from "../1127-success.json";
  3. Set Options: Configure the animation settings:

    const defaultOptions1 = {
       loop: true,
       autoplay: true,
       animationData: location.default,
       rendererSettings: {
          preserveAspectRatio: "xMidYMid slice",
       },
    };
    
    const defaultOptions2 = {
       loop: true,
       autoplay: true,
       animationData: success.default,
       rendererSettings: {
          preserveAspectRatio: "xMidYMid slice",
       },
    };
  4. Use States:

    • Use three states to manage the loading animations:
    const [data, setData] = useState([]);
    const [loading, setLoading] = useState(false);
    const [completed, setCompleted] = useState(false);
  5. Fetch Data:

    • Similar to Type 1, but with two loading indicators:
    useEffect(() => {
       setTimeout(() => {
          fetch("https://jsonplaceholder.typicode.com/posts")
             .then(response => response.json())
             .then(json => {
                setData(json);
                setLoading(true);
                setTimeout(() => {
                   setCompleted(true);
                }, 1000);
             });
       }, 2000);
    }, []);
  6. Render:

    return (
       <>
          {!completed ? (
             <>
                {!loading ? (
                   <Lottie options={defaultOptions1} height={200} width={200} />
                ) : (
                   <Lottie options={defaultOptions2} height={100} width={100} />
                )}
             </>
          ) : (
             <h1>Your Data</h1>
          )}
       </>
    );

Type 3: Using Simple CSS

For a minimalist approach, you can create a loading screen using simple CSS styling. This method gives you full control over the design but requires a bit more effort.

  1. Setup: Create a new file, PreLoader3.js, and copy the setup from PreLoader2.js.

  2. Remove Lottie: Delete all code related to react-lottie and update the return statement:

    return (
       <>
          {!completed ? (
             <>
                {!loading ? (
                   <div className="spinner">
                      <span>Loading...</span>
                      <div className="half-spinner"></div>
                   </div>
                ) : (
                   <div className="completed">&#x2713;</div>
                )}
             </>
          ) : (
             <h1>Your Data</h1>
          )}
       </>
    );
  3. Styling: Create a CSS file named preloader3.css to style the spinner and success symbol.

Conclusion

These three approaches offer different levels of customization and complexity for creating engaging loading screens in React applications. Whether you prefer using libraries or crafting custom CSS animations, these methods provide flexible solutions to enhance user experience during data fetching or processing tasks.

相关阅读

webdev

Quick Guide To 3D Transformations in CSS3 😎

Introduction Creating dynamic 3D scenes in web development can elevate your design, making...

webdev

Implementing 3D Graphics in React

3D is an exciting area in computer science, and it could range from creating 3D shapes, vectors,...

database

The True Cost of Poor Data Quality: Why It Matters and How to Improve It

In today’s fast-paced, data-driven world, businesses have more access to data than ever before....

原文发布

本文首发于 dev.to,评论与点赞保留在原站。

在 dev.to 继续阅读
上一篇Demystifying Heuristic Search AlgorithmsIntroduction: In the vast landscape of artificial intelligence, heuristic search algorithms are a...
返回档案
下一篇Introduction to Docker Containers 🐋📦 [With Commands]Introduction Containers have revolutionized software development and deployment by...
返回档案
频道开放·随时打个招呼 · 2026
--:--:--EST
联系

看到什么有意思的?和我聊聊。

这是一个作品集,不是服务 · 但每一条留言我都会看 — 如果哪里让你有所触动,或者只想打个招呼,欢迎写信过来。

开启对话

订阅

偶尔收到一封简讯

来自 m4rkyu.com 的笔记与日志——简短、标注日期、没有杂音。随时可退订。

作品

线上发布、游戏作品与视觉档案。

  • 项目
  • 游戏
  • 档案
  • 日志

资源

每日好用的工具与个人收藏的链接库。

  • 搜索
  • 最新
  • 工具
  • 链接
  • 笔记
  • 主题
  • RSS
  • JSON Feed
  • 商店

工作室

背景、联系方式以及合作渠道。

  • 关于
  • 联系
  • 更新日志
  • 技术说明
  • 简历筹备中

社交

在常去的平台上找到我。

  • Facebook敬请期待
  • Instagram敬请期待
  • YouTube敬请期待
  • 领英敬请期待
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
© 2026 ZhenXiao Mark Yu·加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英 (敬请期待)
  • 推特 / X (敬请期待)
  • Instagram (敬请期待)
由 Next.js 16 · React 19 · Tailwind 4 构建

由 Next.js 16 · React 19 · Tailwind 4 构建