{"id":318086,"date":"2021-02-16T09:01:35","date_gmt":"2021-02-16T09:01:35","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=318086"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=318086","title":{"rendered":"\u0427\u0442\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c: \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438\u043b\u0438 useThis?"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440!<\/p>\n<p>\u041a\u0430\u043a \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u0441 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435, \u0443 \u043d\u0430\u0441 \u043e\u0442\u043d\u044f\u043b\u0438 \u0442\u0430\u043a\u0443\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e \u0432\u0435\u0449\u044c \u043a\u0430\u043a this, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0443 \u043d\u0430\u0441 \u0432\u043e\u0437\u043d\u0438\u043a \u0432\u043e\u043f\u0440\u043e\u0441: \u201c\u0430 \u0433\u0434\u0435 \u0436\u0435 \u0442\u043e\u0433\u0434\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c <code>timeoutId<\/code>?\u201d. \u0418 \u044f \u0432\u0438\u0434\u0435\u043b \u043a\u0430\u043a \u043b\u044e\u0434\u0438 \u043f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443 \u0432\u044b\u043a\u0440\u0443\u0447\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0438\u0437 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b (<a href=\"https:\/\/youtu.be\/AMf1tEeQfqY\" rel=\"noopener noreferrer nofollow\"><u>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u043e\u0439 \u0432\u0438\u0434\u0435\u043e<\/u><\/a>)<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 <code>timeoutId<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e <code>useEffect<\/code> \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u043e\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442:<\/p>\n<pre><code class=\"javascript\">useEffect(() =&gt; {   const timeout = setTimeout(() =&gt; {     \/\/ do some action   }, 3000);      return () =&gt; {     clearTimeout(timeout);   } }, [...]);<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0443\u0436\u0434\u0430 \u043e\u0447\u0438\u0449\u0430\u0442\u044c <code>timeout<\/code> \u043f\u043e \u043a\u043b\u0438\u043a\u0443 \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443, \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0443\u0436\u0435 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0435\u0448\u0438\u043b\u0438, \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u043d\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0438 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043d\u0435\u0439 <code>id<\/code> \u044d\u0442\u043e\u0433\u043e \u0442\u0430\u0439\u043c-\u0430\u0443\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043a\u043b\u0438\u043a\u0443 \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0435\u0433\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"javascript\">let timeout;  const Test = () =&gt; {   const onClick = () =&gt; clearTimeout(timeout);      useEffect(() =&gt; {     timeout = setTimeout(() =&gt; {       \/\/ do some action     }, 3000);   }, [...]);         return (...); }<\/code><\/pre>\n<p> \u0418 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0431\u0435\u0437 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c. \u041d\u043e \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u201c\u041d\u041e\u201d.<\/p>\n<h2>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <strong>Counter<\/strong>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0435\u0441\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0432\u043d\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"javascript\">let globalCounter = 0;  const Counter = () =&gt; {   const [stateCounter, setStateCounter] = useState(0);      const onClick = () =&gt; {     globalCounter++;     setStateCounter((stateCounter) =&gt; stateCounter + 1);   };      return (     &lt;div&gt;       &lt;p&gt;global counter - &lt;b&gt;{globalCounter}&lt;\/b&gt;&lt;\/p&gt;       &lt;p&gt;state counter - &lt;b&gt;{stateCounter}&lt;\/b&gt;&lt;\/p&gt;       &lt;button onClick={onClick}&gt;increment&lt;\/button&gt;     &lt;\/div&gt;   ); }<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442:<\/p>\n<pre><code class=\"javascript\">const App = () =&gt; {   const [countersNumber, setCountersNumber] = useState(0);      return (     &lt;div&gt;       &lt;button onClick={setCountersNumber((count) =&gt; count + 1)}&gt;         add       &lt;\/button&gt;       &lt;button onClick={setCountersNumber((count) =&gt; count - 1)}&gt;         removed       &lt;\/button&gt;       {[...Array.from(countersNumber).keys()].map((index) =&gt; (         &lt;Counter key={index} \/&gt;       ))}     &lt;\/div&gt;   ); };<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0445\u0440\u0430\u043d\u0438\u043c \u0432 <code>state<\/code> \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432, \u0438 \u043d\u0438\u0436\u0435 \u0438\u043c\u0435\u0435\u043c 2 \u043a\u043d\u043e\u043f\u043a\u0438: \u0434\u043b\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f. \u0418 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0430\u043c\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e, \u043a\u0430\u043a \u0443 \u043d\u0430\u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>countersNumber<\/code>.<\/p>\n<h4>\u0421\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0434\u0438\u043d \u0441\u0447\u0435\u0442\u0447\u0438\u043a;<\/p>\n<\/li>\n<li>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043d\u0430\u0436\u043c\u0435\u043c &#171;increment&#187; \u0442\u0440\u0438 \u0440\u0430\u0437\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/28a\/95f\/4d7\/28a95f4d7f9c320b642fd616c98a86be.jpg\" width=\"1680\" height=\"929\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435 \u0443 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u0430\u0443\u043d\u0442\u0435\u0440\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0440\u0438. \u0418 \u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u0442\u0435, \u043e\u043d\u0438 \u0432\u0441\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e. \u0418 \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u0443\u0434\u0430\u043b\u0438\u0442\u0435 \u0432\u0441\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438, \u0430 \u043f\u043e\u0442\u043e\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u0430\u0443\u043d\u0442\u0435\u0440 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d \u0442\u0440\u0435\u043c, \u0442\u0430\u043a \u043a\u0430\u043a <strong>\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0441\u0430\u0439\u0442\u0430 \u0438 \u043e\u0447\u0438\u0449\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u043b\u043d\u043e\u043c \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0438 \u0442\u0430\u0431\u044b \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0441 \u0432\u0430\u0448\u0438\u043c \u0441\u0430\u0439\u0442\u043e\u043c.<\/strong><\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 <code>timeoutId<\/code> \u0432 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u043e\u0434\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c \u0431\u0430\u0433\u0430\u043c. \u0414\u0430 \u0438 \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u0430\u043a\u0438\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u0432\u044b \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0437\u0430\u0441\u043e\u0440\u044f\u0435\u0442\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430, \u0445\u043e\u0442\u044c \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u043d\u0430 \u0441\u043f\u0438\u0447\u043a\u0430\u0445.<\/p>\n<h2>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0443<\/h2>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0445\u0443\u043a\u0430 <code>useRef()<\/code>. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442 React \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3a8\/933\/a4e\/3a8933a4e46bde5694028db92efa2918.jpg\" width=\"1671\" height=\"936\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u043d\u0438 \u043f\u0440\u044f\u043c\u043e \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b\u0438, \u0447\u0442\u043e <code>useRef()<\/code> \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0430\u043d\u0430\u043b\u043e\u0433 <code>this<\/code>. \u0418 \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0432 <code>useRef()<\/code> \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 <code>timeout<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">const Test = () =&gt; {   const timeout = useRef();      const onClick = () =&gt; clearTimeout(timeout.current);      useEffect(() =&gt; {     timeout.current = setTimeout(() =&gt; {       \/\/ do some action     }, 3000);   }, [...]);      return (...); }<\/code><\/pre>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0432\u0430\u0441 \u0441\u043c\u0443\u0449\u0430\u0435\u0442, \u0442\u043e \u0447\u0442\u043e \u0432 <code>timeout<\/code> \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>current<\/code>, \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u043d\u043e \u0443 \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0437\u0443\u043c\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u201c<a href=\"https:\/\/habr.com\/en\/post\/540442\/\" rel=\"noopener noreferrer nofollow\">createRef, setRef, useRef \u0438 \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u0435\u043d current \u0432 ref<\/a>\u201d.<\/p>\n<h2>prevProps \u043d\u0435 \u0438\u0441\u0447\u0435\u0437\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438<\/h2>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>useRef()<\/code> \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f <code>timeout<\/code> \u044d\u0442\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e. \u041d\u043e \u0435\u0441\u0442\u044c \u0438 \u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445 \u0432 \u0432\u0438\u0434\u0435 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0435\u0441\u0442\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 <code>componentDidUpdate<\/code>. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 <code>prevProps<\/code>, \u0442.\u0435. <code>props<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u0434\u0430\u0432\u0430\u043b\u043e \u043d\u0430\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c <code>props<\/code> \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 <code>props<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439. \u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.<\/p>\n<pre><code class=\"javascript\">componentDidUpdate(prevProps) {   if (this.props.id !== prevProps.id) {     \/\/ do some action   } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0443 \u0432\u0430\u0441 \u043e\u0442\u043d\u044f\u043b\u0438 \u0431\u0435\u0437\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043d\u043e, \u0442\u043e\u0433\u0434\u0430 \u0432\u044b \u043e\u0448\u0438\u0431\u0430\u0435\u0442\u0435\u0441\u044c.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0445\u0443\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code>props<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"javascript\">const useGetPrevValue = (value) =&gt; {   const prevValueRef = useRef();    useEffect(() =&gt; {     prevValueRef.current = value;   });      return prevValueRef.current; };<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c <code>value<\/code> \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c <code>ref<\/code> \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438. \u0418 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u0432\u0435\u0440\u043d\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>current<\/code> \u0440\u0430\u0432\u043d\u043e\u0435 <code>null<\/code>. \u041d\u043e \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u043c <code>current<\/code> \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 <code>ref<\/code> \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439.<\/p>\n<p>\u0418 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0445\u0443\u043a:<\/p>\n<pre><code class=\"javascript\">const CounterView = ({ counter }) =&gt; {   const prevCount = useGetPrevValue(counter);      const classes = classNames({     [styles.greenCounter]: counter &lt; prevCounter,     [styles.redCounter] counter &gt; prevCounter,   });      ... }<\/code><\/pre>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435, \u0438\u043c\u0435\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>counter<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043a\u0440\u0430\u0441\u0438\u0442\u044c \u0432 \u0437\u0435\u043b\u0435\u043d\u044b\u0439 \u0446\u0432\u0435\u0442, \u0435\u0441\u043b\u0438 <code>counter<\/code> \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b\u0441\u044f, \u0438\u043b\u0438 \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439, \u0435\u0441\u043b\u0438 <code>counter<\/code> \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u0441\u044f.<\/p>\n<h2>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0439\u0442\u0435 \u0441\u043e\u0437\u043d\u0430\u043d\u0438\u0435<\/h2>\n<p>\u041c\u044b \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b\u0438 \u043b\u0438\u0448\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>ref<\/code> \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435\u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u0434\u043b\u044f <code>ref<\/code> \u0437\u0430\u0434\u0430\u0447. \u041d\u043e \u0438\u0445 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u044b \u043e\u0442\u043a\u0440\u043e\u0435\u0442\u0435 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0434\u0435\u0441\u043a\u0442\u043e\u043f \u0432\u0435\u0440\u0441\u0438\u0438, \u0438 \u0431\u0443\u0434\u0435\u0442\u0435 \u0441\u043a\u0440\u043e\u043b\u0438\u0442\u044c \u0432\u0432\u0435\u0440\u0445, \u0442\u043e \u0443 \u0432\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u0442\u0430, \u0430 \u0435\u0441\u043b\u0438 \u0432\u043d\u0438\u0437, \u043e\u043d\u0430 \u0438\u0441\u0447\u0435\u0437\u043d\u0435\u0442. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043a\u0440\u043e\u043b\u0430 \u0432 <code>ref<\/code>, \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432 \u043a\u0430\u043a\u0443\u044e \u0436\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0441\u043a\u0440\u043e\u043b\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c58\/5a0\/182\/c585a018215380a80786140c6e57247b.jpg\" width=\"1673\" height=\"934\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0432\u044b \u0443\u0436\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0438\u0441\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>ref<\/code> \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043e \u043b\u0438\u0448\u044c \u0432\u0430\u0448\u0435\u0439 \u0444\u0430\u043d\u0442\u0430\u0437\u0438\u0435\u0439 \u0438 \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0435\u043c\u0443 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0447\u0435\u043c \u043c\u044b \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b\u0438 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u0418 \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435 \u0435\u0449\u0435 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>ref<\/code> \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/541884\/\"> https:\/\/habr.com\/ru\/post\/541884\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440!<\/p>\n<p>\u041a\u0430\u043a \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u0441 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435, \u0443 \u043d\u0430\u0441 \u043e\u0442\u043d\u044f\u043b\u0438 \u0442\u0430\u043a\u0443\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e \u0432\u0435\u0449\u044c \u043a\u0430\u043a this, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0443 \u043d\u0430\u0441 \u0432\u043e\u0437\u043d\u0438\u043a \u0432\u043e\u043f\u0440\u043e\u0441: \u201c\u0430 \u0433\u0434\u0435 \u0436\u0435 \u0442\u043e\u0433\u0434\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c <code>timeoutId<\/code>?\u201d. \u0418 \u044f \u0432\u0438\u0434\u0435\u043b \u043a\u0430\u043a \u043b\u044e\u0434\u0438 \u043f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443 \u0432\u044b\u043a\u0440\u0443\u0447\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0438\u0437 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b (<a href=\"https:\/\/youtu.be\/AMf1tEeQfqY\" rel=\"noopener noreferrer nofollow\"><u>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u043e\u0439 \u0432\u0438\u0434\u0435\u043e<\/u><\/a>)<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 <code>timeoutId<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e <code>useEffect<\/code> \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u043e\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442:<\/p>\n<pre><code class=\"javascript\">useEffect(() =&gt; {   const timeout = setTimeout(() =&gt; {     \/\/ do some action   }, 3000);      return () =&gt; {     clearTimeout(timeout);   } }, [...]);<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0443\u0436\u0434\u0430 \u043e\u0447\u0438\u0449\u0430\u0442\u044c <code>timeout<\/code> \u043f\u043e \u043a\u043b\u0438\u043a\u0443 \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443, \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0443\u0436\u0435 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0435\u0448\u0438\u043b\u0438, \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u043d\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0438 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043d\u0435\u0439 <code>id<\/code> \u044d\u0442\u043e\u0433\u043e \u0442\u0430\u0439\u043c-\u0430\u0443\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043a\u043b\u0438\u043a\u0443 \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0435\u0433\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"javascript\">let timeout;  const Test = () =&gt; {   const onClick = () =&gt; clearTimeout(timeout);      useEffect(() =&gt; {     timeout = setTimeout(() =&gt; {       \/\/ do some action     }, 3000);   }, [...]);         return (...); }<\/code><\/pre>\n<p> \u0418 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0431\u0435\u0437 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c. \u041d\u043e \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u201c\u041d\u041e\u201d.<\/p>\n<h2>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <strong>Counter<\/strong>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0435\u0441\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0432\u043d\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"javascript\">let globalCounter = 0;  const Counter = () =&gt; {   const [stateCounter, setStateCounter] = useState(0);      const onClick = () =&gt; {     globalCounter++;     setStateCounter((stateCounter) =&gt; stateCounter + 1);   };      return (     &lt;div&gt;       &lt;p&gt;global counter - &lt;b&gt;{globalCounter}&lt;\/b&gt;&lt;\/p&gt;       &lt;p&gt;state counter - &lt;b&gt;{stateCounter}&lt;\/b&gt;&lt;\/p&gt;       &lt;button onClick={onClick}&gt;increment&lt;\/button&gt;     &lt;\/div&gt;   ); }<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442:<\/p>\n<pre><code class=\"javascript\">const App = () =&gt; {   const [countersNumber, setCountersNumber] = useState(0);      return (     &lt;div&gt;       &lt;button onClick={setCountersNumber((count) =&gt; count + 1)}&gt;         add       &lt;\/button&gt;       &lt;button onClick={setCountersNumber((count) =&gt; count - 1)}&gt;         removed       &lt;\/button&gt;       {[...Array.from(countersNumber).keys()].map((index) =&gt; (         &lt;Counter key={index} \/&gt;       ))}     &lt;\/div&gt;   ); };<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0445\u0440\u0430\u043d\u0438\u043c \u0432 <code>state<\/code> \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432, \u0438 \u043d\u0438\u0436\u0435 \u0438\u043c\u0435\u0435\u043c 2 \u043a\u043d\u043e\u043f\u043a\u0438: \u0434\u043b\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f. \u0418 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0430\u043c\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e, \u043a\u0430\u043a \u0443 \u043d\u0430\u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>countersNumber<\/code>.<\/p>\n<h4>\u0421\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0434\u0438\u043d \u0441\u0447\u0435\u0442\u0447\u0438\u043a;<\/p>\n<\/li>\n<li>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043d\u0430\u0436\u043c\u0435\u043c &#171;increment&#187; \u0442\u0440\u0438 \u0440\u0430\u0437\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435 \u0443 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u0430\u0443\u043d\u0442\u0435\u0440\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0440\u0438. \u0418 \u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u0442\u0435, \u043e\u043d\u0438 \u0432\u0441\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e. \u0418 \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u0443\u0434\u0430\u043b\u0438\u0442\u0435 \u0432\u0441\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438, \u0430 \u043f\u043e\u0442\u043e\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u0430\u0443\u043d\u0442\u0435\u0440 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d \u0442\u0440\u0435\u043c, \u0442\u0430\u043a \u043a\u0430\u043a <strong>\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0441\u0430\u0439\u0442\u0430 \u0438 \u043e\u0447\u0438\u0449\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u043b\u043d\u043e\u043c \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0438 \u0442\u0430\u0431\u044b \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0441 \u0432\u0430\u0448\u0438\u043c \u0441\u0430\u0439\u0442\u043e\u043c.<\/strong><\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 <code>timeoutId<\/code> \u0432 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u043e\u0434\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c \u0431\u0430\u0433\u0430\u043c. \u0414\u0430 \u0438 \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u0430\u043a\u0438\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u0432\u044b \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0437\u0430\u0441\u043e\u0440\u044f\u0435\u0442\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430, \u0445\u043e\u0442\u044c \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u043d\u0430 \u0441\u043f\u0438\u0447\u043a\u0430\u0445.<\/p>\n<h2>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0443<\/h2>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0445\u0443\u043a\u0430 <code>useRef()<\/code>. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442 React \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u043d\u0438 \u043f\u0440\u044f\u043c\u043e \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b\u0438, \u0447\u0442\u043e <code>useRef()<\/code> \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0430\u043d\u0430\u043b\u043e\u0433 <code>this<\/code>. \u0418 \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0432 <code>useRef()<\/code> \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 <code>timeout<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">const Test = () =&gt; {   const timeout = useRef();      const onClick = () =&gt; clearTimeout(timeout.current);      useEffect(() =&gt; {     timeout.current = setTimeout(() =&gt; {       \/\/ do some action     }, 3000);   }, [...]);      return (...); }<\/code><\/pre>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0432\u0430\u0441 \u0441\u043c\u0443\u0449\u0430\u0435\u0442, \u0442\u043e \u0447\u0442\u043e \u0432 <code>timeout<\/code> \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>current<\/code>, \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u043d\u043e \u0443 \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0437\u0443\u043c\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u201c<a href=\"https:\/\/habr.com\/en\/post\/540442\/\" rel=\"noopener noreferrer nofollow\">createRef, setRef, useRef \u0438 \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u0435\u043d current \u0432 ref<\/a>\u201d.<\/p>\n<h2>prevProps \u043d\u0435 \u0438\u0441\u0447\u0435\u0437\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438<\/h2>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>useRef()<\/code> \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f <code>timeout<\/code> \u044d\u0442\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e. \u041d\u043e \u0435\u0441\u0442\u044c \u0438 \u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445 \u0432 \u0432\u0438\u0434\u0435 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0435\u0441\u0442\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 <code>componentDidUpdate<\/code>. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 <code>prevProps<\/code>, \u0442.\u0435. <code>props<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u0434\u0430\u0432\u0430\u043b\u043e \u043d\u0430\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c <code>props<\/code> \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 <code>props<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439. \u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.<\/p>\n<pre><code class=\"javascript\">componentDidUpdate(prevProps) {   if (this.props.id !== prevProps.id) {     \/\/ do some action   } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0443 \u0432\u0430\u0441 \u043e\u0442\u043d\u044f\u043b\u0438 \u0431\u0435\u0437\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043d\u043e, \u0442\u043e\u0433\u0434\u0430 \u0432\u044b \u043e\u0448\u0438\u0431\u0430\u0435\u0442\u0435\u0441\u044c.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0445\u0443\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code>props<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"javascript\">const useGetPrevValue = (value) =&gt; {   const prevValueRef = useRef();    useEffect(() =&gt; {     prevValueRef.current = value;   });      return prevValueRef.current; };<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c <code>value<\/code> \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c <code>ref<\/code> \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438. \u0418 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u0432\u0435\u0440\u043d\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>current<\/code> \u0440\u0430\u0432\u043d\u043e\u0435 <code>null<\/code>. \u041d\u043e \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u043c <code>current<\/code> \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 <code>ref<\/code> \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439.<\/p>\n<p>\u0418 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0445\u0443\u043a:<\/p>\n<pre><code class=\"javascript\">const CounterView = ({ counter }) =&gt; {   const prevCount = useGetPrevValue(counter);      const classes = classNames({     [styles.greenCounter]: counter &lt; prevCounter,     [styles.redCounter] counter &gt; prevCounter,   });      ... }<\/code><\/pre>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435, \u0438\u043c\u0435\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>counter<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043a\u0440\u0430\u0441\u0438\u0442\u044c \u0432 \u0437\u0435\u043b\u0435\u043d\u044b\u0439 \u0446\u0432\u0435\u0442, \u0435\u0441\u043b\u0438 <code>counter<\/code> \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b\u0441\u044f, \u0438\u043b\u0438 \u0432 \u043a\u0440\u0430\u0441\u043d\u044b\u0439, \u0435\u0441\u043b\u0438 <code>counter<\/code> \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u0441\u044f.<\/p>\n<h2>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0439\u0442\u0435 \u0441\u043e\u0437\u043d\u0430\u043d\u0438\u0435<\/h2>\n<p>\u041c\u044b \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b\u0438 \u043b\u0438\u0448\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>ref<\/code> \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435\u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u0434\u043b\u044f <code>ref<\/code> \u0437\u0430\u0434\u0430\u0447. \u041d\u043e \u0438\u0445 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u044b \u043e\u0442\u043a\u0440\u043e\u0435\u0442\u0435 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c \u0434\u0435\u0441\u043a\u0442\u043e\u043f \u0432\u0435\u0440\u0441\u0438\u0438, \u0438 \u0431\u0443\u0434\u0435\u0442\u0435 \u0441\u043a\u0440\u043e\u043b\u0438\u0442\u044c \u0432\u0432\u0435\u0440\u0445, \u0442\u043e \u0443 \u0432\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u0442\u0430, \u0430 \u0435\u0441\u043b\u0438 \u0432\u043d\u0438\u0437, \u043e\u043d\u0430 \u0438\u0441\u0447\u0435\u0437\u043d\u0435\u0442. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043a\u0440\u043e\u043b\u0430 \u0432 <code>ref<\/code>, \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432 \u043a\u0430\u043a\u0443\u044e \u0436\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0441\u043a\u0440\u043e\u043b\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0432\u044b \u0443\u0436\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0438\u0441\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>ref<\/code> \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043e \u043b\u0438\u0448\u044c \u0432\u0430\u0448\u0435\u0439 \u0444\u0430\u043d\u0442\u0430\u0437\u0438\u0435\u0439 \u0438 \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0435\u043c\u0443 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0447\u0435\u043c \u043c\u044b \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b\u0438 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u0418 \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435 \u0435\u0449\u0435 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>ref<\/code> \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/541884\/\"> https:\/\/habr.com\/ru\/post\/541884\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-318086","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318086"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318086\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}