티스토리 뷰
반응형
React에서 useRef를 사용하여 구현해보았다. UI style만 수정되면 실무에서도 적용할 수 있을것 같다.
예제
import { useRef } from 'react'
const dialog = (): JSX.Element => {
const dialogRef = useRef<HTMLDialogElement>(null)
const openDialog = () => {
if (dialogRef.current === null) return
dialogRef.current.showModal()
}
const closeDialog = () => {
if (dialogRef.current === null) return
dialogRef.current.close()
}
return (
<div>
<div>
<button onClick={openDialog}>open</button>
</div>
<dialog ref={dialogRef}>
<div>Modal Box</div>
<button onClick={closeDialog}>close</button>
</dialog>
</div>
)
}
export default dialog
반응형
'React와 NextJS' 카테고리의 다른 글
React-Transition-Group vs. React-Spring (0) | 2024.11.04 |
---|---|
ContextAPI와 Recoil의 차이 (0) | 2024.11.03 |
getInitialProps란? | getInitialProps 사용이 권장되지 않는 이유 (0) | 2024.10.30 |
iOS에서 input의 focus zoom 막는 방법 (1) | 2024.10.28 |
redirect 와 rewrite (0) | 2024.10.26 |
- Total
- Today
- Yesterday
반응형