--- title: useNotice description: "`useNotice`は、アプリケーションの通知を制御するカスタムフックです。" links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/hooks/use-notice - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/hooks-usenotice--basic --- ```tsx const notice = useNotice() return ( ) ``` ## 使い方 ```tsx import { useNotice } from "@yamada-ui/react" ``` ```tsx import { useNotice } from "@/components/ui" ``` ```tsx import { useNotice } from "@workspaces/ui" ``` ```tsx const notice = useNotice() ``` ### バリアントを変更する ```tsx const notice = useNotice() return ( {(variant) => ( )} ) ``` ### カラースキームを変更する ```tsx const notice = useNotice() return ( {(colorScheme) => ( )} ) ``` ### ローディングスキームを変更する ```tsx const notice = useNotice() return ( {(loadingScheme) => ( )} ) ``` ### ステータスを変更する ステータスを変更する場合は、`status`に`"info"`や`"success"`などを設定します。 ```tsx const notice = useNotice() return ( {(status) => ( )} ) ``` ### 表示件数を変更する 表示件数を変更する場合は、`limit`に数値を設定します。 ```tsx const notice = useNotice({ limit: 10 }) return ( ) ``` ### 表示時間を変更する 表示時間を変更する場合は、`duration`に数値を設定します。 ```tsx const notice = useNotice({ duration: 10000 }) return ( ) ``` ### 表示を維持する 表示を維持する場合は、`duration`に`null`を設定します。 ```tsx const notice = useNotice({ duration: null }) return ( ) ``` ### 表示位置を変更する 表示位置を変更する場合は、`placement`に`"start"`や`"end"`などを設定します。 ```tsx const notice = useNotice({ duration: null }) return ( {(placement) => ( )} ) ``` ### 通知を閉じる方法を変更する 通知を閉じる方法を変更する場合は、`closeStrategy`に`"click"`や`"drag"`などを設定します。 ```tsx const notice = useNotice() return ( {(closeStrategy) => ( )} {(closeStrategy) => ( )} ) ``` ### 通知を閉じる 通知を閉じる場合は、`close`や`closeAll`を使用します。 ```tsx const notice = useNotice() const id = useRef(null) const onOpen = () => { id.current = notice({ closable: true, description: "お前が好きだ。", duration: 30000, title: "クラン・クラン", }) } const onClose = () => { if (id.current) notice.close(id.current) } const onCloseAll = () => { notice.closeAll() } return ( ) ``` ### 通知を更新する 通知を更新する場合は、`update`メソッドを使用します。 ```tsx const notice = useNotice() const id = useRef(null) const onOpen = () => { id.current = notice({ colorScheme: "orange", description: "チャンスは目の前にあるものよ。", duration: 5000, title: "シェリル・ノーム", }) } const onUpdate = () => { if (id.current) notice.update(id.current, { colorScheme: "blue", description: "人生はワン・ツー・デカルチャー!!頑張れ、私。", duration: 5000, title: "ランカ・リー", }) } return ( ) ``` ## コンフィグ ### 通知を常に展開する 通知を常に展開する場合は、`expand`に`true`を設定します。 ```tsx import { UIProvider, extendConfig } from "@yamada-ui/react" const config = extendConfig({ notice: { expand: true, }, }) const App = () => { return ( ) } ``` ### 表示位置を変更する 表示位置を変更する場合は、`placement`に`"start"`や`"end"`などを設定します。 ```tsx import { UIProvider, extendConfig } from "@yamada-ui/react" const config = extendConfig({ notice: { placement: "end-end",, }, }) const App = () => { return ( ) } ``` ### 表示件数を変更する 表示件数を変更する場合は、`limit`に数値を設定します。 ```tsx import { UIProvider, extendConfig } from "@yamada-ui/react" const config = extendConfig({ notice: { limit: 5, }, }) const App = () => { return ( ) } ``` ### 通知を閉じる方法を変更する 通知を閉じる方法を変更する場合は、`closeStrategy`に`"click"`や`"drag"`などを設定します。 ```tsx import { UIProvider, extendConfig } from "@yamada-ui/react" const config = extendConfig({ notice: { closeStrategy: "click", }, }) const App = () => { return ( ) } ```