--- title: useLoading description: "`useLoading` is a custom hook for controlling the loading of the application." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/hooks/use-loading - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/hooks-useloading--basic --- ```tsx const { screen, page, background } = useLoading() const onLoadingScreen = async () => { try { screen.start() await wait(3000) } finally { screen.finish() } } const onLoadingPage = async () => { try { page.start() await wait(3000) } finally { page.finish() } } const onLoadingBackground = async () => { try { background.start() await wait(3000) } finally { background.finish() } } return ( ) ``` ## Usage ```tsx import { useLoading } from "@yamada-ui/react" ``` ```tsx import { useLoading } from "@/components/ui" ``` ```tsx import { useLoading } from "@workspaces/ui" ``` ```tsx const { screen, page, background } = useLoading() ``` `useLoading` returns instances of `screen`, `page`, and `background`. Each instance provides several methods: - `start`: Starts the loading animation. - `update`: Updates the loading animation. - `finish`: Finishes the loading animation. - `force`: Forces the loading animation to update. ### Change Loading Scheme ```tsx const { screen, page, background } = useLoading() return ( ) ``` ### Set Duration To set the duration, set a number (milliseconds) to `duration`. ```tsx const { screen, page, background } = useLoading() return ( ) ``` ### Set Message To set a message, set a `ReactNode` to `message`. ```tsx const { screen, page, background } = useLoading() return ( ) ``` ### Update Message To update a message, use `update`. ```tsx const { screen, page, background } = useLoading() const onLoadingScreen = async () => { try { screen.start({ message: "Loading" }) await wait(3000) screen.update({ message: "Please Wait" }) await wait(3000) } finally { screen.finish() } } const onLoadingPage = async () => { try { page.start({ message: "Loading" }) await wait(3000) page.update({ message: "Please Wait" }) await wait(3000) } finally { page.finish() } } const onLoadingBackground = async () => { try { background.start({ message: "Loading" }) await wait(3000) background.update({ message: "Please Wait" }) await wait(3000) } finally { background.finish() } } return ( ) ``` ## Configuration ### Change Loading Scheme ```tsx const config = extendConfig({ loading: { background: { loadingScheme: "puff" }, page: { loadingScheme: "dots" }, screen: { loadingScheme: "grid" }, }, }) const App: FC = () => { const { screen, page, background } = useLoading() return ( ) } return ( ) ```