{"id":192422,"date":"2020-11-09T09:27:35","date_gmt":"2020-11-09T01:27:35","guid":{"rendered":"http:\/\/4563.org\/?p=192422"},"modified":"2020-11-15T18:53:49","modified_gmt":"2020-11-15T10:53:49","slug":"react-hooks-%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e7%9a%84%e6%ad%a3%e7%a1%ae%e5%a7%bf%e5%8a%bf","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=192422","title":{"rendered":"React Hooks \u6027\u80fd\u4f18\u5316\u7684\u6b63\u786e\u59ff\u52bf"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  React Hooks \u6027\u80fd\u4f18\u5316\u7684\u6b63\u786e\u59ff\u52bf               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u524d\u8a00<\/h2>\n<p><code>React Hooks<\/code> \u51fa\u6765\u5f88\u957f\u4e00\u6bb5\u65f6\u95f4\u4e86\uff0c\u76f8\u4fe1\u6709\u4e0d\u5c11\u7684\u670b\u53cb\u5df2\u7ecf\u6df1\u5ea6\u4f7f\u7528\u4e86\u3002\u65e0\u8bba\u662f <code>React<\/code> \u672c\u8eab\uff0c\u8fd8\u662f\u5176\u751f\u6001\u4e2d\uff0c\u90fd\u5728\u6478\u7d22\u7740\u8fdb\u6b65\u3002\u9274\u4e8e\u6211\u4f7f\u7528\u7684 <code>React<\/code> \u7684\u7ecf\u9a8c\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u3002\u5bf9\u4e8e <code>React hooks<\/code>\uff0c\u6027\u80fd\u4f18\u5316\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u7740\u624b\u8003\u8651\u3002<\/p>\n<h2>\u573a\u666f 1<\/h2>\n<p>\u5728\u4f7f\u7528\u4e86 <code>React Hooks<\/code> \u540e\uff0c\u5f88\u591a\u4eba\u90fd\u4f1a\u62b1\u6028\u6e32\u67d3\u7684\u6b21\u6570\u53d8\u591a\u4e86\u3002\u6ca1\u9519\uff0c\u5b98\u65b9\u5c31\u662f\u8fd9\u4e48\u63a8\u8350\u7684\uff1a<\/p>\n<blockquote>\n<p>\u6211\u4eec\u63a8\u8350\u628a <code>state<\/code> \u5207\u5206\u6210\u591a\u4e2a <code>state<\/code> \u53d8\u91cf\uff0c\u6bcf\u4e2a\u53d8\u91cf\u5305\u542b\u7684\u4e0d\u540c\u503c\u4f1a\u5728\u540c\u65f6\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<\/blockquote>\n<pre><code>function Box() {   const [position, setPosition] = useState({ left: 0, top: 0 });   const [size, setSize] = useState({ width: 100, height: 100 });   \/\/ ... } <\/code><\/pre>\n<p>\u8fd9\u79cd\u5199\u6cd5\u5728\u540c\u65f6 <code>setPosition<\/code> \u548c <code>setSize<\/code> \u7684\u65f6\u5019\uff0c\u76f8\u8f83\u4e8e class \u5199\u6cd5\u4f1a\u989d\u5916\u591a\u51fa\u4e00\u6b21 <code>render<\/code>\u3002\u8fd9\u4e5f\u5c31\u662f\u6e32\u67d3\u6b21\u6570\u53d8\u591a\u7684\u6839\u672c\u539f\u56e0\u3002\u5f53\u7136\u8fd9\u79cd\u5199\u6cd5\u4ecd\u7136\u503c\u5f97\u63a8\u8350\uff0c\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u66f4\u9ad8\uff0c\u80fd\u66f4\u597d\u7684\u903b\u8f91\u5206\u79bb\u3002<\/p>\n<p>\u9488\u5bf9\u8fd9\u79cd\u573a\u666f\u82e5\u51fa\u73b0\u5341\u51e0\u6216\u51e0\u5341\u4e2a <code>useState<\/code> \u7684\u65f6\u5019\uff0c\u53ef\u8bfb\u6027\u5c31\u4f1a\u53d8\u5dee\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u8981\u76f8\u5173\u6027\u7684\u7ec4\u4ef6\u5316\u4e86\u3002\u4ee5\u903b\u8f91\u4e3a\u5bfc\u5411\uff0c\u62bd\u79bb\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\uff0c\u501f\u52a9 <code>React.memo<\/code> \u6765\u5c4f\u853d\u5176\u4ed6 <code>state<\/code> \u5bfc\u81f4\u7684 <code>rerender<\/code>\u3002<\/p>\n<pre><code>const Position = React.memo(({ position }: PositionProps) =&gt; {   \/\/ position \u76f8\u5173\u903b\u8f91   return (     &lt;div&gt;{position.left}&lt;\/div&gt;   ); }); <\/code><\/pre>\n<p>\u56e0\u6b64\u5728 <code>React hooks<\/code> \u7ec4\u4ef6\u4e2d\u5c3d\u91cf\u4e0d\u8981\u5199\u6d41\u6c34\u7ebf\u4ee3\u7801\uff0c\u4fdd\u6301\u5728 200 \u884c\u5de6\u53f3\u6700\u4f73\uff0c\u901a\u8fc7\u7ec4\u4ef6\u5316\u964d\u4f4e\u8026\u5408\u548c\u590d\u6742\u5ea6\uff0c\u8fd8\u80fd\u4f18\u5316\u4e00\u5b9a\u7684\u6027\u80fd\u3002<\/p>\n<h2>\u573a\u666f 2<\/h2>\n<p><code>class<\/code> \u5bf9\u6bd4 <code>hooks<\/code>\uff0c\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre><code>class Counter extends React.Component {   state = {     count: 0,   };    increment = () =&gt; {     this.setState((prev) =&gt; ({       count: prev.count + 1,     }));   };    render() {     const { count } = this.state;     return &lt;ChildComponent count={count} onClick={this.increment} \/&gt;;   } } <\/code><\/pre>\n<pre><code>function Counter() {   const [count, setCount] = React.useState(0);    function increment() {     setCount((n) =&gt; n + 1);   }   return &lt;ChildComponent count={count} onClick={increment} \/&gt;; } <\/code><\/pre>\n<p>\u51ed\u76f4\u89c2\u611f\u53d7\uff0c\u4f60\u662f\u5426\u4f1a\u89c9\u5f97 <code>hooks<\/code> \u7b49\u540c\u4e8e <code>class<\/code> \u7684\u5199\u6cd5\uff1f\u9519\uff0c<code>hooks<\/code> \u7684\u5199\u6cd5\u5df2\u7ecf\u57cb\u4e86\u4e00\u4e2a\u5751\u3002\u5728 <code>count<\/code> \u72b6\u6001\u66f4\u65b0\u7684\u65f6\u5019\uff0c <code>Counter<\/code> \u7ec4\u4ef6\u4f1a\u91cd\u65b0\u6267\u884c\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f1a\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570 <code>increment<\/code>\u3002\u8fd9\u6837\u4f20\u9012\u7ed9 <code>ChildComponent<\/code> \u7684 <code>onClick<\/code> \u6bcf\u6b21\u90fd\u662f\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u4ece\u800c\u5bfc\u81f4 <code>ChildComponent<\/code> \u7ec4\u4ef6\u7684 <code>React.memo<\/code> \u5931\u6548\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a<\/p>\n<pre><code>function usePersistFn&lt;T extends (...args: any[]) =&gt; any&gt;(fn: T) {   const ref = React.useRef&lt;Function&gt;(() =&gt; {     throw new Error('Cannot call function while rendering.');   });   ref.current = fn;   return React.useCallback(ref.current as T, [ref]); } <\/code><\/pre>\n<pre><code>\/\/ \u5efa\u8bae\u4f7f\u7528 `usePersistFn` const increment = usePersistFn(() =&gt; {   setCount((n) =&gt; n + 1); }); \/\/ \u6216\u8005\u4f7f\u7528 useCallback const increment = React.useCallback(() =&gt; {   setCount((n) =&gt; n + 1); }, []); <\/code><\/pre>\n<p>\u4e0a\u9762\u58f0\u660e\u4e86 <code>usePersistFn<\/code> \u81ea\u5b9a\u4e49 <code>hook<\/code>\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u51fd\u6570\u5730\u5740\u5728\u672c\u7ec4\u4ef6\u4e2d\u6c38\u8fdc\u4e0d\u4f1a\u53d8\u5316\u3002\u5b8c\u7f8e\u89e3\u51b3 <code>useCallback<\/code> \u4f9d\u8d56\u503c\u53d8\u5316\u800c\u91cd\u65b0\u751f\u6210\u65b0\u51fd\u6570\u7684\u95ee\u9898\uff0c\u903b\u8f91\u91cf\u5927\u7684\u7ec4\u4ef6\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528\u3002<\/p>\n<p>\u4e0d\u4ec5\u4ec5\u662f\u51fd\u6570\uff0c\u6bd4\u5982\u6bcf\u6b21 <code>render<\/code> \u6240\u521b\u5efa\u7684\u65b0\u5bf9\u8c61\uff0c\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\u90fd\u4f1a\u6709\u6b64\u7c7b\u95ee\u9898\u3002\u5c3d\u91cf\u4e0d\u5728\u7ec4\u4ef6\u7684\u53c2\u6570\u4e0a\u4f20\u9012\u56e0 <code>render<\/code> \u800c\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u6bd4\u5982 <code>style={{ width: 0 }}<\/code> \u6b64\u7c7b\u7684\u4ee3\u7801\u7528 <code>React.useMemo<\/code> \u6765\u4f18\u5316\u3002<\/p>\n<pre><code>const CustomComponent = React.memo(({ width }: CustomComponentProps) =&gt; {   const style = React.useMemo(() =&gt; ({ width } as React.CSSProperties), [width]);   return &lt;ChildComponent style={style} \/&gt;; }); <\/code><\/pre>\n<p><code>style<\/code> \u82e5\u4e0d\u9700\u6539\u53d8\uff0c\u53ef\u4ee5\u63d0\u53d6\u5230\u7ec4\u4ef6\u5916\u9762\u58f0\u660e\u3002\u5c3d\u7ba1\u8fd9\u6837\u505a\u5199\u6cd5\u611f\u89c9\u592a\u7e41\u7410\uff0c\u4f46\u662f\u4e0d\u4f9d\u8d56 <code>React.memo<\/code> \u91cd\u65b0\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\uff0c\u662f\u4f18\u5316\u6027\u80fd\u7684\u6709\u6548\u624b\u6bb5\u3002<\/p>\n<h2>\u573a\u666f 3<\/h2>\n<p>\u5bf9\u4e8e\u590d\u6742\u7684\u573a\u666f\uff0c\u4f7f\u7528 <code>useWhyDidYouUpdate<\/code> hook \u6765\u8c03\u8bd5\u5f53\u524d\u7684\u53ef\u53d8\u53d8\u91cf\u5f15\u8d77\u7684 <code>rerender<\/code>\u3002\u8fd9\u4e2a\u51fd\u6570\u4e5f\u53ef\u76f4\u63a5\u4f7f\u7528 <code>ahooks<\/code> \u4e2d\u7684\u5b9e\u73b0\u3002<\/p>\n<pre><code>function useWhyDidYouUpdate(name, props) {   const previousProps = useRef();    useEffect(() =&gt; {     if (previousProps.current) {       const allKeys = Object.keys({ ...previousProps.current, ...props });       const changesObj = {};       allKeys.forEach(key =&gt; {         if (previousProps.current[key] !== props[key]) {           changesObj[key] = {             from: previousProps.current[key],             to: props[key]           };         }       });        if (Object.keys(changesObj).length) {         console.log('[why-did-you-update]', name, changesObj);       }     }     previousProps.current = props;   }); }  const Counter = React.memo(props =&gt; {   useWhyDidYouUpdate('Counter', props);   return &lt;div style={props.style}&gt;{props.count}&lt;\/div&gt;; }); <\/code><\/pre>\n<p>\u5f53 <code>useWhyDidYouUpdate<\/code> \u4e2d\u6240\u76d1\u542c\u7684 props \u53d1\u751f\u4e86\u53d8\u5316\uff0c\u5219\u4f1a\u6253\u5370\u5bf9\u5e94\u7684\u503c\u5bf9\u6bd4\uff0c\u662f\u8c03\u8bd5\u4e2d\u7684\u795e\u5668\uff0c\u6781\u529b\u63a8\u8350\u3002<\/p>\n<h2>\u573a\u666f 4<\/h2>\n<p>\u501f\u52a9 Chrome Performance \u4ee3\u7801\u8fdb\u884c\u8c03\u8bd5\uff0c\u5f55\u5236\u4e00\u6bb5\u64cd\u4f5c\uff0c\u5728 <code>Timings<\/code> \u9009\u9879\u5361\u4e2d\u5206\u6790\u8017\u65f6\u6700\u957f\u903b\u8f91\u5728\u4ec0\u4e48\u5730\u65b9\uff0c\u4f1a\u5c55\u73b0\u51fa\u7ec4\u4ef6\u7684\u5c42\u7ea7\u6808\uff0c\u7136\u540e\u7cbe\u51c6\u4f18\u5316\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/1de1d08847fd44fba0de46989de72685tplv-k3u1fbpfcp-watermark.image_.png\" alt=\"React Hooks \u6027\u80fd\u4f18\u5316\u7684\u6b63\u786e\u59ff\u52bf\" \/><\/p>\n<h2>\u573a\u666f 5<\/h2>\n<p>\u5728 <code>React<\/code> \u4e2d\u662f\u6781\u529b\u63a8\u8350\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u53ef\u4ee5\u8ba9\u6570\u636e\u4e0d\u53ef\u53d8\u6027\u4f5c\u4e3a\u6211\u4eec\u4f18\u5316\u7684\u624b\u6bb5\u3002\u6211\u5728 <code>React<\/code> class \u65f6\u4ee3\u5927\u91cf\u4f7f\u7528\u4e86 <code>immutable.js<\/code> \u7ed3\u5408 <code>redux<\/code> \u6765\u642d\u5efa\u4e1a\u52a1\uff0c\u4e0e <code>React<\/code> \u4e2d <code>PureComponnet<\/code> \u5b8c\u7f8e\u914d\u5408\uff0c\u6027\u80fd\u4fdd\u6301\u975e\u5e38\u597d\u3002\u4f46\u662f\u5728 <code>React hooks<\/code> \u4e2d\u518d\u7ed3\u5408 <code>typescript<\/code> \u5b83\u5c31\u663e\u5f97\u6709\u70b9\u683c\u683c\u4e0d\u5165\u4e86\uff0c\u7c7b\u578b\u652f\u6301\u5f97\u4e0d\u662f\u5f88\u5b8c\u7f8e\u3002\u8fd9\u91cc\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e0b <code>immer.js<\/code>\uff0c\u5f15\u5165\u6210\u672c\u5c0f\uff0c\u5199\u6cd5\u4e5f\u7b80\u6d01\u4e86\u4e0d\u5c11\u3002<\/p>\n<pre><code>const nextState = produce(currentState, (draft) =&gt; {   draft.p.x.push(2); })   \/\/ true currentState === nextState; <\/code><\/pre>\n<h2>\u573a\u666f 6<\/h2>\n<p>\u590d\u6742\u573a\u666f\u4f7f\u7528 <code>Map<\/code> \u5bf9\u8c61\u4ee3\u66ff\u6570\u7ec4\u64cd\u4f5c\uff0c<code>map.get()<\/code>, <code>map.has()<\/code>\uff0c\u4e0e\u6570\u7ec4\u67e5\u627e\u76f8\u6bd4\u5c24\u5176\u9ad8\u6548\u3002<\/p>\n<pre><code>\/\/ Map const map = new Map([['a', { id: 'a' }], ['b', { id: 'b' }], ['c', { id: 'c' }]]);  \/\/ \u67e5\u627e\u503c map.has('a'); \/\/ \u83b7\u53d6\u503c map.get('a'); \/\/ \u904d\u5386 map.forEach(n =&gt; n);  \/\/ \u5b83\u53ef\u4ee5\u5f88\u5bb9\u6613\u8f6c\u6362\u4e3a\u6570\u7ec4 Array.from(map.values());  \/\/ \u6570\u7ec4 const list = [{ id: 'a' }, { id: 'b' }, { id: 'c' }];  \/\/ \u67e5\u627e\u503c list.some(n =&gt; n.id === 'a'); \/\/ \u83b7\u53d6\u503c list.find(n =&gt; n.id === 'a'); \/\/ \u904d\u5386 list.forEach(n =&gt; n);  <\/code><\/pre>\n<h2>\u7ed3\u8bed<\/h2>\n<p>React \u6027\u80fd\u8c03\u4f18\uff0c\u9664\u4e86\u963b\u6b62 <code>rerender<\/code>\uff0c\u8fd8\u6709\u4e0e\u5199\u4ee3\u7801\u7684\u65b9\u5f0f\u6709\u5173\u7cfb\u3002\u6700\u540e\uff0c\u6211\u8981\u63a8\u4e00\u4e0b\u8fd1\u671f\u5199\u7684 <code>React<\/code> \u72b6\u6001\u7ba1\u7406\u5e93 https:\/\/github.com\/MinJieLiu\/heo\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u6027\u80fd\u4f18\u5316\u7684\u4e00\u4e2a\u624b\u6bb5\uff0c\u5e0c\u671b\u5927\u5bb6\u4ece <code>redux<\/code> \u7684\u7e41\u7410\u4e2d\u89e3\u653e\u51fa\u6765\uff0c\u7701\u4e0b\u7684\u65f6\u95f4\u7528\u6765\u4eab\u53d7\u751f\u6d3b      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>21<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4150527\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : answeryou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &gt; \u8fd9\u79cd\u5199\u6cd5\u5728\u540c\u65f6 setPosition \u548c setSize \u7684\u65f6\u5019\uff0c\u76f8\u8f83\u4e8e class \u5199\u6cd5\u4f1a\u989d\u5916\u591a\u51fa\u4e00\u6b21 render<\/p>\n<p>\u4e24\u6b21 render? \u540c\u65f6\u8d4b\u503c\u53ea\u6709\u4e00\u6b21\u554a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150528\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cityboy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5927\u4f6c\u95ee\u4e2a\u9898\u5916\u8bdd\uff0creact hooks \u5728\u5b9e\u6218\u4e2d\u7528\u4ec0\u4e48\u5171\u4eab\u5168\u5c40\u53d8\u91cf\u554a\uff0cuseContext \u611f\u89c9\u4e0d\u662f\u5f88\u597d\u7528\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150529\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ruoxie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cityboy \u6211\u4eec\u5728\u7528 hox\uff0c\u7b80\u5355\u6613\u7528                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150530\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KuroNekoFan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5176\u5b9e depth \u4e0d\u8981\u7528 object\uff0c\u7528\u57fa\u672c\u7c7b\u578b\u6216\u8005\u81ea\u5b9a\u4e49\u7684\u5e8f\u5217\u5316\u51fd\u6570\u628a object \u5e8f\u5217\u5316\u6210 string\/number \u5c31\u53ef\u4ee5\u89e3\u51b3 object \u4f5c\u4e3a\u4f9d\u8d56\u5bfc\u81f4 unexceptable \u7684\u53d8\u5316\uff0cuseEffect \u548c useCallback \u90fd\u53ef\u4ee5\u8fd9\u6837\u5904\u7406                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150531\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @answeryou \u56e0\u4e3a\u662f\u4e24\u4e2a useState                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150532\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cityboy \u4f60\u53ef\u4ee5\u8bd5\u8bd5 https:\/\/github.com\/MinJieLiu\/heo\uff0chox \u4e5f\u884c\uff0c\u4e0d\u8fc7 hox \u597d\u50cf\u662f\u5355\u4f8b\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150533\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leelz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             React.memo \u662f\u4e00\u4e2a HOC\uff0c\u5982\u679c\u4e0d\u8db3\u4ee5\u62bd\u6210\u7ec4\u4ef6\u7684\u90a3\u5757\u4e5f\u7528 React.memo \u6765 wrapper\uff0c\u53cd\u800c\u4f1a\u9002\u5f97\u5176\u53cd\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150534\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : answeryou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liumingyi1 \u4e24\u4e2a useState \u540c\u65f6\u8bbe\u7f6e\u503c\u4e5f\u53ea\u4f1a\u4e00\u6b21 re-render \u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150535\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @leelz \u5e76\u4e0d\u7b97\u771f\u6b63\u610f\u4e49\u4e0a\u7684 HOC \u5427\uff0c\u53ea\u662f\u591a\u4e86\u5c42\u5bf9\u6bd4\uff0c\u5f71\u54cd\u5fae\u4e4e\u5176\u5fae                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150536\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @answeryou \u8fd9\u91cc\u7684\u786e\u662f\u6211\u5199\u9519\u4e86\uff0c\u5e94\u8be5\u8981\u52a0\u4e0a\u5f02\u6b65\u7684\u73af\u5883\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150537\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u573a\u666f 1\uff1a\u8fd9\u79cd\u5199\u6cd5\u5728\u5f02\u6b65\u7684\u6761\u4ef6\u4e0b\uff0c\u6bd4\u5982\u8c03\u7528\u63a5\u53e3\u8fd4\u56de\u540e\uff0c\u540c\u65f6 `setPosition` \u548c `setSize`\uff0c\u76f8\u8f83\u4e8e class \u5199\u6cd5\u4f1a\u989d\u5916\u591a\u51fa\u4e00\u6b21 `render`\u3002<\/p>\n<p>\u6587\u7ae0\u7684\u5931\u8bef\u656c\u8bf7\u8c05\u89e3                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150538\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leelz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liumingyi1 https:\/\/reactjs.org\/docs\/react-api.html#reactmemo \u201cReact.memo is a higher order component.\u201d<br \/>https:\/\/github.com\/facebook\/react\/blob\/master\/packages\/react\/src\/ReactMemo.js<br \/>https:\/\/github.com\/facebook\/react\/blob\/62efd9618b5027816cf7f8b54f5fc80b3d7af8ec\/packages\/react-reconciler\/src\/ReactFiberBeginWork.new.js#L1172<br \/>https:\/\/github.com\/facebook\/react\/blob\/62efd9618b5027816cf7f8b54f5fc80b3d7af8ec\/packages\/react-reconciler\/src\/ReactFiberBeginWork.new.js#L381                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150539\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nuanyang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5173\u4e8e useCallback \u548c useMemo \u7684\u4f7f\u7528\u9700\u8981\u5c0f\u5fc3\u70b9\u907f\u514d\u6ee5\u7528\uff0c\u989d\u5916\u7684 deps \u6bd4\u5bf9\u7684\u6210\u672c\u4ee5\u53ca\u989d\u5916\u7684\u63a7\u4ef6\u6210\u672c\u53cd\u800c\u4f1a\u5bf9\u6027\u80fd\u662f\u4e00\u79cd\u635f\u8017\u3002<br \/>https:\/\/jancat.github.io\/post\/2019\/translation-usememo-and-usecallback\/<br \/>https:\/\/blog.logrocket.com\/rethinking-hooks-memoization\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150540\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6027\u80fd\u4f18\u5316\u7684\u786e\u662f\u628a\u53cc\u5203\u5251                                                             <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150541\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : answeryou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liumingyi1 \u5f02\u6b65\u6761\u4ef6\u4e0b, \u4e0d\u8bba\u662f Hook \u8fd8\u662f Class \u5176\u5b9e\u8868\u73b0\u90fd\u662f\u4e00\u6837\u7684, \u90fd\u4f1a\u591a\u4e00\u6b21 render                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150542\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liumingyi1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @answeryou \u4e3b\u8981\u662f class \u4e0b\uff0csetState \u4e00\u822c\u90fd\u5728\u5bf9\u8c61\u91cc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150543\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : peterjose <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @KuroNekoFan \u5e8f\u5217\u5316\u6027\u80fd\u5dee\u5230\u8d1f\u4f18\u5316\u3002\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150544\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : peterjose <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nuanyang \u5bf9\u7684 \u5207\u5fcc\u63d0\u524d\u4f18\u5316                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150545\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gouflv <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5f88\u5b9e\u7528\u7684\u603b\u7ed3\u3002\u6bcf\u6b21\u5199 hooks \u90fd\u4f1a\u6000\u7591\u4eba\u751f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150546\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yikyo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             hooks \u6ca1\u7528\u597d\u662f\u4e2a\u707e\u96be\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4150547\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KuroNekoFan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @peterjose \u8fd9\u770b\u5177\u4f53\u5b9e\u73b0\u5427\uff0c\u5982\u679c\u968f\u4fbf json.stringify \u7136\u540e\u5bf9\u6bd4\u4e00\u4e0b\u90a3\u81ea\u7136\u4e0d\u592a\u884c\uff0c\u9ad8\u6548\u7684\u5b9e\u73b0\u81ea\u7136\u8fd8\u662f\u8981\u770b\u5177\u4f53\u7684\u573a\u666f\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>React Hooks \u6027\u80fd\u4f18\u5316\u7684&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\/192422"}],"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=192422"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/192422\/revisions"}],"predecessor-version":[{"id":192424,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/192422\/revisions\/192424"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192422"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}