{"id":2095,"date":"2023-02-28T09:00:00","date_gmt":"2023-02-28T00:00:00","guid":{"rendered":"https:\/\/www.miracleave.co.jp\/contents\/?p=2095"},"modified":"2023-05-12T16:21:15","modified_gmt":"2023-05-12T07:21:15","slug":"post-2095","status":"publish","type":"post","link":"https:\/\/www.miracleave.co.jp\/contents\/2095\/post-2095\/","title":{"rendered":"[ React ref ]\u30c0\u30a4\u30a2\u30ed\u30b0\u3092\u958b\u3044\u305f\u6642\u306b\u81ea\u52d5\u30b9\u30af\u30ed\u30fc\u30eb\u3055\u305b\u305f\u3044"},"content":{"rendered":"\n<p>\u3053\u3093\u306b\u3061\u306f\uff01<br>\u73fe\u5728React\u3067\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306e\u958b\u767a\u3092\u62c5\u5f53\u3057\u3066\u3044\u308b\u571f\u4e95\u3067\u3059?\uff01<br>\u4eca\u56de\u306fDOM\u53c2\u7167\u306b\u4f7f\u3046ref\u5c5e\u6027\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u3001\u5b9f\u969b\u306e\u696d\u52d9\u3067\u8a70\u307e\u3063\u305f\u4e8b\u3068\u89e3\u6c7a\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><span class=\"fz-16px\"><span class=\"fz-28px\">\u5b9f\u73fe\u3057\u305f\u3044\u3053\u3068<\/span><\/span><\/strong><\/h2>\n\n\n\n<p>\u30da\u30fc\u30b8\u304b\u3089\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u3066\u30c0\u30a4\u30a2\u30ed\u30b0\u3092\u958b\u304f\u969b\u306b\u3001\u4e0b\u306e\u753b\u9762\u30a4\u30e1\u30fc\u30b8\u306e\u3088\u3046\u306b\u3001\u3042\u308b\u4e00\u5b9a\u306e\u4f4d\u7f6e\u307e\u3067\u81ea\u52d5\u30b9\u30af\u30ed\u30fc\u30eb\u3057\u3066\u8868\u793a\u3057\u305f\u3044\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"982\" height=\"1024\" src=\"https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2023\/02\/ref_callback-982x1024.png\" alt=\"\" class=\"wp-image-2143\" srcset=\"https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2023\/02\/ref_callback-982x1024.png 982w, https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2023\/02\/ref_callback-288x300.png 288w, https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2023\/02\/ref_callback-768x801.png 768w, https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2023\/02\/ref_callback.png 1322w\" sizes=\"(max-width: 982px) 100vw, 982px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong><span class=\"fz-16px\"><span class=\"fz-28px\">\u30cf\u30de\u3063\u305f\u3053\u3068<\/span><\/span><\/strong><\/strong><\/h2>\n\n\n\n<p>\u30b9\u30af\u30ed\u30fc\u30eb\u51e6\u7406\u306f\u3001<code>Element.ScrollTo()<\/code> \u3067\u5bfe\u5fdc\u3067\u304d\u308b\u304c\u3001\u809d\u5fc3\u306eElement\u306e\u53c2\u7167\u306f\u3069\u3046\u3059\u308b\u304b\uff1f<br>\u5f53\u521d\u306fuseRef\u3092\u4f7f\u3063\u3066\u53c2\u7167\u3057\u3088\u3046\u3068\u3057\u305f\u3002\uff08\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u2193\uff09<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>\/\/\u30c0\u30a4\u30a2\u30ed\u30b0\u306eref\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nconst dialogRef = useRef(null)\n\n\/\/ \u30c0\u30a4\u30a2\u30ed\u30b0\u3092\u958b\u3051\u305f\u3089\u3001\u30b9\u30af\u30ed\u30fc\u30eb\u3059\u308buseEffect\nReact.useEffect(() =&gt; {\n  if (open) {\n    if (dialogRef.current !== null) {\n      dialogRef.current.scrollTo(0, 2000);\u3000\/\/ \u30b9\u30af\u30ed\u30fc\u30eb\u51e6\u7406\n    }\n  }\n}, [open]);\u3000\/\/ \u4f9d\u5b58\u914d\u5217\u306b\u306f\u30c0\u30a4\u30a2\u30ed\u30b0\u306e\u958b\u9589state\n\n~~~~~~~~~~~~~~\u7701\u7565~~~~~~~~~~~~~~~~\n      &lt;Dialog\n        open={open}\u3000\u3000\u3000\/\/  \u30c0\u30a4\u30a2\u30ed\u30b0\u306e\u958b\u9589state\n        onClose={handleClose}\n        scroll=&quot;paper&quot;\n        aria-labelledby=&quot;scroll-dialog-title&quot;\n        aria-describedby=&quot;scroll-dialog-description&quot;\n      &gt;\n        &lt;DialogTitle id=&quot;scroll-dialog-title&quot;&gt;Subscribe&lt;\/DialogTitle&gt;\n        &lt;DialogContent\n          dividers\n\u3000\u3000\u3000\u3000\u3000\u3000    ref={dialogRef}    \/\/  ref\u5c5e\u6027\u306bref\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30bb\u30c3\u30c8\n        &gt;<\/code><\/pre><\/div>\n\n\n\n<p>\u3057\u304b\u3057\u3001\u30c0\u30a4\u30a2\u30ed\u30b0\u3092\u958b\u3044\u305f\u6642\u306f\u3001dialogRef.current \u304c null\u306b\u306a\u3063\u3066\u304a\u308a\u30b9\u30af\u30ed\u30fc\u30eb\u51e6\u7406\u304c\u5b9f\u884c\u3059\u3053\u308d\u3068\u304c\u51fa\u6765\u306a\u3044\u3002<br>\u306a\u305cdialogRef.current\u304cnull\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u304b\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong><span class=\"fz-16px\"><span class=\"fz-28px\">\u539f\u56e0<\/span><\/span><\/strong><\/strong><\/h2>\n\n\n\n<p><strong>Dialog\u306f\u5e38\u306bDOM\u5185\u306b\u306f\u5b58\u5728\u305b\u305a\u3001Dialog\u3092\u958b\u3044\u305f\u969b\u306b\u30de\u30a6\u30f3\u30c8\u3055\u308c\u308b<\/strong>\u305f\u3081\u3002<br>open\u304ctrue\u306b\u306a\u3063\u305f\u6642\u70b9\u3067\u306f\u307e\u3060\u30de\u30a6\u30f3\u30c8\u3055\u308c\u3066\u304a\u3089\u305a\u3001dialogRef.current\u306fnull\u306b\u306a\u308b\u3089\u3057\u3044\u3002<br><br>&lt;\u88dc\u8db3&gt;<br><strong>\u30de\u30a6\u30f3\u30c8\u3000\u3000\u3000:<\/strong>\u3000DOMnode\u304cDOM\u30c4\u30ea\u30fc\u306b\u8ffd\u52a0\u3055\u308c\u308b\u3053\u3068<br>\u3061\u306a\u307f\u306b\u6df7\u540c\u3057\u3084\u3059\u3044\u3082\u306e\u3068\u3057\u3066&#8221;\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0&#8221;\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306freact\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089DOMnode\u3092\u4f5c\u308b\u305f\u3081\u306bprops\u3068\u304bstate\u306a\u3069\u304c\u8aad\u307f\u8fbc\u307e\u308c\u308b\u51e6\u7406\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong><span class=\"fz-16px\"><span class=\"fz-28px\">\u89e3\u6c7a\u65b9\u6cd5<\/span><\/span><\/strong><\/strong><\/h2>\n\n\n\n<p>ref\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306fstate\u306e\u3088\u3046\u306b\u4e2d\u8eab\u306b\u5909\u66f4\u304c\u3042\u3063\u305f\u969b\u306e\u901a\u77e5\u306f\u3055\u308c\u306a\u3044\u306e\u3067\u3001useEffect\u306f\u4f7f\u3048\u306a\u3044\u3002<br>React\u516c\u5f0f\u306b\u3082\u8a18\u8f09\u304c\u3042\u308b\u304c\u3001ref\u5c5e\u6027\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u6e21\u3057\u3066\u3001\u305d\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306e\u4e2d\u3067\u30b9\u30af\u30ed\u30fc\u30eb\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\uff01<br><strong>\u307e\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306e\u5f15\u6570\u306b\u306f\u3001node\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u53ef\u80fd\uff01\uff01<span class=\"fz-20px\">(\u2190\u3053\u3053\u304c\u30dd\u30a4\u30f3\u30c8)<\/span><\/strong><br>\u95a2\u6570\u306fref\u5c5e\u6027\u306b\u66f4\u65b0\u304c\u3042\u308b\uff08\u30de\u30a6\u30f3\u30c8\u3059\u308b\uff09\u5ea6\u306b\u547c\u3073\u51fa\u3055\u308c\u308b\u306e\u3067\u3001<br>\u3064\u307e\u308a\u3001\u30de\u30a6\u30f3\u30c8\u3055\u308c\u3066node\u304c\u53c2\u7167\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u6bb5\u968e\u3067\u4f55\u3089\u304b\u306e\u51e6\u7406\u304c\u53ef\u80fd\u306b\u306a\u308b\u3002<br>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>cost scrollDialog = useCallback((node) =&gt;\u3000{\u3000\/\/ \u5f15\u6570\u306bnode\u3092\u53d7\u3051\u53d6\u308b\n    if (!node) return;  \/\/ node\u304cnull\u306e\u5834\u5408\u306f\u30ea\u30bf\u30fc\u30f3\u3057\u3066\u51e6\u7406\u7d42\u4e86\n    if (node.scrollTop !== 2000) {\n        node.scrollTo(0, 2000); \/\/ \u30b9\u30af\u30ed\u30fc\u30eb\u51e6\u7406\n    }\n},[]);\n\n~~~~~~~~~~~~~~\u7701\u7565~~~~~~~~~~~~~~~~\n      &lt;Dialog\n        open={open}\u3000\u3000\u3000\/\/  \u30c0\u30a4\u30a2\u30ed\u30b0\u306e\u958b\u9589state\n        onClose={handleClose}\n        scroll=&quot;paper&quot;\n        aria-labelledby=&quot;scroll-dialog-title&quot;\n        aria-describedby=&quot;scroll-dialog-description&quot;\n      &gt;\n        &lt;DialogTitle id=&quot;scroll-dialog-title&quot;&gt;Subscribe&lt;\/DialogTitle&gt;\n        &lt;DialogContent\n          dividers\n\u3000\u3000\u3000\u3000\u3000\u3000    ref={scrollDialog}    \/\/  ref\u5c5e\u6027\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u30bb\u30c3\u30c8\n        &gt;<\/code><\/pre><\/div>\n\n\n\n<p>React\u516c\u5f0f\u306f<a href=\"https:\/\/ja.reactjs.org\/docs\/hooks-faq.html#how-can-i-measure-a-dom-node\" target=\"_blank\" rel=\"noopener\" title=\"\u3053\u3061\u3089\">\u3053\u3061\u3089<\/a><br><br>\u6700\u521d\u306f\u3001\u306a\u305cref\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304cnull\u306b\u306a\u308b\u306e\u304b\u539f\u56e0\u304c\u63b4\u3081\u305a\u3001\u89e3\u6c7a\u306b\u7d50\u69cb\u6642\u9593\u3092\u8981\u3057\u3066\u3057\u307e\u3063\u305f\u3002<br>\u306a\u3093\u3060\u304b\u3093\u3060\u516c\u5f0f\u3092\u8aad\u3080\u306e\u306f\u3084\u306f\u308a\u5927\u4e8b\u3068\u5b9f\u611f\u3057\u307e\u3057\u305f?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\uff01\u73fe\u5728React\u3067\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306e\u958b\u767a\u3092\u62c5\u5f53\u3057\u3066\u3044\u308b\u571f\u4e95\u3067\u3059?\uff01\u4eca\u56de\u306fDOM\u53c2\u7167\u306b\u4f7f\u3046ref\u5c5e\u6027\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u3001\u5b9f\u969b\u306e\u696d\u52d9\u3067\u8a70\u307e\u3063\u305f\u4e8b\u3068\u89e3\u6c7a\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\uff01 \u5b9f\u73fe\u3057\u305f\u3044\u3053\u3068 \u30da\u30fc\u30b8\u304b\u3089\u30dc\u30bf\u30f3\u3092\u62bc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"swell_btn_cv_data":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[145,80,2],"class_list":["post-2095","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-html","tag-javascript","tag-react"],"jetpack_featured_media_url":"https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2023\/02\/react.jpeg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/2095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/comments?post=2095"}],"version-history":[{"count":58,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/2095\/revisions"}],"predecessor-version":[{"id":2242,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/2095\/revisions\/2242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/media\/2139"}],"wp:attachment":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/media?parent=2095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/categories?post=2095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/tags?post=2095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}