๐ ๐ซ๐จ๐ฆ ๐๐ฆ๐ฉ๐๐ซ๐๐ญ๐ข๐ฏ๐ ๐๐๐ง๐๐ฅ๐๐ฌ ๐ญ๐จ ๐๐๐๐ฅ๐๐ซ๐๐ญ๐ข๐ฏ๐ ๐๐ซ๐จ๐ฉ๐ฌ ๐๐จ๐ซ ๐๐ฅ๐๐๐ง๐๐ซ ๐๐จ๐๐
๐ฌ๐บ๐๐บ๐๐๐๐ ๐๐๐ฝ๐บ๐
๐๐๐๐๐ป๐๐
๐๐๐ ๐๐ ๐ฑ๐พ๐บ๐ผ๐ ๐ผ๐๐๐๐๐๐พ๐๐๐ ๐๐ ๐บ ๐ผ๐๐๐๐๐ ๐๐บ๐๐.
๐ถ๐๐๐
๐พ ๐๐๐พ๐จ๐๐๐พ๐๐บ๐๐๐๐พ๐ง๐บ๐๐ฝ๐
๐พ ๐ผ๐๐๐ป๐๐๐พ๐ฝ ๐๐๐๐ ๐ฟ๐๐๐๐บ๐๐ฝ๐ฑ๐พ๐ฟ ๐๐ฟ๐ฟ๐พ๐๐ ๐บ ๐๐๐๐พ๐๐ฟ๐๐
๐๐บ๐ ๐๐ ๐พ๐๐๐๐๐พ ๐๐พ๐๐๐๐ฝ๐ ๐ฟ๐๐๐ ๐บ ๐ผ๐๐๐
๐ฝ ๐ผ๐๐๐๐๐๐พ๐๐ ๐๐ ๐๐๐ ๐๐บ๐๐พ๐๐, ๐๐ ๐๐ฟ๐๐พ๐ ๐๐๐๐๐๐ฝ๐๐ผ๐พ๐ ๐บ๐ ๐๐๐๐พ๐๐บ๐๐๐๐พ ๐ผ๐๐๐๐๐๐
๐ฟ๐
๐๐ ๐๐๐บ๐ ๐ผ๐บ๐ ๐ป๐พ ๐
๐พ๐๐ ๐๐๐๐๐๐๐๐๐พ ๐๐๐บ๐ ๐ฑ๐พ๐บ๐ผ๐'๐ ๐ฝ๐พ๐ผ๐
๐บ๐๐บ๐๐๐๐พ ๐๐บ๐๐บ๐ฝ๐๐๐.
๐๐ฆ๐ฉ๐๐ซ๐๐ญ๐ข๐ฏ๐ ๐๐จ๐ง๐ญ๐ซ๐จ๐ฅ ๐ฐ๐ข๐ญ๐ก ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ธฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ทฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ
The initial pattern uses ฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ to pass a ref down to the ResultModal component. Inside the component, ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ธฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ทฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ then allows us to define specific methods (like ฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ() and ฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ()) that can be called directly from the parent via that ฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ.
// Parent component would call:
// modalRef.current.open();
// modalRef.current.close();
๐๐๐๐ฅ๐๐ซ๐๐ญ๐ข๐ฏ๐ ๐๐จ๐ง๐ญ๐ซ๐จ๐ฅ ๐ฐ๐ข๐ญ๐ก ๐๐ซ๐จ๐ฉ๐ฌ
๐ฅ๐๐ ๐๐๐๐๐
๐พ ๐๐๐๐/๐๐๐ฝ๐พ ๐ฟ๐๐๐ผ๐๐๐๐๐บ๐
๐๐๐, ๐บ ๐๐๐๐พ '๐ฑ๐พ๐บ๐ผ๐-๐' ๐๐บ๐ ๐๐ ๐๐ ๐ผ๐๐๐๐๐๐
๐๐๐พ ๐๐๐ฝ๐บ๐
'๐ ๐๐๐๐๐ป๐๐
๐๐๐ ๐๐๐๐๐๐๐ ๐๐๐๐๐. ๐ถ๐พ ๐๐๐๐๐๐ฝ๐๐ผ๐พ ๐บ๐ ๐ฬฒ๐๐ฎ๐๐พ๐ ๐๐๐๐ ๐๐ ๐ฝ๐๐ผ๐๐บ๐๐พ ๐๐๐พ๐๐๐พ๐ ๐๐๐พ ๐๐๐ฝ๐บ๐
๐๐๐๐๐
๐ฝ ๐ป๐พ ๐๐๐พ๐ ๐๐ ๐ผ๐
๐๐๐พ๐ฝ, ๐บ๐๐ฝ ๐บ๐ ๐๐๐ข๐
๐๐๐พ ๐๐๐๐ ๐๐ ๐๐บ๐๐ฝ๐
๐พ ๐๐๐พ ๐ผ๐
๐๐๐๐๐ ๐พ๐๐พ๐๐ ๐ฟ๐๐๐ ๐๐๐๐๐๐ ๐๐๐พ ๐๐๐ฝ๐บ๐
.
import { useRef, useEffect } from "react";
export default function ResultModal({ result, targetTime, isOpen, onClose }) {
const dialogRef = useRef();
// Crucially, we use useEffect to synchronize the modal's state
// with the `isOpen` prop.
useEffect(() => {
if (isOpen) {
// The HTML <dialog> element's showModal() method opens the modal
// in a top-layer, blocking state.
dialogRef.current?.showModal();
} else {
// The close() method dismisses the modal.
dialogRef.current?.close();
}
}, [isOpen]); // Dependency array: This effect runs ONLY when `isOpen` changes.
return (
<dialog ref={dialogRef} className="result-modal" onClose={onClose}>
<h2>You {result}</h2>
<p>The target time was <strong>{targetTime} seconds.</strong></p>
<p>You stopped the timer with <strong>X seconds left.</strong></p>
<form method="dialog">
<button onClick={onClose}>Close</button>
</form>
</dialog>
);
}
๐๐ซ๐จ๐ฉ-๐๐๐ฌ๐๐ ๐๐ฉ๐ฉ๐ซ๐จ๐๐๐ก:
- ๐๐ฆ๐ค๐ญ๐ข๐ณ๐ข๐ต๐ช๐ท๐ฆ ๐๐ฐ๐ฏ๐ต๐ณ๐ฐ๐ญ: ๐ณ๐๐พ ๐๐บ๐๐พ๐๐ ๐ผ๐๐๐๐๐๐พ๐๐ ๐ฝ๐พ๐ผ๐ ๐บ๐๐พ๐ ๐๐๐พ ๐ฝ๐พ๐๐๐๐พ๐ฝ ๐๐๐บ๐๐พ (๐๐๐ฎ๐๐พ๐: ๐๐๐๐พ ๐๐ ๐๐๐ฎ๐๐พ๐: ๐ฟ๐บ๐ ๐๐พ
- ๐๐น๐ฑ๐ญ๐ช๐ค๐ช๐ต ๐๐ข๐ต๐ข ๐๐ญ๐ฐ๐ธ: ๐ณ๐๐พ ๐ฟ๐ ๐๐ ๐๐ฟ ๐ฝ๐บ๐๐บ ๐๐ ๐ผ๐ ๐พ๐บ๐: ๐ฬฒฬฒ๐ฬฒฬฒ๐พฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ ๐บ๐๐ฝ ๐ฬฒฬฒ๐ฬฒฬฒ๐ฒฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ ๐บ๐๐พ ๐พ๐๐๐ ๐๐ผ๐๐๐ ๐ ๐๐บ๐๐๐พ๐ฝ ๐ฝ๐๐๐ ๐บ๐ ๐๐๐๐๐.
- ๐๐บ๐ฏ๐ค๐ฉ๐ณ๐ฐ๐ฏ๐ช๐ป๐ข๐ต๐ช๐ฐ๐ฏ ๐ธ๐ช๐ต๐ฉ ๐ถ๐ด๐ฆ๐๐ง๐ง๐ฆ๐ค๐ต: ๐ณ๐๐พ ๐๐๐พ๐ค๐ฟ๐ฟ๐พ๐ผ๐ ๐๐๐๐ ๐๐ ๐๐๐๐บ๐ ๐๐พ๐๐พ. ๐จ๐ ๐พ๐๐๐๐๐พ๐ ๐๐๐บ๐ ๐๐๐พ ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ ฬฒฬฒ๐ผฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ() ๐๐ ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ() ๐๐พ๐๐๐๐ฝ๐ ๐๐ฟ ๐๐๐พ ๐๐บ๐๐๐๐พ ๐ง๐ณ๐ฌ๐ซ <ฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ>
- ๐๐ฆ๐ท๐ฆ๐ณ๐ข๐จ๐ช๐ฏ๐จ ๐๐ข๐ต๐ช๐ท๐ฆ <๐ฅ๐ช๐ข๐ญ๐ฐ๐จ>: This approach effectively uses the built-in functionality of the HTML <ฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ> element.
๐ถ๐๐๐ ๐พ ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ธฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ทฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒฬฒ๐ฬฒ ๐๐บ๐ ๐๐๐ ๐ ๐พ๐๐๐๐๐๐บ๐๐พ ๐๐๐พ ๐ผ๐บ๐๐พ๐ (๐พ.๐., ๐พ๐๐๐๐๐๐๐ ๐บ๐ ๐ ๐ฏ๐จ ๐ฟ๐๐ ๐ฟ๐๐ผ๐๐๐๐๐ ๐พ๐ ๐พ๐๐พ๐๐๐, ๐๐๐๐๐๐พ๐๐๐๐ ๐บ๐๐๐๐บ๐๐๐๐๐), ๐ฟ๐๐ ๐๐๐๐๐ ๐พ ๐๐๐บ๐๐พ-๐ฝ๐๐๐๐พ๐ ๐๐๐๐พ๐๐บ๐ผ๐๐๐๐๐ ๐ ๐๐๐พ ๐๐๐๐๐๐๐/๐๐๐ฝ๐๐๐ ๐บ ๐๐๐ฝ๐บ๐ , ๐บ ๐๐๐๐-๐ป๐บ๐๐พ๐ฝ ๐บ๐๐๐๐๐บ๐ผ๐ ๐๐ฟ๐๐พ๐ ๐ ๐พ๐บ๐ฝ๐ ๐๐ ๐๐๐๐พ ๐๐พ๐บ๐ฝ๐บ๐ป๐ ๐พ, ๐๐บ๐๐๐๐บ๐๐๐บ๐ป๐ ๐พ, ๐บ๐๐ฝ ๐๐๐พ๐ฝ๐๐ผ๐๐บ๐ป๐ ๐พ ๐ฑ๐พ๐บ๐ผ๐ ๐ผ๐๐๐๐๐๐พ๐๐๐.
๐ถ๐๐บ๐ ๐บ๐๐พ ๐๐๐๐ ๐๐๐๐๐๐๐๐ ๐๐ ๐๐๐๐พ๐๐บ๐๐๐๐พ ๐๐. ๐ฝ๐พ๐ผ๐ ๐บ๐๐บ๐๐๐๐พ ๐๐บ๐๐๐พ๐๐๐ ๐๐ ๐ฑ๐พ๐บ๐ผ๐? ๐ฒ๐๐บ๐๐พ ๐๐๐๐ ๐๐๐๐๐๐๐๐!
Top comments (0)