{"id":210386,"date":"2020-11-28T08:31:25","date_gmt":"2020-11-28T00:31:25","guid":{"rendered":"http:\/\/4563.org\/?p=210386"},"modified":"2020-11-28T08:31:25","modified_gmt":"2020-11-28T00:31:25","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-react-hooks-%e5%b0%81%e8%a3%85%e4%b8%80%e4%b8%aa%e9%bc%a0%e6%a0%87%e7%bb%98%e5%9b%be%e7%bb%84%e4%bb%b6%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=210386","title":{"rendered":"\u5982\u4f55\u7528 react hooks \u5c01\u88c5\u4e00\u4e2a\u9f20\u6807\u7ed8\u56fe\u7ec4\u4ef6\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u7528 react hooks \u5c01\u88c5\u4e00\u4e2a\u9f20\u6807\u7ed8\u56fe\u7ec4\u4ef6\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8fd9\u662f\u6211\u60f3\u8981\u5c01\u88c5\u7684\u4e00\u4e2a\u9f20\u6807\u753b\u56fe\u7ec4\u4ef6\uff0c\u4e0d\u505c\u7684\u89e6\u53d1 mouseMove \u83b7\u53d6\u5f53\u524d\u7684\u4f4d\u7f6e\uff0c\u540c\u65f6\u53c8\u7f13\u5b58\u4e86\u4e0a\u4e00\u6b21\u7684\u4f4d\u7f6e\uff0c\u8fde\u63a5\u4e24\u4e2a\u70b9\u5373\u53ef\uff0c\u4f46\u662f\u6211\u60f3\u8981\u7528 hooks \u5c01\u88c5\uff0c\u611f\u89c9\u5f88\u96be\uff0c\u6ca1\u6709\u5934\u7eea\u3002<\/p>\n<pre><code>\/\/ When true, moving the mouse draws on the canvas let isDrawing = false; let x = 0; let y = 0;  const myPics = document.getElementById('myPics'); const context = myPics.getContext('2d');  \/\/ event.offsetX, event.offsetY gives the (x,y) offset from the edge of the canvas.  \/\/ Add the event listeners for mousedown, mousemove, and mouseup myPics.addEventListener('mousedown', e =&gt; {   x = e.offsetX;   y = e.offsetY;   isDrawing = true; });  myPics.addEventListener('mousemove', e =&gt; {   if (isDrawing === true) {     drawLine(context, x, y, e.offsetX, e.offsetY);     x = e.offsetX;     y = e.offsetY;   } });  window.addEventListener('mouseup', e =&gt; {   if (isDrawing === true) {     drawLine(context, x, y, e.offsetX, e.offsetY);     x = 0;     y = 0;     isDrawing = false;   } });  function drawLine(context, x1, y1, x2, y2) {   context.beginPath();   context.strokeStyle = 'black';   context.lineWidth = 1;   context.moveTo(x1, y1);   context.lineTo(x2, y2);   context.stroke();   context.closePath(); } <\/code><\/pre>\n<p>\u8fd9\u662f\u6211\u5199\u51fa\u6765\u7684\u5b8c\u5168\u4e0d\u80fd\u753b\u56fe\u7684 hook \uff08\u6211\u4f30\u8ba1\u662f\u56e0\u4e3a\u6ca1\u6709\u50cf\u4e0a\u9762\u90a3\u6837\u83b7\u53d6\u4e0a\u6b21\u4f4d\u7f6e\u548c\u5f53\u524d\u4f4d\u7f6e\u7684\u539f\u56e0<\/p>\n<pre><code>const Canvas = () =&gt; {     const canvasRef = React.useRef(null);     \/\/const [paths, setPaths] = useState([]);     const [isDrawing, setIsDrawing] = useState(false);     const [pos, setPos] = useState({         x: 0,         y: 0,     });     const [isMouseDown, setIsMouseDown] = useState(false);     const [isMouseMove, setIsMouseMove] = useState(false);     const [isMouseUp, setIsMouseUp] = useState(false);     useEffect(() =&gt; {         const ctx = canvasRef.current.getContext(\"2d\");         if (isDrawing) {             console.log(\"isDrawing\");             ctx.beginPath();             ctx.strokeStyle = 'black';             ctx.lineWidth = 1;             ctx.lineTo(pos.x, pos.y);             ctx.stroke();             ctx.closePath();         }     }, [isMouseDown, isDrawing, pos]);     const handleMouseDown = (e) =&gt; {         setPos({             x: e.clientX,             y: e.clientY,         });         setIsDrawing(true);         setIsMouseDown(true);     }      const handleMouseUp = () =&gt; {         setIsDrawing(false);         setIsMouseUp(false);     }     const handleMouseMove = (e) =&gt; {         \/\/ console.log(e);         setIsMouseMove(true);         setPos({             x: e.clientX,             y: e.clientY,         });         \/\/console.log(state.ctx);     }     return (         &lt;div className=\"back-div\" &gt;             &lt;canvas ref={canvasRef} id=\"canvas1\" className=\"paint-canvas\" onMouseDown={handleMouseDown} onMouseMove={handleMouseMove} onMouseUp={handleMouseUp}&gt;             &lt;\/canvas&gt;         &lt;\/div&gt;     ) } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>17<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4361959\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u987a\u5e26\u4e00\u63d0\uff0c\u6211\u7684 e.offsetX \u4e3a\u4ec0\u4e48\u8bfb\u51fa\u6765\u662f undefined                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361960\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u554a\uff0c\u522b\u6c89\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361961\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : frankwei777 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             react \u7684 offsetX \u5728 event.nativeEvent \u4e2d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361962\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shenyu1996 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/codesandbox.io\/s\/busy-saha-rs7f9<br \/>\u7a0d\u7a0d\u6539\u4e86\u4e00\u4e0b \u53ef\u4ee5\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361963\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @frankwei777 \u8c22\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361964\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @shenyu1996 \u8c22\u8c22\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361965\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : v135ex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6709\u5565\u96be\u7684\uff0c\u81ea\u5df1\u591a\u60f3\u60f3\u5c31\u5199\u51fa\u6765\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361966\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @v135ex <br \/>\u8fd8\u662f\u6709\u70b9\u96be\u7684\uff0c\u4f60\u53ef\u4ee5\u8bd5\u8bd5\uff0ccss \u6539\u52a8 canvas \u5927\u5c0f\u7684\u8bdd\u9f20\u6807\u4f4d\u7f6e\u548c canvas \u4e0a\u7ed8\u56fe\u5750\u6807\u662f\u4e0d\u4e00\u81f4\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361967\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : IamJ <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @weimo383 \u7528\u6807\u7b7e\u5c5e\u6027\u8bbe\u7f6e\u5927\u5c0f\u800c\u4e0d\u662f\u6837\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361968\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : v135ex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @weimo383 \u597d\u7684 \u6ca1\u6709\u5c1d\u8bd5\u8fc7\u5984\u4e0b\u5b9a\u8bba\u4e86\uff0c\u6211\u4e5f\u5c1d\u8bd5\u4e0b\u5b66\u4e60\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361969\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xrr2016 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u7684\u5efa\u8bae\u662f\u7528\u6210\u719f\u7684\u7ed8\u56fe\u5e93\uff0c\u4f8b\u5982 p5.js \u8fd9\u79cd\uff0c\u7136\u540e\u5728\u7528 hooks \u505a\u4f60\u9700\u8981\u7684\u529f\u80fd                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361970\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xrr2016 \u4e3b\u8981\u662f\u8fd8\u662f\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u5e93\uff08\u589e\u52a0\u8d1f\u62c5\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361971\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : funnyecho <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e2a\u4eba\u770b\u6cd5\uff0c\u8fd9\u91cc\u7528 react hook \uff08 useState \uff09\u5176\u5b9e\u6ca1\u6709\u4ec0\u4e48\u610f\u4e49\uff0c\u4f60\u7b2c\u4e00\u6bb5\u4ee3\u7801\u90fd\u81ea\u6210\u4e00\u4f53\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u6765\u7684\u66f4\u5feb\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7b49\u5f85 react setState \u7684\u8c03\u5ea6\u3002<\/p>\n<p>\u4e0e\u5176\u8bf4\u4f7f\u7528 hook\uff0c\u4e0d\u5982\u628a\u7b2c\u4e00\u6bb5\u4ee3\u7801\u5c01\u88c5\u6210\u4e00\u4e2a\u7eaf\u51fd\u6570\uff0c\u63a5\u53d7\u4e00\u4e2a ref \u6765\u5904\u7406\u7ed8\u56fe\uff0c\u5728\u5916\u90e8 FC \u4e2d\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u7684\u66f4\u597d\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361972\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @funnyecho ref \u53ef\u4ee5\u5728\u7ec4\u4ef6\u4e2d\u4f20\u9012\u5417\uff1f<br \/>\u5927\u4f6c\u80fd\u4e0d\u80fd\u518d\u5177\u4f53\u4e00\u4e9b\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361973\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : funnyecho <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @weimo383 <\/p>\n<p>\u628a\u7b2c\u4e00\u6bb5\u4ee3\u7801\u5c01\u88c5\u6210\u51fd\u6570\uff1a<br \/>&#8220;`<br \/>\/\/ \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a canvas element\uff0c\u5e76\u8fd4\u56de destructor \u51fd\u6570\u7528\u6765 removeListener<br \/>type draw = ($canvas) =&gt; (() =&gt; void)<br \/>&#8220;`<\/p>\n<p>\u7136\u540e\uff0cReact.FC \u4e2d\u8c03\u7528 draw \u65b9\u6cd5\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a<br \/>&#8220;`<br \/>useEffect(() =&gt; {<\/p>\n<p>return draw(canvasRef.current)<br \/>}, [canvasRef.current])<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361974\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : weimo383 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @funnyecho \u6211\u7684\u7ec4\u4ef6\u5f97\u8981\u80fd\u591f\u4fdd\u5b58\u7ed8\u5236\u5386\u53f2\u8bb0\u5f55\uff0c\u6240\u4ee5\u526f\u4f5c\u7528\u548c\u72b6\u6001\u662f\u76f8\u5173\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4361975\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : funnyecho <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @weimo383 \u770b\u4ee3\u7801\u5c31\u51e0\u4e2a\u72b6\u6001\uff0c\u6bcf\u6b21 mousedown \u90fd\u4f1a\u91cd\u7f6e\uff0c\u4fdd\u5b58\u5728 draw \u51fd\u6570\u5185\u90e8\u7684\u4f5c\u7528\u57df\u5c31\u597d\u4e86\u5440\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 react hooks \u5c01&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/210386"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210386"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/210386\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210386"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}