Learning million
Google search

Learning million

Million.js is a tool that makes React applications run faster. I found out about it while looking for ways to improve my website's performance. I stumbled upon a post by Timonwa and decided to explore Million.js further.

According to the documentation. The <For/> component should be used to fetch a list of items from the API database for better performance. This is better than using Array.map(), which could slow down the application. So, Million.js and the <For/> component are recommended for a smoother and quicker React application.

Example?

Let’s retrieve the emails from users using the Typicode API, using either React Query or Tan Query. If you're interested in exploring other alternative methods, you can refer to the article:

How to Fetch API Data in React.

Using the <For/> Tag

<>

  <For each={emails}>

    {(email) => <div key={email}>{email.email}</div>}

  </For>

</>        

Complete code

import { useQuery } from '@tanstack/react-query';
import { For } from 'million';

const UseFor = () => {
 const { data: emails, isLoading, error } = useQuery({
    queryFn: () =>
      fetch('https://jsonplaceholder.typicode.com/comment_limit=10').then(
        (res) => res.json()
      ),
    queryKey: ['emails'],
  });

  if (isLoading) {
    return <h2 className="info">Loading...</h2>;
  }

  if (error) {
    return <h2 className="error">Error</h2>; // Fixed typo in error message
  }

  console.log(emails); // Log comments to the console

  return (
    <>
      <h1>Email of Users</h1>
      {/* Utilize Fragment syntax for cleaner JSX */}
      <For each={emails}>
        {(email) => <div key={email.id}>{email.email}</div>} 
      </For>
    </>
  );
};

export default UseFor;
        

Result

Result

In the gif above, the brief delay happened because I wanted to demonstrate the loading and error states I set up it has nothing to do with million.js. When there's no internet, the error state shows up. But as soon as the internet is back, the list of emails appears quickly, making the application performance 70% faster.?

If there's a better option, feel free to suggest improvements—I'm open to corrections and updates.

and also feel free to experiment with the provided examples and share your experience. Your feedback and questions are not only welcomed but encouraged.

If you like my work, I'd appreciate your support by Buying me a drink??.

?#workinprogress?

Resource

?Thanks for reading ??

要查看或添加评论,请登录

Ijeoma Igboagu的更多文章

  • Doc: Building An Event-Driven Architecture Notification App using AWS Services

    Doc: Building An Event-Driven Architecture Notification App using AWS Services

    Today's project focuses on creating a notification system for NBA games or any other sport. The goal is to keep users…

  • How to Authenticate Your React App Using Firebase

    How to Authenticate Your React App Using Firebase

    Authentication is a fundamental aspect of modern web and mobile applications. It ensures that users can securely access…

  • How to Build an Application With Node.js

    How to Build an Application With Node.js

    Node.js is a runtime environment that allows you to run JavaScript code on the server side for building server-side…

  • Blogging

    Blogging

    Starting a blog might seem like a good way to make money, but it's not easy – it takes a lot of hard work and…

  • GRATEFUL

    GRATEFUL

    I'm thankful to God for giving me the strength to make meaningful contributions to the freeCodeCamp community. Reasons…

    5 条评论
  • Starting as a new developer

    Starting as a new developer

    Starting as a new developer can be tough. When I started learning web development, it was more challenging than I…

  • The Art of Documentation

    The Art of Documentation

    Do you have experience with properly documenting and including clear commit messages for your code? Good documentation…

  • The female kangaroo

    The female kangaroo

    The ability of female kangaroos to raise three offspring simultaneously, all from the same place, fascinates me! Female…

  • Draft

    Draft

    I find writing inspiration in unexpected places and circumstances. It could come up in a casual discussion, and when it…

  • Cr youtube??

    Cr youtube??

    About the project This web application simplifies searching and watching YouTube videos. It was built using React and…

社区洞察

其他会员也浏览了