3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Chakra UI】1つのファイル内で複数のモーダルを開く場合のuseDisclosureの定義方法について

Last updated at Posted at 2024-08-06

はじめに

Chakura UIにて、1つのファイル内で複数のモーダルを開く場合の実装方法で若干迷ったので展開します。

問題

モーダルを複数開く場合、useDisclosureのhooksからisOpen, onOpen, onCloseプロパティをそれぞれ取得する必要があるが、1つのファイル内で複数のモーダルを開く場合のやり方

useDisclosure(例)
// 編集画面用
const { isOpen, onOpen, onClose } = useDisclosure()
// 削除画面用
const { isOpen, onOpen, onClose } = useDisclosure()

↑のように、複数のuseDisclosureを宣言すると定義が重複する。

解決方法

一旦別名をつける。

useDisclosure(例)
// 編集画面用
const editModal = useDisclosure();
// 削除画面用
const deleteModal = useDisclosure();
使い方:(例)
// 編集画面
<EditModal
 isOpen={editModal.isOpen}
 onClose={editModal.onClose}/>
    
// 削除画面
<DeleteModal
 isOpen={deleteModal.isOpen}
 onClose={deleteModal.onClose}/>

おわりに

簡単に解決するけど割と思いつかないものですね...

参考

JISOUのメンバー募集中🔥

プログラミングコーチングJISOUではメンバーを募集しています。
日本一のアウトプットコミュニティでキャリアアップしませんか?
気になる方はぜひHPからライン登録お願いします!👇

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?