{"id":293148,"date":"2019-08-08T15:01:20","date_gmt":"2019-08-08T15:01:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=293148"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=293148","title":{"rendered":"\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Context API \u0432 React \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u0442\u0435\u043c\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u041f\u0440\u0438\u0432\u0435\u0442, \u044d\u0442\u043e \u043c\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0441\u0442 \u043d\u0430 \u0425\u0430\u0431\u0440. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0412\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u044f \u0445\u043e\u0447\u0443 \u043d\u0430\u0447\u0430\u0442\u044c \u0441\u0435\u0440\u0438\u044e \u043f\u043e\u0441\u0442\u043e\u0432, \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u043b\u0438 \u043a\u043e\u0441\u0432\u0435\u043d\u043d\u043e \u043a\u0430\u0441\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f ui-kit.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/71e\/299\/09d\/71e29909dd4bd4256d77c181082df862.jpg\" alt=\"image\"><\/p>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430: \u041d\u0430\u0439\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f \u0442\u0435\u043c\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u0432 <code>React.js<\/code>. \u0411\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0435\u043c\u044b \u2014 <i>dark<\/i> \u0438 <i>light<\/i>.<\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c <code>create-react-context<\/code>, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0430\u043f\u043a\u0438 \u0432 \u043a\u043e\u0440\u043d\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 (<i>src\/<\/i>) \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c <b>theme-context<\/b>. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0430\u043f\u043a\u0438 \u0431\u0443\u0434\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=\"markdown\">theme-context\/    ThemeConsumer\/       ThemeConsumer.js       index.js     ThemeProvider\/       ThemeProvider.js       index.js   constants.js    context.js   index.js <\/code><\/pre>\n<p>  \u041b\u0438\u0447\u043d\u043e, \u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e \u0441 \u0444\u0430\u0439\u043b\u0430 <b>index.js<\/b>. \u0414\u0435\u043b\u0430\u0435\u0448\u044c \u0432\u0441\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u044b \u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u044b \u0432\u043d\u0430\u0447\u0430\u043b\u0435, \u0438 \u043f\u043e\u0442\u043e\u043c \u0443\u0436\u0435 \u0433\u043e\u043b\u043e\u0432\u0430 \u043e \u043d\u0438\u0445 \u043d\u0435 \u0431\u043e\u043b\u0438\u0442.<\/p>\n<p>  <b>theme-context\/index.js<\/b><\/p>\n<pre><code class=\"javascript\">export { ThemeProvider } from '.\/ThemeProvider'; export { ThemeConsumer } from '.\/ThemeConsumer'; <\/code><\/pre>\n<p>  <b>theme-context\/ThemeConsumer\/index.js<\/b><\/p>\n<pre><code class=\"javascript\">export { ThemeConsumer } from '.\/ThemeConsumer'; <\/code><\/pre>\n<p>  <b>theme-context\/ThemeProvider\/index.js<\/b><\/p>\n<pre><code class=\"javascript\">export { ThemeProvider } from '.\/ThemeProvider'; <\/code><\/pre>\n<p>  <b>theme-context\/context.js<\/b><\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435, \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>createContext<\/code> (\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u0435 \u0437\u0430 \u043a\u0430\u043b\u0430\u043c\u0431\u0443\u0440), \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u043e\u0434\u0443\u043b\u044c <a href=\"https:\/\/www.npmjs.com\/package\/create-react-context\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a>.<\/p>\n<pre><code class=\"javascript\">import createContext from 'create-react-context';  const { Provider, Consumer } = createContext();  export { Provider, Consumer }; <\/code><\/pre>\n<p>  \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c <code>createContext<\/code>, \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u0443\u0435\u043c \u0435\u0433\u043e \u043d\u0430 <code>Provider<\/code> \u0438 <code>Consumer<\/code>, \u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0438\u0445.<\/p>\n<p>  <b>theme-context\/constants.js<\/b><\/p>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u0430\u0448\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u0433\u0440\u044f\u0437\u043d\u044f\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b.<\/p>\n<pre><code class=\"javascript\">export const themeLight = 'light'; export const themeDark = 'dark'; export const defaultTheme = themeLight; export const themes = [themeLight, themeDark]; <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u044f \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0440\u0430\u043d\u0435\u0435, \u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0434\u0432\u0435 \u0442\u0435\u043c\u044b \u2014 \u043b\u0430\u0439\u0442 \u0438 \u0434\u0430\u0440\u043a.<\/p>\n<p>  <b>theme-context\/ThemeProvider\/ThemeProvider.js<\/b><\/p>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0435 \u2014 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 <code>React.Context<\/code>. \u041e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u043d\u0441\u044e\u043c\u0435\u0440\u0430\u043c \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.<\/p>\n<p>  \u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u043f\u0440\u043e\u043f <code>\u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430<\/code> \u2014 \u044d\u0442\u043e <code>theme<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u0432\u0441\u0435\u043c <code>\u041a\u043e\u043d\u0443\u0441\u044e\u043c\u0435\u0440\u0430\u043c<\/code>-\u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c \u0434\u0430\u043d\u043d\u043e\u0433\u043e <code>Provider<\/code>\u0430.<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { Provider } from '..\/context'; import { defaultTheme, themes } from '..\/constants';  function ThemeProvider({ theme, children }) {   return &lt;Provider value={theme}&gt;{children}&lt;\/Provider&gt;; }  export { ThemeProvider }; <\/code><\/pre>\n<p>  <b>theme-context\/ThemeConsumer\/ThemeConsumer.js<\/b><\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 Consumer \u2014 \u044d\u0442\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u00ab\u0441\u043b\u0443\u0448\u0430\u0435\u0442, \u0436\u0434\u0435\u0442\u00bb \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430. \u0414\u0438\u0442\u044f (Children) \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f. \u042d\u0442\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>Consumer<\/code>.<\/p>\n<p>  \u0414\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>React Node<\/code>, \u043f\u0440\u043e\u0449\u0435 \u0433\u043e\u0432\u043e\u0440\u044f \u2014 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442.<\/p>\n<p>  \u0418\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 {theme =&gt; \/* \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0442\u043e-\u043b\u0438\u0431\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 *\/}) \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u043e \u043f\u0440\u043e\u043f\u0441\u0443 <code>theme<\/code> \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0435\u0433\u043e, \u0432\u044b\u0448\u0435\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 <code>Provider<\/code> \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { defaultTo } from 'lodash'; import { Consumer } from '..\/context'; import { defaultTheme, themes } from '..\/constants';  function ThemeConsumer(props) {   return &lt;Consumer&gt;{theme =&gt; props.children(defaultTo(theme, props.defaultTheme))}&lt;\/Consumer&gt;; }  export { ThemeConsumer }; <\/code><\/pre>\n<p>  \u0422\u0443\u0442 \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<br \/>  \u0415\u0441\u043b\u0438 \u0442\u0435\u043c\u0430 \u043d\u0435 \u0431\u044b\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u0430 \u044d\u043a\u0441\u043f\u043b\u0438\u0446\u0438\u0442\u043d\u043e, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0442\u0435\u043c\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0431\u044b\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u0430 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0443\u0442\u0438\u043b\u044c \u0438\u0437 <code>lodash<\/code> \u2014 <code>defaultTo<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0438\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432.<\/p>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0442\u0435\u043c\u044b \u0433\u043e\u0442\u043e\u0432 \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e!<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0442\u0435\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"css\">.my-class {   font-family: sans-serif;   text-align: center;   font-size: 30px; }  .my-class-light {   color: #39cccc; }  .my-class-dark {   color: #001a33; } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"javascript\">import React from \"react\"; import ReactDOM from \"react-dom\"; import cx from \"classnames\"; import { ThemeConsumer, ThemeProvider } from \".\/theme-context\";  import \".\/styles.css\";  function MyComponent() {   const renderMyComponent = theme =&gt; {     const myComponentClassName = cx(\"my-class\", {       \"my-class-dark\": theme === \"dark\",       \"my-class-light\": theme === \"light\"     });     return (       &lt;div className={myComponentClassName}&gt;         &lt;h1&gt;\u0422\u0435\u043a\u0441\u0442 \u0432 \u0446\u0432\u0435\u0442\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0435\u043c\u044b&lt;\/h1&gt;       &lt;\/div&gt;     );   };   return &lt;ThemeConsumer&gt;{theme =&gt; renderMyComponent(theme)}&lt;\/ThemeConsumer&gt;; };  function App() {   return (       &lt;MyComponent \/&gt;   ); }  const rootElement = document.getElementById(\"root\"); ReactDOM.render( \/\/ \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0442\u0435\u043a\u0443\u0448\u0443\u044e \u0442\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0441\u043c\u0435\u043d\u0438\u0432 theme \u043d\u0430 dark &lt;ThemeProvider theme=\"light\"&gt;   &lt;App \/&gt; &lt;\/ThemeProvider&gt; , rootElement); <\/code><\/pre>\n<p>  \u0418\u0442\u0430\u043a, \u043c\u044b \u043e\u0431\u0435\u0440\u043d\u0443\u043b\u0438 \u043d\u0430\u0448 <code>&lt;App \/&gt;<\/code> \u0432 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0435, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0442\u0435\u043c\u0430 \u0441\u0442\u0430\u043b\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0432\u0441\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c-\u043a\u043e\u043d\u0441\u044e\u043c\u0435\u0440\u0430\u043c \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0414\u0430\u043b\u0435\u0435, <code>&lt;App \/&gt;<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>&lt;MyComponent \/&gt;<\/code>, \u044d\u0442\u043e <code>\u041a\u043e\u043d\u0441\u044e\u043c\u0435\u0440<\/code>, \u0438 \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u043d\u0430\u0448 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0441\u0442 \u0435\u043c\u0443 \u0442\u0435\u043c\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0410 \u0443\u0436\u0435 \u0438\u043c\u0435\u044f \u0442\u0435\u043c\u0443 \u0432 \u0432\u0438\u0434\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"javascript\">&lt;ThemeConsumer&gt;{theme =&gt; renderMyComponent(theme)}&lt;\/ThemeConsumer&gt; <\/code><\/pre>\n<p>  \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430  <\/p>\n<pre><code class=\"javascript\">  const renderMyComponent = theme =&gt; {     \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u0442\u0435\u043c\u0435 }; <\/code><\/pre>\n<p>  <a href=\"https:\/\/codesandbox.io\/s\/blazing-paper-nwfst\">\u0420\u0430\u0431\u043e\u0447\u0438\u0439 \u043a\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0443\u0442<\/a>.<\/p>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0412\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0441\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u043e\u0441\u0442\u0443 \u044f \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u044e \u0441\u043e\u0437\u0434\u0430\u0442\u044c <b>media-context<\/b>, \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0441\u043d\u0430\u0432\u0430\u043d\u0438\u0438 \u0434\u0435\u0432\u0430\u0439\u0441\u0430 \u044e\u0437\u0435\u0440\u0430.<\/p>\n<p>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e.<\/p><\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \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\/462963\/\"> https:\/\/habr.com\/ru\/post\/462963\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u041f\u0440\u0438\u0432\u0435\u0442, \u044d\u0442\u043e \u043c\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0441\u0442 \u043d\u0430 \u0425\u0430\u0431\u0440. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0412\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u044f \u0445\u043e\u0447\u0443 \u043d\u0430\u0447\u0430\u0442\u044c \u0441\u0435\u0440\u0438\u044e \u043f\u043e\u0441\u0442\u043e\u0432, \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u043b\u0438 \u043a\u043e\u0441\u0432\u0435\u043d\u043d\u043e \u043a\u0430\u0441\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f ui-kit.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/71e\/299\/09d\/71e29909dd4bd4256d77c181082df862.jpg\" alt=\"image\"><\/p>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430: \u041d\u0430\u0439\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f \u0442\u0435\u043c\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u0432 <code>React.js<\/code>. \u0411\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0435\u043c\u044b \u2014 <i>dark<\/i> \u0438 <i>light<\/i>.<\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c <code>create-react-context<\/code>, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.  <\/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-293148","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/293148","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=293148"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/293148\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=293148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=293148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=293148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}