{"id":311657,"date":"2020-10-18T21:00:43","date_gmt":"2020-10-18T21:00:43","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=311657"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=311657","title":{"rendered":"\u041a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c React Context"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/522896\/\">\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/507572\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/a>, \u044f \u043f\u0438\u0441\u0430\u043b \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 (React Context) \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0443 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 &#8212; \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0435\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 (Context Consumer), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0432\u0430\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0443\u043f\u0440\u043e\u0441\u0442\u044f\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438\/\u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a.<\/p>\n<blockquote>\n<p>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/habr.com\/ru\/post\/507572\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/a>. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u041e\u0434\u043d\u0430\u043a\u043e, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u043c \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b, \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u043d\u0438\u0442\u0435 \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u041d\u0415 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u043c\u0438, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u0435\u0442 (\u0438 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0434\u043e\u043b\u0436\u0435\u043d) \u0431\u044b\u0442\u044c \u0447\u0430\u0441\u0442\u044c\u044e \u043a\u0430\u043a\u043e\u0439 \u043b\u0438\u0431\u043e \u0432\u0435\u0442\u043a\u0438 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<\/blockquote>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435, \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>src\/count-context.js<\/code> \u0438 \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u0442\u0430\u043c \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.js import React from 'react'  const CountStateContext = React.createContext() const CountDispatchContext = React.createContext()<\/code><\/pre>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0432 <code>CountStateContext<\/code> \u043d\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a: <code>React.createContext ({count: 0})<\/code>, \u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u043d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430. \u0414\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>defaultValue<\/code> \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u0430\u043a\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"javascript\">function CountDisplay() {   const {count} = React.useContext(CountStateContext) \/\/ &lt;-   return &lt;div&gt;{count}&lt;\/div&gt; }  ReactDOM.render(&lt;CountDisplay \/&gt;, document.getElementById('\u269b\ufe0f'))<\/code><\/pre>\n<p>\u0418\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0443 <code>CountStateContext<\/code> \u043d\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0432 <code>useContext<\/code> \u042d\u0442\u043e \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u043d\u0430\u0448\u0435 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e, \u043e\u043d\u043e <code>undefined<\/code> \u0430 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c <code>undefined<\/code> \u0432 <code>useContext<\/code>.<\/p>\n<p>\u041d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u043d\u0440\u0430\u0432\u044f\u0442\u0441\u044f runtime-\u043e\u0448\u0438\u0431\u043a\u0438, \u0442\u0430\u043a \u0447\u0442\u043e, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0432\u0430\u0448\u0435\u0439 \u043f\u0435\u0440\u0432\u043e\u0439 \u0440\u0435\u0430\u043a\u0446\u0438\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041d\u043e \u0437\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0435\u0433\u043e \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f? \u0415\u0441\u043b\u0438 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0442\u043e \u0432 \u043d\u0435\u043c \u0435\u0434\u0432\u0430 \u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u043b\u0438\u0431\u043e \u0441\u043c\u044b\u0441\u043b. \u0412 \u043f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u043c \u0447\u0438\u0441\u043b\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u0441\u0432\u043e\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0438 (<code>Context Consumer<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <code>useContext<\/code>, \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u043b\u0438\u0441\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 (<code>Context Provider<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<blockquote>\n<p>\u0417\u0430\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u0434\u0430, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043a\u043e\u0433\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u044b, \u043d\u043e \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043e\u043d\u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b.<\/p>\n<\/blockquote>\n<p>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f <a href=\"https:\/\/ru.reactjs.org\/docs\/context.html#reactcreatecontext\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0430\u043a\u0442\u0430<\/a> \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 &#171;\u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0432 \u0438\u0437\u043e\u043b\u044f\u0446\u0438\u0438 \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445&#187;. \u0414\u0430, \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e, \u043d\u043e, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043b\u0443\u0447\u0448\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043d\u0443\u0436\u043d\u044b\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c. \u041f\u043e\u043c\u043d\u0438\u0442\u0435 &#8212; \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u044b \u0432 \u0442\u0435\u0441\u0442\u0435 \u043d\u0430 \u0441\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u043e\u0432 \u0435\u0441\u043b\u0438 \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0432 \u043d\u0435\u043c \u0447\u0442\u043e-\u0442\u043e, \u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0432 \u0441\u0430\u043c\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c <a href=\"https:\/\/kentcdodds.com\/blog\/the-merits-of-mocking\" rel=\"noopener noreferrer nofollow\">The Merits of Mocking<\/a>, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439.<\/p>\n<blockquote>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 Flow \u0438\u043b\u0438 TypeScript, \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0435 <code>React.useContext<\/code>, \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u0434\u0440\u0430\u0436\u0430\u0442\u044c. \u041d\u0438\u0436\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<\/blockquote>\n<p><strong>\u0422\u0430\u043a \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u0435\u043d \u044d\u0442\u043e\u0442 <\/strong><code>CountDispatchContext<\/code><strong>?<\/strong> \u0423\u0436\u0435 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u044f \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438, \u044f \u0442\u0430\u043a \u0436\u0435 \u043e\u0431\u0449\u0430\u044e\u0441\u044c \u0441\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c\u0438 \u0438\u0437 Facebook \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442 \u0441 \u043d\u0438\u043c\u0438 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c \u044f, \u0438 \u043c\u043e\u0433\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0442\u043e \u0441\u0430\u043c\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0432\u0435\u0449\u044c \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438 (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 <code>dispatch<\/code> \u0432 \u044d\u0444\u0444\u0435\u043a\u0442\u0430\u0445) \u044d\u0442\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (<code>state<\/code>) \u0438 <code>dispatch<\/code> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435. \u042d\u0442\u043e \u0437\u0432\u0443\u0447\u0438\u0442 \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u043d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u044f \u0432\u0441\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u044e!<\/p>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043c\u043e\u0434\u0443\u043b\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u043b, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c&nbsp;<code>Provider<\/code>&nbsp;\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">function App() {   return (     &lt;CountProvider&gt;       &lt;CountDisplay \/&gt;       &lt;Counter \/&gt;     &lt;\/CountProvider&gt;   ) }  ReactDOM.render(&lt;App \/&gt;, document.getElementById('\u269b\ufe0f'))<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.js import React from 'react'  const CountStateContext = React.createContext() const CountDispatchContext = React.createContext()  function countReducer(state, action) {   switch (action.type) {     case 'increment': {       return {count: state.count + 1}     }     case 'decrement': {       return {count: state.count - 1}     }     default: {       throw new Error(`Unhandled action type: ${action.type}`)     }   } }  function CountProvider({children}) {   const [state, dispatch] = React.useReducer(countReducer, {count: 0})   return (     &lt;CountStateContext.Provider value={state}&gt;       &lt;CountDispatchContext.Provider value={dispatch}&gt;         {children}       &lt;\/CountDispatchContext.Provider&gt;     &lt;\/CountStateContext.Provider&gt;   ) }  export {CountProvider}<\/code><\/pre>\n<blockquote>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0430\u0434\u0443\u043c\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440. \u042f \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043b \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u044b\u0439 \u043a \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u0434.&nbsp;<strong>\u042d\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0447\u0438\u0442 \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c.<\/strong>&nbsp;\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0439\u0442\u0435 \u0445\u0443\u043a&nbsp;<code>useState<\/code>&nbsp;\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0432\u0430\u0448\u0435\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0432\u0430\u0448\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u043c\u0438, \u043a\u0430\u043a \u044d\u0442\u043e\u0442, \u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u0431\u0443\u0434\u0443\u0442 \u041a\u0423\u0414\u0410 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438, \u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0445\u0443\u043a\u043e\u0432.<\/p>\n<\/blockquote>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0445\u0443\u043a \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c (Consumer Hook)<\/h2>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">import React from 'react' import {SomethingContext} from 'some-context-package'  function YourComponent() {   const something = React.useContext(SomethingContext) }<\/code><\/pre>\n<p>\u041d\u043e \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0443\u0434\u0430 \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:<\/p>\n<pre><code class=\"javascript\">import React from 'react' import {useSomething} from 'some-context-package'  function YourComponent() {   const something = useSomething() }<\/code><\/pre>\n<p>\u0427\u0435\u043c \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434 \u043b\u0443\u0447\u0448\u0435? \u041d\u0443, \u044d\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043c \u0446\u0435\u043b\u044b\u0439 \u0440\u044f\u0434 \u043d\u043e\u0432\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.js import React from 'react'  const CountStateContext = React.createContext() const CountDispatchContext = React.createContext()  function countReducer(state, action) {   switch (action.type) {     case 'increment': {       return {count: state.count + 1}     }     case 'decrement': {       return {count: state.count - 1}     }     default: {       throw new Error(`Unhandled action type: ${action.type}`)     }   } }  function CountProvider({children}) {   const [state, dispatch] = React.useReducer(countReducer, {count: 0})   return (     &lt;CountStateContext.Provider value={state}&gt;       &lt;CountDispatchContext.Provider value={dispatch}&gt;         {children}       &lt;\/CountDispatchContext.Provider&gt;     &lt;\/CountStateContext.Provider&gt;   ) }  \/\/ \u043d\u0430\u0448\u0438 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0445\u0443\u043a\u0438:  function useCountState() {   const context = React.useContext(CountStateContext)   if (context === undefined) {     throw new Error('useCountState must be used within a CountProvider')   }   return context }  function useCountDispatch() {   const context = React.useContext(CountDispatchContext)   if (context === undefined) {     throw new Error('useCountDispatch must be used within a CountProvider')   }   return context }  export {CountProvider, useCountState, useCountDispatch}<\/code><\/pre>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0445\u0443\u043a\u0438&nbsp;<code>useCountState<\/code>&nbsp;\u0438&nbsp;<code>useCountDispatch<\/code>&nbsp;\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442&nbsp;<code>React.useContext<\/code>&nbsp;\u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438\u0437 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0435\u0433\u043e&nbsp;<code>CountProvider<\/code>. \u0412 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043a\u043e\u0433\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0442, \u043c\u044b \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u0445\u0443\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0432\u043d\u0443\u0442\u0440\u0438&nbsp;<code>CountProvider<\/code>. \u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430, \u0442\u043e, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u044d\u0442\u0443 \u043e\u0448\u0438\u0431\u043a\u0443 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c.<\/p>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438, \u0442\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0440\u0430\u0437\u0434\u0435\u043b. \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 React &lt; 16.8.0, \u0438\u043b\u0438, \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435, \u0432\u043e\u0442 \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u043e\u0434\u0445\u043e\u0434 render-prop:<\/p>\n<pre><code class=\"javascript\">function CountConsumer({children}) {   return (     &lt;CountStateContext.Consumer&gt;       {context =&gt; {         if (context === undefined) {           throw new Error('CountConsumer must be used within a CountProvider')         }         return children(context)       }}     &lt;\/CountStateContext.Consumer&gt;   ) }<\/code><\/pre>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u044f \u0438 \u0434\u0435\u043b\u0430\u043b \u0434\u043e \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0445\u0443\u043a\u0438. \u042d\u0442\u043e \u043d\u0435 \u043f\u043b\u043e\u0445\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434. \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0445\u0443\u043a\u0438.<\/p>\n<h2>TypeScript \/ Flow<\/h2>\n<p>\u0412\u044b\u0448\u0435 \u044f \u043e\u0431\u0435\u0449\u0430\u043b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u0442\u0435\u043c \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0441\u0442\u0430\u043b\u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (<code>defaultValue<\/code>) \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 TypeScript \u0438\u043b\u0438 Flow. \u0412\u043e\u0442 \u043e\u043d\u043e:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.tsx import * as React from 'react'  type Action = {type: 'increment'} | {type: 'decrement'} type Dispatch = (action: Action) =&gt; void type State = {count: number} type CountProviderProps = {children: React.ReactNode}  const CountStateContext = React.createContext&lt;State | undefined&gt;(undefined) const CountDispatchContext = React.createContext&lt;Dispatch | undefined&gt;(   undefined, )  function countReducer(state: State, action: Action) {   switch (action.type) {     case 'increment': {       return {count: state.count + 1}     }     case 'decrement': {       return {count: state.count - 1}     }     default: {       throw new Error(`Unhandled action type: ${action.type}`)     }   } }  function CountProvider({children}: CountProviderProps) {   const [state, dispatch] = React.useReducer(countReducer, {count: 0})   return (     &lt;CountStateContext.Provider value={state}&gt;       &lt;CountDispatchContext.Provider value={dispatch}&gt;         {children}       &lt;\/CountDispatchContext.Provider&gt;     &lt;\/CountStateContext.Provider&gt;   ) }  function useCountState() {   const context = React.useContext(CountStateContext)   if (context === undefined) {     throw new Error('useCountState must be used within a CountProvider')   }   return context }  function useCountDispatch() {   const context = React.useContext(CountDispatchContext)   if (context === undefined) {     throw new Error('useCountDispatch must be used within a CountProvider')   }   return context }  export {CountProvider, useCountState, useCountDispatch}<\/code><\/pre>\n<blockquote>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c&nbsp;<code>useCountState<\/code>&nbsp;\u0438\u043b\u0438&nbsp;<code>useCountDispatch<\/code>&nbsp;\u0431\u0435\u0437 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430&nbsp;<code>undefined<\/code>, \u0442\u0430\u043a \u043c\u044b \u0443\u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u043b\u0438 \u044d\u0442\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443!<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/codesandbox.io\/s\/bitter-night-i5mhj\" rel=\"noopener noreferrer nofollow\">\u0412\u043e\u0442 \u0432\u0435\u0440\u0441\u0438\u044f \u043d\u0430 codesandbox<\/a><\/p>\n<h2>\u0427\u0442\u043e \u043d\u0430 \u0441\u0447\u0435\u0442&nbsp;type&nbsp;\u0442\u0438\u043f\u043e\u0432 \u0432&nbsp;dispatch?<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0420\u0435\u0434\u0430\u043a\u0441\u0430 \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0437\u0430\u043a\u0440\u0438\u0447\u0430\u0442\u044c: &#171;\u042d\u0439, \u0430 \u0433\u0434\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u044d\u043a\u0448\u0435\u043d\u043e\u0432?!&#187; (Action Creator). \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0442\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u044d\u043a\u0448\u0435\u043d\u043e\u0432, \u0432 \u044d\u0442\u043e\u043c \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u043f\u043b\u043e\u0445\u043e\u0433\u043e. \u041d\u043e \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u044d\u0442\u043e \u043b\u0438\u0448\u043d\u044f\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435, \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 TypeScript \u0438\u043b\u0438 Flow, \u0438 \u0432\u044b \u0442\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u043b\u0438 \u0442\u0438\u043f\u044b \u0434\u043b\u044f \u0432\u0430\u0448\u0438\u0445 \u044d\u043a\u0448\u0435\u043d\u043e\u0432, \u0442\u043e \u0432\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u044d\u043a\u0448\u0435\u043d\u043e\u0432. \u0412\u044b \u0443\u0436\u0435 \u0438 \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0430\u0432\u0442\u043e\u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043e\u043a \u0442\u0438\u043f\u043e\u0432 \u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0435 \u043a\u043e\u0434\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8ff\/58e\/a3b\/8ff58ea3ba3cc4f5e8169dad8d2b1861.png\" width=\"1680\" height=\"420\"><figcaption><\/figcaption><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d3c\/e38\/146\/d3ce38146788c4ffd34cb953af23e1e3.png\" width=\"1720\" height=\"388\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u043d\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c&nbsp;<code>dispatch<\/code>&nbsp;\u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438, \u043a\u0430\u043a \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043b\u044e\u0441,&nbsp;<code>dispatch<\/code>&nbsp;\u0441\u0442\u0430\u0431\u0438\u043b\u0435\u043d \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u043a\u0430 \u0436\u0438\u0432\u0435\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u043b \u0435\u0433\u043e, \u0442\u0430\u043a \u0447\u0442\u043e \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043c\u0435\u043b\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0432 \u043b\u0438\u0441\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439&nbsp;<code>useEffect<\/code>&nbsp;(\u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u043e\u043d \u0442\u0443\u0434\u0430 \u0438\u043b\u0438 \u043d\u0435\u0442).<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0442\u0438\u043f\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0435 \u0441\u0432\u043e\u0439 JavaScript \u043a\u043e\u0434 (\u0432\u043e\u043e\u0431\u0449\u0435, \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c), \u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043f\u0440\u0438 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u043c \u0442\u0438\u043f\u0435 \u044d\u043a\u0448\u0435\u043d\u0430 &#8212; \u043e\u0442\u043a\u0430\u0437\u043e\u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u0430.<\/p>\n<h2>\u0427\u0442\u043e \u043d\u0430 \u0441\u0447\u0435\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438?<\/h2>\n<p>\u042d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u043e\u043f\u0440\u043e\u0441. \u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c (dispatch) \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0445\u043e\u0434\u0435 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430? \u0414\u0430, \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0441\u0430\u043c\u043e\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435, \u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0432\u0441\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/p>\n<p>\u042f \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f, \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c&nbsp;<code>dispatch<\/code>&nbsp;\u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043b\u044e\u0431\u044b\u043c\u0438 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u044b. \u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0438\u0437 \u043c\u043e\u0435\u0433\u043e \u043a\u0443\u0440\u0441\u0430&nbsp;<a href=\"https:\/\/kentcdodds.com\/workshops\/advanced-react-patterns\" rel=\"noopener noreferrer nofollow\">Advanced React Patterns Workshop<\/a>):<\/p>\n<pre><code class=\"javascript\">\/\/ user-context.js async function updateUser(dispatch, user, updates) {   dispatch({type: 'start update', updates})   try {     const updatedUser = await userClient.updateUser(user, updates)     dispatch({type: 'finish update', updatedUser})   } catch (error) {     dispatch({type: 'fail update', error})   } }  export {UserProvider, useUserDispatch, useUserState, updateUser}<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"javascript\">\/\/ user-profile.js import {useUserState, useUserDispatch, updateUser} from '.\/user-context'  function UserSettings() {   const {user, status, error} = useUserState()   const userDispatch = useUserDispatch()   function handleSubmit(event) {     event.preventDefault()     updateUser(userDispatch, user, formState)   }    \/\/ more code... }<\/code><\/pre>\n<h2>\u0421\u043e\u0432\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (state) \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 (dispatch)<\/h2>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0438\u0437\u043b\u0438\u0448\u043d\u0438\u043c:<\/p>\n<pre><code class=\"javascript\">const state = useCountState() const dispatch = useCountDispatch()<\/code><\/pre>\n<p>\u041e\u043d\u0438 \u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044e\u0442, &#171;\u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a?&#187;:<\/p>\n<pre><code class=\"javascript\">const [state, dispatch] = useCount()<\/code><\/pre>\n<p>\u0414\u0430, \u043c\u043e\u0436\u043d\u043e:<\/p>\n<pre><code class=\"javascript\">function useCount() {   return [useCountState(), useCountDispatch()] }<\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.js import React from 'react'  const CountStateContext = React.createContext() const CountDispatchContext = React.createContext()  function countReducer(state, action) {   switch (action.type) {     case 'increment': {       return {count: state.count + 1}     }     case 'decrement': {       return {count: state.count - 1}     }     default: {       throw new Error(`Unhandled action type: ${action.type}`)     }   } }  function CountProvider({children}) {   const [state, dispatch] = React.useReducer(countReducer, {count: 0})   return (     &lt;CountStateContext.Provider value={state}&gt;       &lt;CountDispatchContext.Provider value={dispatch}&gt;         {children}       &lt;\/CountDispatchContext.Provider&gt;     &lt;\/CountStateContext.Provider&gt;   ) }  function useCountState() {   const context = React.useContext(CountStateContext)   if (context === undefined) {     throw new Error('useCountState must be used within a CountProvider')   }   return context }  function useCountDispatch() {   const context = React.useContext(CountDispatchContext)   if (context === undefined) {     throw new Error('useCountDispatch must be used within a CountProvider')   }   return context }  export {CountProvider, useCountState, useCountDispatch}<\/code><\/pre>\n<p><a href=\"https:\/\/codesandbox.io\/s\/react-codesandbox-je6cc\" rel=\"noopener noreferrer nofollow\">\u0412\u043e\u0442 \u043a\u043e\u0434 \u043d\u0430 codesandbox<\/a><\/p>\n<p>\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435 \u0447\u0442\u043e \u044f \u041d\u0415 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u044e&nbsp;<code>CountContext<\/code>. \u042d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c \u0435\u0433\u043e. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043e, \u044d\u0442\u043e \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0443\u0442\u0438\u043b\u0438\u0442\u044b \u0434\u043b\u044f \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0435\u0439.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0432\u0430\u0441! \u041f\u043e\u043c\u043d\u0438\u0442\u0435:<\/p>\n<ol>\n<li>\n<p>\u041d\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0445\u0432\u0430\u0442\u0430\u0442\u044c\u0441\u044f \u0437\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u043f\u0440\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043b\u044e\u0431\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0439 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u043a \u043b\u044e\u0431\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 (\u0438 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0434\u043e\u043b\u0436\u043d\u044b) \u0438\u043c\u0435\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u0432 \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438.<\/p>\n<\/li>\n<\/ol>\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\/522896\/\"> https:\/\/habr.com\/ru\/post\/522896\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/522896\/\">\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/507572\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/a>, \u044f \u043f\u0438\u0441\u0430\u043b \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 (React Context) \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0443 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 &#8212; \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0435\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 (Context Consumer), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0432\u0430\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0443\u043f\u0440\u043e\u0441\u0442\u044f\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438\/\u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a.<\/p>\n<blockquote>\n<p>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/habr.com\/ru\/post\/507572\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/a>. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u041e\u0434\u043d\u0430\u043a\u043e, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u043c \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b, \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u043d\u0438\u0442\u0435 \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u041d\u0415 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u043c\u0438, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043c\u043e\u0436\u0435\u0442 (\u0438 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0434\u043e\u043b\u0436\u0435\u043d) \u0431\u044b\u0442\u044c \u0447\u0430\u0441\u0442\u044c\u044e \u043a\u0430\u043a\u043e\u0439 \u043b\u0438\u0431\u043e \u0432\u0435\u0442\u043a\u0438 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<\/blockquote>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435, \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>src\/count-context.js<\/code> \u0438 \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u0442\u0430\u043c \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.js import React from 'react'  const CountStateContext = React.createContext() const CountDispatchContext = React.createContext()<\/code><\/pre>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0432 <code>CountStateContext<\/code> \u043d\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a: <code>React.createContext ({count: 0})<\/code>, \u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u043d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430. \u0414\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>defaultValue<\/code> \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u0430\u043a\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"javascript\">function CountDisplay() {   const {count} = React.useContext(CountStateContext) \/\/ &lt;-   return &lt;div&gt;{count}&lt;\/div&gt; }  ReactDOM.render(&lt;CountDisplay \/&gt;, document.getElementById('\u269b\ufe0f'))<\/code><\/pre>\n<p>\u0418\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0443 <code>CountStateContext<\/code> \u043d\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0432 <code>useContext<\/code> \u042d\u0442\u043e \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u043d\u0430\u0448\u0435 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e, \u043e\u043d\u043e <code>undefined<\/code> \u0430 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c <code>undefined<\/code> \u0432 <code>useContext<\/code>.<\/p>\n<p>\u041d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u043d\u0440\u0430\u0432\u044f\u0442\u0441\u044f runtime-\u043e\u0448\u0438\u0431\u043a\u0438, \u0442\u0430\u043a \u0447\u0442\u043e, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0432\u0430\u0448\u0435\u0439 \u043f\u0435\u0440\u0432\u043e\u0439 \u0440\u0435\u0430\u043a\u0446\u0438\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041d\u043e \u0437\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0435\u0433\u043e \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f? \u0415\u0441\u043b\u0438 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0442\u043e \u0432 \u043d\u0435\u043c \u0435\u0434\u0432\u0430 \u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u043b\u0438\u0431\u043e \u0441\u043c\u044b\u0441\u043b. \u0412 \u043f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u043c \u0447\u0438\u0441\u043b\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u0441\u0432\u043e\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0438 (<code>Context Consumer<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <code>useContext<\/code>, \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u043b\u0438\u0441\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 (<code>Context Provider<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<blockquote>\n<p>\u0417\u0430\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u0434\u0430, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043a\u043e\u0433\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u044b, \u043d\u043e \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043e\u043d\u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b.<\/p>\n<\/blockquote>\n<p>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f <a href=\"https:\/\/ru.reactjs.org\/docs\/context.html#reactcreatecontext\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0430\u043a\u0442\u0430<\/a> \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 &#171;\u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0432 \u0438\u0437\u043e\u043b\u044f\u0446\u0438\u0438 \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445&#187;. \u0414\u0430, \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e, \u043d\u043e, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043b\u0443\u0447\u0448\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043d\u0443\u0436\u043d\u044b\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c. \u041f\u043e\u043c\u043d\u0438\u0442\u0435 &#8212; \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u044b \u0432 \u0442\u0435\u0441\u0442\u0435 \u043d\u0430 \u0441\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u043e\u0432 \u0435\u0441\u043b\u0438 \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0432 \u043d\u0435\u043c \u0447\u0442\u043e-\u0442\u043e, \u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0432 \u0441\u0430\u043c\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c <a href=\"https:\/\/kentcdodds.com\/blog\/the-merits-of-mocking\" rel=\"noopener noreferrer nofollow\">The Merits of Mocking<\/a>, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439.<\/p>\n<blockquote>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 Flow \u0438\u043b\u0438 TypeScript, \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0435 <code>React.useContext<\/code>, \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u0434\u0440\u0430\u0436\u0430\u0442\u044c. \u041d\u0438\u0436\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<\/blockquote>\n<p><strong>\u0422\u0430\u043a \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u0435\u043d \u044d\u0442\u043e\u0442 <\/strong><code>CountDispatchContext<\/code><strong>?<\/strong> \u0423\u0436\u0435 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u044f \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438, \u044f \u0442\u0430\u043a \u0436\u0435 \u043e\u0431\u0449\u0430\u044e\u0441\u044c \u0441\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c\u0438 \u0438\u0437 Facebook \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442 \u0441 \u043d\u0438\u043c\u0438 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c \u044f, \u0438 \u043c\u043e\u0433\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0442\u043e \u0441\u0430\u043c\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0432\u0435\u0449\u044c \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438 (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 <code>dispatch<\/code> \u0432 \u044d\u0444\u0444\u0435\u043a\u0442\u0430\u0445) \u044d\u0442\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (<code>state<\/code>) \u0438 <code>dispatch<\/code> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435. \u042d\u0442\u043e \u0437\u0432\u0443\u0447\u0438\u0442 \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u043d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u044f \u0432\u0441\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u044e!<\/p>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043c\u043e\u0434\u0443\u043b\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u043b, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c&nbsp;<code>Provider<\/code>&nbsp;\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">function App() {   return (     &lt;CountProvider&gt;       &lt;CountDisplay \/&gt;       &lt;Counter \/&gt;     &lt;\/CountProvider&gt;   ) }  ReactDOM.render(&lt;App \/&gt;, document.getElementById('\u269b\ufe0f'))<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.js import React from 'react'  const CountStateContext = React.createContext() const CountDispatchContext = React.createContext()  function countReducer(state, action) {   switch (action.type) {     case 'increment': {       return {count: state.count + 1}     }     case 'decrement': {       return {count: state.count - 1}     }     default: {       throw new Error(`Unhandled action type: ${action.type}`)     }   } }  function CountProvider({children}) {   const [state, dispatch] = React.useReducer(countReducer, {count: 0})   return (     &lt;CountStateContext.Provider value={state}&gt;       &lt;CountDispatchContext.Provider value={dispatch}&gt;         {children}       &lt;\/CountDispatchContext.Provider&gt;     &lt;\/CountStateContext.Provider&gt;   ) }  export {CountProvider}<\/code><\/pre>\n<blockquote>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0430\u0434\u0443\u043c\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440. \u042f \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043b \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u044b\u0439 \u043a \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u0434.&nbsp;<strong>\u042d\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0447\u0438\u0442 \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c.<\/strong>&nbsp;\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0439\u0442\u0435 \u0445\u0443\u043a&nbsp;<code>useState<\/code>&nbsp;\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0432\u0430\u0448\u0435\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0432\u0430\u0448\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u043c\u0438, \u043a\u0430\u043a \u044d\u0442\u043e\u0442, \u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u0431\u0443\u0434\u0443\u0442 \u041a\u0423\u0414\u0410 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438, \u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0445\u0443\u043a\u043e\u0432.<\/p>\n<\/blockquote>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0445\u0443\u043a \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c (Consumer Hook)<\/h2>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u044b \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">import React from 'react' import {SomethingContext} from 'some-context-package'  function YourComponent() {   const something = React.useContext(SomethingContext) }<\/code><\/pre>\n<p>\u041d\u043e \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0443\u0434\u0430 \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:<\/p>\n<pre><code class=\"javascript\">import React from 'react' import {useSomething} from 'some-context-package'  function YourComponent() {   const something = useSomething() }<\/code><\/pre>\n<p>\u0427\u0435\u043c \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434 \u043b\u0443\u0447\u0448\u0435? \u041d\u0443, \u044d\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043c \u0446\u0435\u043b\u044b\u0439 \u0440\u044f\u0434 \u043d\u043e\u0432\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.js import React from 'react'  const CountStateContext = React.createContext() const CountDispatchContext = React.createContext()  function countReducer(state, action) {   switch (action.type) {     case 'increment': {       return {count: state.count + 1}     }     case 'decrement': {       return {count: state.count - 1}     }     default: {       throw new Error(`Unhandled action type: ${action.type}`)     }   } }  function CountProvider({children}) {   const [state, dispatch] = React.useReducer(countReducer, {count: 0})   return (     &lt;CountStateContext.Provider value={state}&gt;       &lt;CountDispatchContext.Provider value={dispatch}&gt;         {children}       &lt;\/CountDispatchContext.Provider&gt;     &lt;\/CountStateContext.Provider&gt;   ) }  \/\/ \u043d\u0430\u0448\u0438 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0445\u0443\u043a\u0438:  function useCountState() {   const context = React.useContext(CountStateContext)   if (context === undefined) {     throw new Error('useCountState must be used within a CountProvider')   }   return context }  function useCountDispatch() {   const context = React.useContext(CountDispatchContext)   if (context === undefined) {     throw new Error('useCountDispatch must be used within a CountProvider')   }   return context }  export {CountProvider, useCountState, useCountDispatch}<\/code><\/pre>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0445\u0443\u043a\u0438&nbsp;<code>useCountState<\/code>&nbsp;\u0438&nbsp;<code>useCountDispatch<\/code>&nbsp;\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442&nbsp;<code>React.useContext<\/code>&nbsp;\u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438\u0437 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0435\u0433\u043e&nbsp;<code>CountProvider<\/code>. \u0412 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043a\u043e\u0433\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0442, \u043c\u044b \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u0445\u0443\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0432\u043d\u0443\u0442\u0440\u0438&nbsp;<code>CountProvider<\/code>. \u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430, \u0442\u043e, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u044d\u0442\u0443 \u043e\u0448\u0438\u0431\u043a\u0443 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c.<\/p>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438, \u0442\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0440\u0430\u0437\u0434\u0435\u043b. \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 React &lt; 16.8.0, \u0438\u043b\u0438, \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435, \u0432\u043e\u0442 \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u043e\u0434\u0445\u043e\u0434 render-prop:<\/p>\n<pre><code class=\"javascript\">function CountConsumer({children}) {   return (     &lt;CountStateContext.Consumer&gt;       {context =&gt; {         if (context === undefined) {           throw new Error('CountConsumer must be used within a CountProvider')         }         return children(context)       }}     &lt;\/CountStateContext.Consumer&gt;   ) }<\/code><\/pre>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u044f \u0438 \u0434\u0435\u043b\u0430\u043b \u0434\u043e \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0445\u0443\u043a\u0438. \u042d\u0442\u043e \u043d\u0435 \u043f\u043b\u043e\u0445\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434. \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0445\u0443\u043a\u0438.<\/p>\n<h2>TypeScript \/ Flow<\/h2>\n<p>\u0412\u044b\u0448\u0435 \u044f \u043e\u0431\u0435\u0449\u0430\u043b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u0442\u0435\u043c \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0441\u0442\u0430\u043b\u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (<code>defaultValue<\/code>) \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 TypeScript \u0438\u043b\u0438 Flow. \u0412\u043e\u0442 \u043e\u043d\u043e:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/count-context.tsx import * as React from 'react'  type Action = {type: 'increment'} | {type: 'decrement'} type Dispatch = (action: Action) =&gt; void type State = {count: number} type CountProviderProps = {children: React.ReactNode}  const CountStateContext = React.createContext&lt;State | undefined&gt;(undefined) const CountDispatchContext = React.createContext&lt;Dispatch | undefined&gt;(   undefined, )  function countReducer(state: State, action: Action) {   switch (action.type) {     case 'increment': {       return {count: state.count + 1}     }     case 'decrement': {       return {count: state.count - 1}     }     default: {       throw new Error(`Unhandled action type: ${action.type}`)     }   } }  function CountProvider({children}: CountProviderProps) {   const [state, dispatch] = React.useReducer(countReducer, {count: 0})   return (     &lt;CountStateContext.Provider value={state}&gt;       &lt;CountDispatchContext.Provider value={dispatch}&gt;         {children}       &lt;\/CountDispatchContext.Provider&gt;     &lt;\/CountStateContext.Provider&gt;   ) }  function useCountState() {   const context = React.useContext(CountStateContext)   if (context === undefined) {     throw new Error('useCountState must be used within a CountProvider')   }   return context }  function useCountDispatch() {   const context = React.useContext(CountDispatchContext)   if (context === undefined) {     throw new Error('useCountDispatch must be used within a CountProvider')   }   return context }  export {CountProvider, useCountState, useCountDispatch}<\/code><\/pre>\n<blockquote>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c&nbsp;<code>useCountState<\/code>&nbsp;\u0438\u043b\u0438&nbsp;<code>useCountDispatch<\/code>&nbsp;\u0431\u0435\u0437 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430&nbsp;<code>undefined<\/code>, \u0442\u0430\u043a \u043c\u044b \u0443\u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u043b\u0438 \u044d\u0442\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443!<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/codesandbox.io\/s\/bitter-night-i5mhj\" rel=\"noopener noreferrer nofollow\">\u0412\u043e\u0442 \u0432\u0435\u0440\u0441\u0438\u044f \u043d\u0430 codesandbox<\/a><\/p>\n<h2>\u0427\u0442\u043e \u043d\u0430 \u0441\u0447\u0435\u0442&nbsp;type&nbsp;\u0442\u0438\u043f\u043e\u0432 \u0432&nbsp;dispatch?<\/h2>\n<p>\u0412<\/p>\n<\/div>\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-311657","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311657","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=311657"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311657\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=311657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=311657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=311657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}