{"id":292080,"date":"2019-07-15T15:01:21","date_gmt":"2019-07-15T15:01:21","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=292080"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=292080","title":{"rendered":"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 Redux-\u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f React Hooks"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/dev.to\/ramsay\/build-a-redux-like-global-store-using-react-hooks-4a7n\">&quot;Build a Redux-like Global Store Using React Hooks&quot;<\/a> \u0430\u0432\u0442\u043e\u0440\u0430 Ramsay.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435 \u043a \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043f\u043e-\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u0432\u0435\u0449\u0430\u043c. \u0415\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0432\u043a\u0440\u0430\u0442\u0446\u0435, \u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c<br \/>  \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>useReducer<\/strong> \u0438 <strong>useContext<\/strong> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0445\u0443\u043a\u0430 React, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443, \u043f\u043e\u0445\u043e\u0436\u0435\u043c\u0443 \u043d\u0430 Redux.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u042f \u043d\u0435 \u0432 \u043a\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044e, \u0447\u0442\u043e \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u044b\u043c \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043e\u043c Redux, \u043f\u043e\u0442\u043e\u043c\u0443-\u0447\u0442\u043e \u044f \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a. \u0413\u043e\u0432\u043e\u0440\u044f &quot;Redux-\u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435&quot;, \u044f \u0438\u043c\u0435\u044e \u0432\u0432\u0438\u0434\u0443 \u0442\u043e,<br \/>  \u0447\u0442\u043e \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <em>dispatch<\/em> \u0438 <em>actions<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u043c\u0443\u0442\u0430\u0446\u0438\u044e \u043d\u0430\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u043a\u043e\u043f\u0438\u044e \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0432\u0448\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.<br \/>  \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c Redux, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0442\u0432\u043e\u0440\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u043d\u0435 \u0447\u0438\u0442\u0430\u043b\u0438 \u044d\u0442\u043e\u0442 \u0430\u0431\u0437\u0430\u0446.<\/p>\n<p>  <\/p>\n<h2 id=\"huki\">\u0425\u0443\u043a\u0438<\/h2>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430(<em>\u0434\u0430\u043b\u0435\u0435<\/em> <strong>Context<\/strong>) \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0430\u0448\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435(<em>\u0434\u0430\u043b\u0435\u0435<\/em> <strong>state<\/strong>) \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438(<em>\u0434\u0430\u043b\u0435\u0435<\/em> <strong>dispatch<\/strong>). \u041c\u044b \u0442\u0430\u043a \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <strong>useStore<\/strong>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0435\u0441\u0442\u0438 \u0441\u0435\u0431\u044f, \u043a\u0430\u043a \u043d\u0430\u0448 \u0445\u0443\u043a.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ store\/useStore.js  import React, { createContext, useReducer, useContext } from &quot;react&quot;;  \/\/ \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u044b\u043c const initialState = {}  const StoreContext = createContext(initialState);  \/\/ useStore \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 React \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445 \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438 \u043c\u0443\u0442\u0430\u0446\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f export const useStore = store =&gt; {   const { state, dispatch } = useContext(StoreContext);   return { state, dispatch }; }; <\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0432\u0441\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 <a href=\"https:\/\/ru.reactjs.org\/docs\/context.html#reactcreatecontext\">React Context<\/a>, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c <a href=\"https:\/\/ru.reactjs.org\/docs\/context.html#reactcreatecontext\">Provider<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0430\u0441\u0442<br \/>  \u043d\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <strong>state<\/strong> \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <strong>dispatch<\/strong>. <strong>Provider<\/strong> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0442\u0430\u043c, \u0433\u0434\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <strong>useReducer<\/strong>.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ store\/useStore.js  ... const StoreContext = createContext(initialState);  export const StoreProvider = ({ children }) =&gt; {   const [state, dispatch] = useReducer(reducer, initialState);   return (     &lt;StoreContext.Provider value={{ state, dispatch }}&gt;       {children}     &lt;\/StoreContext.Provider&gt;   ); }; ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <strong>useReducer<\/strong>, \u0447\u0442\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <strong>state<\/strong> \u0438 <strong>dispatch<\/strong>. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u044d\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u043e, \u0447\u0442\u043e \u0438 \u0434\u0435\u043b\u0430\u0435\u0442 <a href=\"https:\/\/ru.reactjs.org\/docs\/hooks-reference.html#usereducer\">useReducer<\/a>. \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c <strong>state<\/strong> \u0438 <strong>dispatch<\/strong> \u00a0\u0432 <strong>Provider<\/strong>.<br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 React \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <strong>&lt;Provider\/&gt;<\/strong> \u0438 \u044d\u0442\u043e\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0441\u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>useStore<\/strong>, \u0447\u0442\u043e \u0431\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435\u043c.<\/p>\n<p>  <\/p>\n<p>\u041c\u044b \u0435\u0449\u0435 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u043b\u0438 <strong>reducer<\/strong>. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0448\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ store\/useStore.js ... const StoreContext = createContext(initialState);  \/\/ \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u043f\u0438\u043d\u0433\u043e\u043c actions, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0443\u0442\u0430\u0446\u0438\u0438 state const Actions = {};  \/\/ reducer \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 action \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u044e dispatch \/\/ action.type - \u044d\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 Actions \/\/ \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c update \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 state \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0435\u0433\u043e \u043d\u043e\u0432\u0443\u044e \u043a\u043e\u043f\u0438\u044e const reducer = (state, action) =&gt; {   const act = Actions[action.type];   const update = act(state);   return { ...state, ...update }; }; ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u042f \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0444\u0430\u043d\u0430\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f <em>actions<\/em> \u0438 <em>state<\/em> \u0432 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0433\u0440\u0443\u043f\u043f\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: \u0432\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430(\u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430) \u0438\u043b\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f(\u0437\u0430\u0448\u0435\u043b \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0438\u043b\u0438 \u0435\u0433\u043e \u043f\u0435\u0440\u0441\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0435\u043d\u0438\u044f).<br \/>  \u0412 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0432\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043e\u0431\u043e\u0438\u043c \u044d\u0442\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c, \u0442\u0430\u043a \u0447\u0442\u043e \u0438\u0434\u0435\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0445 \u0432 \u0435\u0434\u0438\u043d\u043e\u043c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0432\u043f\u043e\u043b\u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430\u0448\u0438 <em>actions<\/em> \u0432 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0433\u0440\u0443\u043f\u043f\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a <strong>userActions<\/strong> \u0438 <strong>countActions<\/strong>, \u0447\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u043c\u0438 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0449\u0435.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b\u044b <strong>countActions.js<\/strong> \u0438 <strong>userActions.js<\/strong> \u0432 \u043f\u0430\u043f\u043a\u0435 store.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ store\/countActions.js  export const countInitialState = {   count: 0 };  export const countActions = {   increment: state =&gt; ({ count: state.count + 1 }),   decrement: state =&gt; ({ count: state.count - 1 }) };<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ store\/userActions.js  export const userInitialState = {   user: {     loggedIn: false   } };  export const userActions = {   login: state =&gt; {     return { user: { loggedIn: true } };   },   logout: state =&gt; {     return { user: { loggedIn: false } };   } };<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u043e\u0431\u043e\u0438\u0445 \u044d\u0442\u0438\u0445 \u0444\u0430\u0439\u043b\u0430\u0445 \u043c\u044b \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c <strong>initialState<\/strong>, \u043f\u043e\u0442\u043e\u043c\u0443-\u0447\u0442\u043e \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u0442\u043e\u043c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0438\u0445 \u0432 \u0444\u0430\u0439\u043b\u0435 <strong>useStore.js<\/strong> \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 <strong>initialState<\/strong>.<br \/>  \u0422\u0430\u043a \u0436\u0435, \u043c\u044b \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 Actions, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043c\u0443\u0442\u0430\u0446\u0438\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043f\u043e\u0442\u043e\u043c\u0443-\u0447\u0442\u043e \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e \u0431\u044b \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u043e \u0432 <strong>reducer<\/strong>, \u0432 \u0444\u0430\u0439\u043b\u0435 <strong>useStore.js<\/strong>.<\/p>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u043e \u0432\u0441\u0435 \u0432 <strong>useStore.js<\/strong>, \u0447\u0442\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u043d\u0443\u044e \u043a\u0430\u0440\u0442\u0438\u043d\u0443.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ store\/useStore.js  import React, { createContext, useReducer, useContext } from &quot;react&quot;;  import { countInitialState, countActions } from &quot;.\/countActions&quot;; import { userInitialState, userActions } from &quot;.\/userActions&quot;;  \/\/ \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 (initial states) const initialState = {   ...countInitialState,   ...userInitialState };  const StoreContext = createContext(initialState);  \/\/ \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 actions const Actions = {   ...userActions,   ...countActions };  const reducer = (state, action) =&gt; {   const act = Actions[action.type];   const update = act(state);   return { ...state, ...update }; };  export const StoreProvider = ({ children }) =&gt; {   const [state, dispatch] = useReducer(reducer, initialState);   return (     &lt;StoreContext.Provider value={{ state, dispatch }}&gt;       {children}     &lt;\/StoreContext.Provider&gt;   ); };  export const useStore = store =&gt; {   const { state, dispatch } = useContext(StoreContext);   return { state, dispatch }; };<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u044b \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b\u0438! \u0421\u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u043a\u0440\u0443\u0433 \u043f\u043e\u0447\u0435\u0442\u0430, \u0430 \u043a\u043e\u0433\u0434\u0430 \u0432\u0435\u0440\u043d\u0435\u0442\u0435\u0441\u044c, \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u0441\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435.<\/p>\n<p>  <\/p>\n<p>\u0414\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e! \u042f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0432\u0430\u0448 \u043a\u0440\u0443\u0433 \u0431\u044b\u043b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0447\u0435\u0442\u043d\u044b\u043c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 <strong>useStore<\/strong> \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438.<\/p>\n<p>  <\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430\u0448 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <strong>App<\/strong> \u0432 <strong>&lt;StoreProvider\/&gt;<\/strong>.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ App.js  import React from &quot;react&quot;; import ReactDOM from &quot;react-dom&quot;; import { StoreProvider } from &quot;.\/store\/useStore&quot;; import App from &quot;.\/App&quot;;  function Main() {   return (     &lt;StoreProvider&gt;       &lt;App \/&gt;     &lt;\/StoreProvider&gt;   ); }  const rootElement = document.getElementById(&quot;root&quot;); ReactDOM.render(&lt;Main \/&gt;, rootElement);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u044b \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c <strong>App<\/strong> \u0432 <strong>StoreProvider<\/strong>, \u0447\u0442\u043e \u0431\u044b \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0438\u043c\u0435\u043b \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0438\u0437 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430(provider). \u042d\u0442\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438 <strong>state<\/strong>, \u0438 <strong>dispatch<\/strong>.<\/p>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <strong>AppHeader<\/strong> \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043a\u043d\u043e\u043f\u043a\u0430 login\/logout.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ AppHeader.jsx  import React, {useCallback} from &quot;react&quot;; import { useStore } from &quot;.\/store\/useStore&quot;;  const AppHeader = props =&gt; {   const { state, dispatch } = useStore();   const login = useCallback(() =&gt; dispatch({ type: &quot;login&quot; }), [dispatch]);   const logout = useCallback(() =&gt; dispatch({ type: &quot;logout&quot; }), [dispatch]);    const handleClick = () =&gt; {     loggedIn ? logout() : login();   }    return (     &lt;div&gt;       &lt;button onClick={handleClick}&gt; {loggedIn ? &quot;Logout&quot; : &quot;Login&quot;}&lt;\/button&gt;       &lt;span&gt;{state.user.loggedIn ? &quot;logged in&quot; : &quot;logged out&quot;}&lt;\/span&gt;       &lt;span&gt;Counter: {state.count}&lt;\/span&gt;     &lt;\/div&gt;   ); };  export default AppHeader;<\/code><\/pre>\n<p>  <\/p>\n<p><a href=\"https:\/\/codesandbox.io\/s\/xpjm9p052z?from-embed\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 Code Sandbox \u0441 \u043f\u043e\u043b\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439<\/a><\/p>\n<p>  <\/p>\n<p>\u0410\u0432\u0442\u043e\u0440 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0430: <a href=\"https:\/\/dev.to\/ramsay\">Ramsay<\/a><br \/>  <a href=\"https:\/\/dev.to\/ramsay\/build-a-redux-like-global-store-using-react-hooks-4a7n\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b<\/a><\/p>\n<\/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\/460083\/\"> https:\/\/habr.com\/ru\/post\/460083\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/dev.to\/ramsay\/build-a-redux-like-global-store-using-react-hooks-4a7n\">&quot;Build a Redux-like Global Store Using React Hooks&quot;<\/a> \u0430\u0432\u0442\u043e\u0440\u0430 Ramsay.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435 \u043a \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043f\u043e-\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u0432\u0435\u0449\u0430\u043c. \u0415\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0432\u043a\u0440\u0430\u0442\u0446\u0435, \u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c<br \/>  \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>useReducer<\/strong> \u0438 <strong>useContext<\/strong> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0445\u0443\u043a\u0430 React, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443, \u043f\u043e\u0445\u043e\u0436\u0435\u043c\u0443 \u043d\u0430 Redux.<\/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-292080","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/292080","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=292080"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/292080\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=292080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=292080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=292080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}