{"id":52078,"date":"2024-12-03T08:01:23","date_gmt":"2024-12-03T00:01:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52078\/"},"modified":"2024-12-03T08:01:23","modified_gmt":"2024-12-03T00:01:23","slug":"%e4%ba%86%e8%a7%a3-react-%e4%b8%ad%e7%9a%84forwardref%ef%bc%9a%e7%bb%bc%e5%90%88%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52078\/","title":{"rendered":"\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731974626673bd5e24bf00.jpg\" class=\"aligncenter\" title=\"\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357\u63d2\u56fe\" alt=\"\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p>\u968f\u7740 react \u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u5f00\u53d1\u4eba\u5458\u7ecf\u5e38\u4f1a\u9047\u5230\u589e\u5f3a\u7ec4\u4ef6\u7075\u6d3b\u6027\u548c\u53ef\u7528\u6027\u7684\u6a21\u5f0f\u3002\u5176\u4e2d\u4e00\u4e2a\u6a21\u5f0f\u662fforwardref\uff0c\u8fd9\u662f\u4e00\u9879\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u7ec4\u4ef6\u5c06\u5f15\u7528\u4f20\u9012\u7ed9\u5176\u5b50\u7ec4\u4ef6\uff0c\u4ece\u800c\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5e95\u5c42 dom \u5143\u7d20\u6216\u7ec4\u4ef6\u5b9e\u4f8b\u3002\u5728\u8fd9\u7bc7\u535a\u5ba2\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u4ec0\u4e48\u662fforwardref\u3001\u4f55\u65f6\u4f7f\u7528\u5b83\u4ee5\u53ca\u5b83\u5982\u4f55\u7b80\u5316\u60a8\u7684 react \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f\u524d\u5411\u5f15\u7528\uff1f <\/h2>\n<p>forwardref \u662f\u4e00\u4e2a\u9ad8\u9636\u7ec4\u4ef6\uff0c\u4f7f\u60a8\u80fd\u591f\u521b\u5efa\u53ef\u4ee5\u8f6c\u53d1\u5230\u5b50\u7ec4\u4ef6\u7684\u5f15\u7528\u3002\u5f53\u60a8\u5e0c\u671b\u7236\u7ec4\u4ef6\u76f4\u63a5\u4e0e\u5b50\u7ec4\u4ef6\u7684 dom \u8282\u70b9\u4ea4\u4e92\u65f6\uff0c\u8fd9\u7279\u522b\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u60a8\u9700\u8981\u7ba1\u7406\u7126\u70b9\u3001\u52a8\u753b\u6216\u4e0e\u4f9d\u8d56\u4e8e refs \u7684\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48\u4f7f\u7528forwardref\uff1f <\/h3>\n<p>\u4f7f\u7528forwardref\u6709\u51e0\u4e2a\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li> <strong>\u76f4\u63a5 dom \u64cd\u4f5c<\/strong>\uff1a\u5141\u8bb8\u7236\u7ec4\u4ef6\u76f4\u63a5\u64cd\u4f5c\u5b50\u7ec4\u4ef6\u7684 dom\u3002<\/li>\n<li> <strong>\u4e0e\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210<\/strong>\uff1a\u8bb8\u591a\u5e93\u90fd\u5e0c\u671b\u5c06\u5f15\u7528\u4f20\u9012\u7ed9\u7ec4\u4ef6\uff0c\u800cforwardref \u4f7f\u8fd9\u4e00\u5207\u53d8\u5f97\u65e0\u7f1d\u3002<\/li>\n<li> <strong>\u53ef\u91cd\u7528\u7ec4\u4ef6<\/strong>\uff1a\u5b83\u6709\u52a9\u4e8e\u4fdd\u6301\u5c01\u88c5\u6027\uff0c\u540c\u65f6\u5411\u7236\u7ec4\u4ef6\u516c\u5f00\u5fc5\u8981\u7684\u529f\u80fd\uff0c\u4ece\u800c\u589e\u5f3a\u53ef\u91cd\u7528\u6027\u3002<\/li>\n<\/ol>\n<h2> \u8bed\u6cd5\u6982\u8ff0 <\/h2>\n<p>forwardref \u7684\u8bed\u6cd5\u5f88\u7b80\u5355\u3002\u5b83\u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>const componentwithref = react.forwardref((props, ref) =&gt; {\n  return &lt;div ref={ref}&gt;hello, world!&lt;\/div&gt;;\n});\n<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cref\u88ab\u4f20\u9012\u7ed9\u5e95\u5c42\u7684&lt;div&gt;\uff0c\u5141\u8bb8\u7236\u7ec4\u4ef6\u8bbf\u95ee\u5b83\u3002<\/p>\n<p><strong><em>\u5b9e\u73b0\u793a\u4f8b\uff1a<\/em><\/strong><\/p>\n<p><em>\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\u6765\u4e86\u89e3\u5982\u4f55\u4f7f\u7528forwardref\u3002<\/em><\/p>\n<pre>import react, { useref } from 'react';\n\nconst custominput = react.forwardref((props, ref) =&gt; {\n  return &lt;input ref={ref} {...props} \/&gt;;\n});\n\nconst parentcomponent = () =&gt; {\n  const inputref = useref();\n\n  const focusinput = () =&gt; {\n    inputref.current.focus(); \/\/ directly focuses the input element\n  };\n\n  return (\n    &lt;&gt;\n      &lt;custominput ref={inputref} placeholder=\"type here\" \/&gt;\n      &lt;button onclick={focusinput}&gt;focus input&lt;\/button&gt;\n    &lt;\/&gt;\n  );\n};\n\nexport default parentcomponent;\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<p><strong>custominput\uff1a<\/strong>\u8fd9\u662f\u4e00\u4e2a\u529f\u80fd\u7ec4\u4ef6\uff0c\u5b83\u4f7f\u7528forwardref\u5c06\u5176\u5f15\u7528\u8f6c\u53d1\u5230\u5e95\u5c42\u7684&lt;input&gt;\u5143\u7d20\u3002<\/p>\n<p><strong>parentcomponent:<\/strong>\uff1a\u6b64\u7ec4\u4ef6\u4f7f\u7528 useref \u6302\u94a9\u521b\u5efa\u5f15\u7528 (inputref)\u3002\u5355\u51fb\u6309\u94ae\u65f6\uff0c\u4f1a\u8c03\u7528 focusinput\uff0c\u76f4\u63a5\u805a\u7126\u8f93\u5165\u5b57\u6bb5\u3002<\/p>\n<p><strong><em>\u6ca1\u6709forwardref\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/em><\/strong><\/p>\n<p>\u5982\u679c\u60a8\u5728\u4e0d\u4f7f\u7528forwardref\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u7ec4\u4ef6\uff0c\u60a8\u5c06\u9047\u5230\u67d0\u4e9b\u9650\u5236\u3002\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u8fd9\u4e00\u70b9\uff1a<\/p>\n<pre>import React, { useRef } from 'react';\n\nconst CustomInput = ({ placeholder }) =&gt; {\n  return &lt;input placeholder={placeholder} \/&gt;;\n};\n\nconst ParentComponent = () =&gt; {\n  const inputRef = useRef();\n\n  const focusInput = () =&gt; {\n    \/\/ This will NOT work\n    inputRef.current.focus(); \/\/ Will throw an error\n  };\n\n  return (\n    &lt;&gt;\n      &lt;CustomInput ref={inputRef} placeholder=\"Type here\" \/&gt;\n      &lt;button onClick={focusInput}&gt;Focus Input&lt;\/button&gt;\n    &lt;\/&gt;\n  );\n};\n\nexport default ParentComponent;\n<\/pre>\n<p><strong>\u4e0d\u4f7f\u7528forwardref\u7684\u5f71\u54cd\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u8bbf\u95ee\u53d7\u9650\uff1a<\/strong>\u60a8\u5c06\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u8f93\u5165\u7684 dom \u8282\u70b9\uff0c\u4ece\u800c\u4f7f\u5f97\u8bf8\u5982\u805a\u7126\u8f93\u5165\u4e4b\u7c7b\u7684\u4efb\u52a1\u53d8\u5f97\u4e0d\u53ef\u80fd\u3002 2.** \u590d\u6742\u6027\u589e\u52a0**\uff1a\u60a8\u53ef\u80fd\u9700\u8981\u5c06 props \u4f20\u9012\u5230\u591a\u5c42\u7ec4\u4ef6\uff0c\u4ece\u800c\u5bfc\u81f4 prop \u94bb\u5b54\u3002<\/li>\n<li> <strong>\u53ef\u91cd\u7528\u6027\u964d\u4f4e<\/strong>\uff1a\u7ec4\u4ef6\u53d8\u5f97\u4e0d\u592a\u7075\u6d3b\uff0c\u5e76\u4e14\u66f4\u96be\u4e0e\u9700\u8981\u5f15\u7528\u7684\u5e93\u96c6\u6210\u3002<\/li>\n<li> <strong>\u6027\u80fd\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u4f7f\u7528forwardref\u53ef\u4ee5\u5728\u9700\u8981\u76f4\u63a5\u8bbf\u95eedom\u7684\u573a\u666f\u4e2d\u589e\u5f3a\u6027\u80fd\u3002\u901a\u8fc7\u5229\u7528\u5f15\u7528\uff0c\u60a8\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\u5e76\u7ef4\u62a4\u5e72\u51c0\u7684\u7ec4\u4ef6\u6811\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p><em><strong>\u7ed3\u8bba<\/strong><\/em><\/p>\n<p>forwardref \u662f react \u5f00\u53d1\u8005\u5de5\u5177\u5305\u4e2d\u7684\u91cd\u8981\u5de5\u5177\uff0c\u53ef\u4ee5\u521b\u5efa\u7075\u6d3b\u3001\u53ef\u91cd\u7528\u7684\u7ec4\u4ef6\u3002\u901a\u8fc7\u5141\u8bb8\u76f4\u63a5\u8bbf\u95ee dom \u8282\u70b9\u5e76\u4fc3\u8fdb\u4e0e\u7b2c\u4e09\u65b9\u5e93\u7684\u96c6\u6210\uff0c\u5b83\u589e\u5f3a\u4e86\u7ec4\u4ef6\u67b6\u6784\u3002\u91c7\u7528forwardref \u53ef\u4ee5\u4f7f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4ee3\u7801\u66f4\u7b80\u6d01\u5e76\u6539\u8fdb\u529f\u80fd\u3002\u5f53\u60a8\u7ee7\u7eed\u4f7f\u7528 react \u8fdb\u884c\u6784\u5efa\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u5229\u7528\u8fd9\u4e00\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5b9e\u73b0\u6700\u4f73\u7ec4\u4ef6\u8bbe\u8ba1\uff01<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u4f7f\u7528nhooyr.io\/websocket\u65f6\u9047\u5230\u201cnote module requires Go 1.13\u201d\u9519\u8bef\u600e\u4e48\u529e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357 \u6536\u85cf \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300a\u4e86\u89e3 React \u4e2d\u7684forwardRef\uff1a\u7efc\u5408\u6307\u5357\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u968f\u7740 react \u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u5f00\u53d1\u4eba\u5458\u7ecf\u5e38\u4f1a\u9047\u5230\u589e\u5f3a\u7ec4\u4ef6\u7075\u6d3b\u6027\u548c\u53ef\u7528\u6027\u7684\u6a21\u5f0f\u3002\u5176\u4e2d\u4e00\u4e2a\u6a21\u5f0f\u662fforwardref\uff0c\u8fd9\u662f\u4e00\u9879\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u7ec4\u4ef6\u5c06\u5f15\u7528\u4f20\u9012\u7ed9\u5176\u5b50\u7ec4\u4ef6\uff0c\u4ece\u800c\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5e95\u5c42 dom \u5143\u7d20\u6216\u7ec4\u4ef6\u5b9e\u4f8b\u3002\u5728\u8fd9\u7bc7\u535a\u5ba2\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u4ec0\u4e48\u662fforwardref\u3001\u4f55\u65f6\u4f7f\u7528\u5b83\u4ee5\u53ca\u5b83\u5982\u4f55\u7b80\u5316\u60a8\u7684 react \u5e94\u7528\u7a0b\u5e8f\u3002 \u4ec0\u4e48\u662f\u524d\u5411\u5f15\u7528\uff1f forwardref \u662f\u4e00\u4e2a\u9ad8\u9636\u7ec4\u4ef6\uff0c\u4f7f\u60a8\u80fd\u591f\u521b\u5efa\u53ef\u4ee5\u8f6c\u53d1\u5230\u5b50\u7ec4\u4ef6\u7684\u5f15\u7528\u3002\u5f53\u60a8\u5e0c\u671b\u7236\u7ec4\u4ef6\u76f4\u63a5\u4e0e\u5b50\u7ec4\u4ef6\u7684 dom \u8282\u70b9\u4ea4\u4e92\u65f6\uff0c\u8fd9\u7279\u522b\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u60a8\u9700\u8981\u7ba1\u7406\u7126\u70b9\u3001\u52a8\u753b\u6216\u4e0e\u4f9d\u8d56\u4e8e refs \u7684\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210\u7684\u60c5\u51b5\u4e0b\u3002 \u4e3a\u4ec0\u4e48\u4f7f\u7528forwardref\uff1f \u4f7f\u7528forwardref\u6709\u51e0\u4e2a\u4f18\u70b9\uff1a \u76f4\u63a5 dom \u64cd\u4f5c\uff1a\u5141\u8bb8\u7236\u7ec4\u4ef6\u76f4\u63a5\u64cd\u4f5c\u5b50\u7ec4\u4ef6\u7684 dom\u3002 \u4e0e\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210\uff1a\u8bb8\u591a\u5e93\u90fd\u5e0c\u671b\u5c06\u5f15\u7528\u4f20\u9012\u7ed9\u7ec4\u4ef6\uff0c\u800cforwardref \u4f7f\u8fd9\u4e00\u5207\u53d8\u5f97\u65e0\u7f1d\u3002 \u53ef\u91cd\u7528\u7ec4\u4ef6\uff1a\u5b83\u6709\u52a9\u4e8e\u4fdd\u6301\u5c01\u88c5\u6027\uff0c\u540c\u65f6\u5411\u7236\u7ec4\u4ef6\u516c\u5f00\u5fc5\u8981\u7684\u529f\u80fd\uff0c\u4ece\u800c\u589e\u5f3a\u53ef\u91cd\u7528\u6027\u3002 \u8bed\u6cd5\u6982\u8ff0 forwardref \u7684\u8bed\u6cd5\u5f88\u7b80\u5355\u3002\u5b83\u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a const componentwithref = react.forwardref((props, ref) =&gt; { return &lt;div ref={ref}&gt;hello, world!&lt;\/div&gt;; }); \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cref\u88ab\u4f20\u9012\u7ed9\u5e95\u5c42\u7684&lt;div&gt;\uff0c\u5141\u8bb8\u7236\u7ec4\u4ef6\u8bbf\u95ee\u5b83\u3002 \u5b9e\u73b0\u793a\u4f8b\uff1a \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\u6765\u4e86\u89e3\u5982\u4f55\u4f7f\u7528forwardref\u3002 import react, { useref } from &#8216;react&#8217;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52078","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52078"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52078\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}