useLoading
useLoading
is a custom hook for controlling the loading of the application.
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 (
<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 use this code, you need to add
"use client"
to the top of the file.Usage
import { useLoading } from "@yamada-ui/react"
import { useLoading } from "@/components/ui"
import { useLoading } from "@workspaces/ui"
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
const { screen, page, background } = useLoading()
return (
<Wrap gap="md">
<Button
onClick={() => screen.start({ loadingScheme: "grid", duration: 3000 })}
>
Start Screen Loading
</Button>
<Button
onClick={() => page.start({ loadingScheme: "dots", duration: 3000 })}
>
Start Page Loading
</Button>
<Button
onClick={() =>
background.start({ loadingScheme: "puff", duration: 3000 })
}
>
Start Background Loading
</Button>
</Wrap>
)
If you use this code, you need to add
"use client"
to the top of the file.Set Duration
To set the duration, set a number (milliseconds) to duration
.
const { screen, page, background } = useLoading()
return (
<Wrap gap="md">
<Button onClick={() => screen.start({ duration: 3000 })}>
Start Screen Loading
</Button>
<Button onClick={() => page.start({ duration: 3000 })}>
Start Page Loading
</Button>
<Button onClick={() => background.start({ duration: 3000 })}>
Start Background Loading
</Button>
</Wrap>
)
If you use this code, you need to add
"use client"
to the top of the file.Set Message
To set a message, set a ReactNode
to message
.
const { screen, page, background } = useLoading()
return (
<Wrap gap="md">
<Button
onClick={() => screen.start({ message: "Loading", duration: 3000 })}
>
Start Screen Loading
</Button>
<Button onClick={() => page.start({ message: "Loading", duration: 3000 })}>
Start Page Loading
</Button>
<Button
onClick={() => background.start({ message: "Loading", duration: 3000 })}
>
Start Background Loading
</Button>
</Wrap>
)
If you use this code, you need to add
"use client"
to the top of the file.Update Message
To update a message, use update
.
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>
)
If you use this code, you need to add
"use client"
to the top of the file.Configuration
Change Loading Scheme
const config = extendConfig({
loading: {
background: { loadingScheme: "puff" },
page: { loadingScheme: "dots" },
screen: { loadingScheme: "grid" },
},
})
const App: FC = () => {
const { screen, page, background } = useLoading()
return (
<Wrap gap="md">
<Button onClick={() => screen.start({ duration: 3000 })}>
Start Screen Loading
</Button>
<Button onClick={() => page.start({ duration: 3000 })}>
Start Page Loading
</Button>
<Button onClick={() => background.start({ duration: 3000 })}>
Start Background Loading
</Button>
</Wrap>
)
}
return (
<UIProvider config={config}>
<App />
</UIProvider>
)
If you use this code, you need to add
"use client"
to the top of the file.