useLoading
useLoading
is a custom hook for controlling the loading of the application.
Import
import { useLoading } from "@yamada-ui/react"
Usage
useLoading
returns instances of screen
, page
, background
, and custom
. The instances include several methods.
isLoading
: Determines whether the loading animation is in progress.start
: Starts the loading animation.update
: Updates the information of the loading animation.finish
: Ends the loading animation.
custom
is not set in the Default Config. If you want to set up custom loading, please check here.
Editable example
const { screen, page, background } = useLoading() const onLoadingScreen = async () => { try { screen.start() await wait(5000) } finally { screen.finish() } } const onLoadingPage = async () => { try { page.start() await wait(5000) } finally { page.finish() } } const onLoadingBackground = async () => { try { background.start() await wait(5000) } finally { background.finish() } } return ( <Wrap gap="md"> <Button onClick={onLoadingScreen}>Start screen loading</Button> <Button onClick={onLoadingPage}>Start page loading</Button> <Button onClick={onLoadingBackground}>Start background loading</Button> </Wrap> )
If you want to learn more about loading, please check here.
Adding a Timeout
In cases such as communication, it is necessary to add a timeout. In that case, use duration
.
Editable example
const { screen, page, background } = useLoading() return ( <Wrap gap="md"> <Button onClick={() => screen.start({ duration: 5000 })}> Start screen loading </Button> <Button onClick={() => page.start({ duration: 5000 })}> Start page loading </Button> <Button onClick={() => background.start({ duration: 5000 })}> Start background loading </Button> </Wrap> )
Adding a Message
To add a message to the loading, use message
.
Editable example
const { screen, page, background } = useLoading() return ( <Wrap gap="md"> <Button onClick={() => screen.start({ message: "Loading", duration: 5000 })} > Start screen loading </Button> <Button onClick={() => page.start({ message: "Loading", duration: 5000 })}> Start page loading </Button> <Button onClick={() => background.start({ message: "Loading", duration: 5000 })} > Start background loading </Button> </Wrap> )
You can pass a JSX element
to message
.
Editable example
const { screen, page, background } = useLoading() return ( <Wrap gap="md"> <Button onClick={() => screen.start({ message: <Text color="primary">Loading</Text>, duration: 5000, }) } > Start screen loading </Button> <Button onClick={() => page.start({ message: <Text color="primary">Loading</Text>, duration: 5000, }) } > Start page loading </Button> <Button onClick={() => background.start({ message: <Text color="primary">Loading</Text>, duration: 5000, }) } > Start background loading </Button> </Wrap> )
Update Message
To update a message, pass options to the update
method.
Editable example
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 ( <Wrap gap="md"> <Button onClick={onLoadingScreen}>Start screen loading</Button> <Button onClick={onLoadingPage}>Start page loading</Button> <Button onClick={onLoadingBackground}>Start background loading</Button> </Wrap> )
Edit this page on GitHub