MagicJS
mern.aiUniversityGitHub
  • Introduction to MagicJS
  • Why MagicJS?
  • Getting Started & Installation
  • Basic Guide
    • Create a new page using React
    • Navigate between pages
    • Create an API and integrate it with the frontend
    • Authenticate Users
      • Authorise based on Roles
    • Advanced State Management with useContent()
    • Perform CRUD Operations
    • Adding Realtime capabilities using socket
    • Handling file uploads and downloads
  • Advanced Guide
    • Understanding the concept of features in MagicJS
    • Using UI components & functions across multiple Magic Features
    • Advanced Routing of pages
    • Enable SSR
    • Access MongoDB
    • Styling pages using Tailwind CSS
  • Deploying
  • Update MagicJS
  • API References
    • Frontend
      • <LinkDisplay>
      • createSrc()
      • createUploader()
      • importUI()
      • loadConfig()
      • protected()
      • useParams()
      • useAxios()
      • useLogin()
      • useSocket()
      • useContent()
      • usePromise()
      • useNotification()
    • Backend
      • createBackendFunction()
      • data()
      • io()
      • ServerInstance()
      • utils
        • hash()
        • verifyHash()
        • initiateEmailVerification()
        • saveFileToUserUploads()
        • readFileFromUserUploads()
        • removeFileFromUserUploads()
        • assignRoleToUser()
        • unassignRoleFromUser()
        • findAllRolesByUser()
        • isUserInAnyRoles()
        • assignRoleToUser()
Powered by GitBook
On this page

Was this helpful?

  1. API References
  2. Frontend

usePromise()

The usePromise() hook simplifies handling asynchronous operations by managing the state of a Promise-based function.

Functionality Overview:

The usePromise() hook simplifies asynchronous operations in functional components.

It manages three states: result, error, and loading with a refresh function.

Execute the async tasks with refresh() and handle the result, error, and loading states accordingly.

Here's an example:

import { usePromise } from '@magicjs.dev/frontend';
import fetchDataServer from './fetch-data.server';

const MyComponent = () => {
    const { refresh, result, error, loading } = usePromise(fetchData);

    // Execute fetchData when component mounts or whenever needed
    useEffect(() => {
        refresh();
    }, []);

    return (
        <div>
            {
                loading && <p>Loading...</p>
            }
            {
                error && <p>Error: {error.message}</p>
            }
            {
                result && <p>Data: {result}</p>
            }
        </div>
    );
}
PrevioususeContent()NextuseNotification()

Last updated 1 year ago

Was this helpful?

Click here to refer GitHub.