{"id":328248,"date":"2022-01-19T09:00:38","date_gmt":"2022-01-19T09:00:38","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=328248"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=328248","title":{"rendered":"<span>React: Zustand State Manager<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/pu\/no\/zh\/punozh3zkth3cqyppf_przd5wco.png\"\/>  <\/p>\n<p>  <\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0434\u0440\u0443\u0437\u044c\u044f!<\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u0434\u043d\u044f\u0445, \u0431\u043e\u0440\u043e\u0437\u0434\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u044b \u0421\u0435\u0442\u0438 \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u0432\u0434\u043e\u0445\u043d\u043e\u0432\u0435\u043d\u0438\u044f, \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 <a href=\"https:\/\/github.com\/pmndrs\/zustand\"><code>Zustand<\/code><\/a>, \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c <code>React-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/code>, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043b\u043d\u043e (\u0441\u0440\u0435\u0434\u0438 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043c\u043d\u043e\u0433\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432) \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u043c\u043e\u0438\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c \u043e, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u043c, \u0442\u043e \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u043e\u043c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c\u0443 <code>React<\/code> state manager (\u0441\u043c., \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/habr.com\/ru\/company\/timeweb\/blog\/597109\/\">\u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e<\/a>).<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043a\u0430\u0437\u0443 \u043e \u043d\u0435\u043c \u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438, \u0437\u0430\u0442\u0435\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u2014 \u043f\u043e \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u0438, &#171;\u0437\u0430\u043f\u0438\u043b\u0438\u043c&#187; \u043f\u0440\u043e\u0441\u0442\u0443\u044e &#171;\u0442\u0443\u0434\u0443\u0448\u043a\u0443&#187;.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0435\u0441\u043e\u0447\u043d\u0438\u0446\u0430:<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<div class=\"oembed\">\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/61e6e4a36ea11f3bbbaf2164\" data-style=\"\" id=\"61e6e4a36ea11f3bbbaf2164\" width=\"\"><\/div>\n<\/div>\n<\/div><\/div>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/harryheman\/Blog-Posts\/tree\/master\/react-zustand\">\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"teoriya\">\u0422\u0435\u043e\u0440\u0438\u044f<\/h2>\n<p>  <\/p>\n<p><strong>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">yarn add zustand # or npm i zustand<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430<\/strong><\/p>\n<p>  <\/p>\n<p>\u0425\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u2014 \u044d\u0442\u043e \u0445\u0443\u043a. \u0412 \u043d\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e: \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b, \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>set<\/code> \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 (merge) \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import create from 'zustand'  const useStore = create((set) => ({  count: 0,  increment: () => set((state) => ({ count: state.count + 1 })),  decrement: () => set((state) => ({ count: state.count - 1 })),  reset: () => set({ count: 0 }) }))  export default useStore<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430<\/strong><\/p>\n<p>  <\/p>\n<p>\u0425\u0443\u043a \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f (\u0431\u0435\u0437 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430!). \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c\u0441\u044f (\u0442\u043e\u043b\u044c\u043a\u043e) \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p><em>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430<\/em><\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">export default function Counter() {  const { count, increment, decrement, reset } = useStore()   return (    &lt;main>      &lt;h2>{count}&lt;\/h2>      &lt;div className='btn-box'>        &lt;button onClick={decrement} className='btn decrement'>          -        &lt;\/button>        &lt;button onClick={increment} className='btn increment'>          +        &lt;\/button>        &lt;button onClick={reset} className='btn reset'>          0        &lt;\/button>      &lt;\/div>    &lt;\/main>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Counter<\/code> \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p><em>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u0435\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (state slices \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u043e\u043b\u043e\u0433\u0438\u0438 Redux)<\/em><\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ \u0445\u0443\u043a \u0434\u043b\u044f \"\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438\" \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0430 function useLogAfterFirstRender(componentName) {  const firstRender = useRef(true)   useEffect(() => {    firstRender.current = false  }, [])   if (!firstRender.current) {    console.log(`${componentName} render`)  } }  function Count() {  const count = useStore(({ count }) => count)   useLogAfterFirstRender('Count')   return &lt;h2>{count}&lt;\/h2> }  function DecrementBtn() {  const decrement = useStore(({ decrement }) => decrement)   useLogAfterFirstRender('Decrement')   return (    &lt;button onClick={decrement} className='btn decrement'>      -    &lt;\/button>  ) }  function IncrementBtn() {  const increment = useStore(({ increment }) => increment)   useLogAfterFirstRender('Increment')   return (    &lt;button onClick={increment} className='btn increment'>      +    &lt;\/button>  ) }  function ResetBtn() {  const reset = useStore(({ reset }) => reset)   useLogAfterFirstRender('Reset')   return (    &lt;button onClick={reset} className='btn reset'>      0    &lt;\/button>  ) }  const Counter = () => (  &lt;main>    &lt;Count \/>    &lt;div className='btn-box'>      &lt;DecrementBtn \/>      &lt;IncrementBtn \/>      &lt;ResetBtn \/>    &lt;\/div>  &lt;\/main> )  export default Counter<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Count<\/code> \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>count<\/code>.<\/p>\n<p>  <\/p>\n<p><strong>\u0420\u0435\u0446\u0435\u043f\u0442\u044b<\/strong><\/p>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">function Count() {  const count = useStore(({ count }) => count)   useLogAfterFirstRender('Count')   return &lt;h2>{count}&lt;\/h2> }  function Controls() {  const { decrement, increment, reset } = useStore(    ({ decrement, increment, reset }) => ({ decrement, increment, reset })  )   useLogAfterFirstRender('Controls')   return (    &lt;div className='btn-box'>      &lt;button onClick={decrement} className='btn decrement'>        -      &lt;\/button>      &lt;button onClick={increment} className='btn increment'>        +      &lt;\/button>      &lt;button onClick={reset} className='btn reset'>        0      &lt;\/button>    &lt;\/div>  ) }  const Counter = () => (  &lt;main>    &lt;Count \/>    &lt;Controls \/>  &lt;\/main> )  export default Counter<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>Controls<\/code> \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (\u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435, \u0430 \u043d\u0435 \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e).<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>shallow<\/code>, \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0449\u0430\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e\u0441\u0442\u0438 \u0438, \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import shallow from 'zustand\/shallow'  function Controls() {  const { decrement, increment, reset } = useStore(    ({ decrement, increment, reset }) => ({ decrement, increment, reset }),    \/* ? *\/    shallow  )   useLogAfterFirstRender('Controls')   return (    &lt;div className='btn-box'>      &lt;button onClick={decrement} className='btn decrement'>        -      &lt;\/button>      &lt;button onClick={increment} className='btn increment'>        +      &lt;\/button>      &lt;button onClick={reset} className='btn reset'>        0      &lt;\/button>    &lt;\/div>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const useStore = create((set) => ({  count: 0,  controls: {    increment: () => set(({ count }) => ({ count: count + 1 })),    decrement: () => set(({ count }) => ({ count: count - 1 })),    reset: () => set({ count: 0 })  } }))  function Controls() {  \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f `shallow` \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430  const controls = useStore(({ controls }) => controls)   useLogAfterFirstRender('Controls')   return (    &lt;div className='btn-box'>      &lt;button onClick={controls.decrement} className='btn decrement'>        -      &lt;\/button>      &lt;button onClick={controls.increment} className='btn increment'>        +      &lt;\/button>      &lt;button onClick={controls.reset} className='btn reset'>        0      &lt;\/button>    &lt;\/div>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e <code>shallow<\/code> \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const todos = useStore(  state => state.todos,  (oldTodos, newTodos) => compare(oldTodos, newTodos) )<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u041c\u0435\u043c\u043e\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u044b<\/em><\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u043e\u0432 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a <code>useCallback<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const todoById = useStore(useCallback(state => state.todos[id], [id]))<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 (scope), \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 (\u044d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u043e\u0439\/fixed reference):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const selector = state => state.todos  function TodoList() {  const todos = useStore(selector)   \/\/ ... }<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0417\u0430\u043c\u0435\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<\/em><\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <code>true<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>set<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const useStore = create(set => ({  \/\/ ...  clear: () => set({}, true) }))<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/em><\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f <code>zustand<\/code> \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043a\u0430\u043a\u043e\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0438\u043b\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>set<\/code> \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0438 \u0432 \u043d\u0443\u0436\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const useStore = create((set, get) => ({  todos: [],  loading: false,  error: null,  fetchTodos: async () => {    set({ loading: true })    try {      const response = await fetch(SERVER_URI)      if (!response.ok) throw response      set({ todos: await response.json() })    } catch (e) {      let error = e      \/\/ custom error      if (e.status === 400) {        error = await e.json()      }      set({ error })    } finally {      set({ loading: false })    }  } }))<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0427\u0442\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445<\/em><\/p>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>get<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 (\u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 <code>set<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const useStore = create((set, get) => ({  todos: [],  removeTodo(id) {    const newTodos = get().todos.filter(t => t.id !== id)    set({ todos: newTodos })  } }))<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f<\/em><\/p>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>subscribe<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c\u0441\u044f (bind) \u043a \u0447\u0430\u0441\u0442\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0431\u0435\u0437 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0430 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438. \u0414\u0430\u043d\u043d\u0443\u044e \u0442\u0435\u0445\u043d\u0438\u043a\u0443 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0445\u0443\u043a\u0435 <code>useEffect<\/code> \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u043f\u0438\u0441\u043a\u0438 (unsubscribe) \u043f\u0440\u0438 \u0440\u0430\u0437\u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const useStore = create(set => ({ count: 0, \/* ... *\/ }))  function Counter() {  \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435  const countRef = useRef(useStore.getState().count)   useEffect(() => {    \/\/ \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 \u043f\u0440\u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438,    \/\/ \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043f\u0440\u0438 \u0440\u0430\u0437\u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438    const unsubscribe = useStore.subscribe(      state => (countRef.current = state.count)    )    return () => {      unsubscribe()    }  }, []) }<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0414\u043e\u043b\u0433\u043e\u0441\u0440\u043e\u0447\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<\/em><\/p>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>persist<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u043b\u044e\u0431\u043e\u0439 \u0432\u0438\u0434 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>localStorage<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import create from 'zustand' import { persist } from 'zustand\/middleware'  const useStore = create(persist(  (set, get) => ({    todos: [],    addTodo(newTodo) {      const newTodos = [...get().todos, newTodo]      set({ todos: newTodos })    }  }, {    name: \"todos-storage\",    getStorage: () => sessionStorage  }) ))<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0436\u0438\u0442\u044c \u0431\u0435\u0437 Redux<\/em><\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const types = { incrementBy: 'INCREMENT_BY', decrementBy: 'DECREMENT_BY', reset: 'RESET' }  const reducer = (state, { type, payload }) => {  switch (type) {    case types.incrementBy: return { count: state.count + payload }    case types.decrementBy: return { count: state.count - payload }    case types.reset: return { count: 0 }    default: return state  } }  const useStore = create(set => ({  count: 0,  dispatch: action => set(state => reducer(state, action)) }))  const dispatch = useStore(state => state.dispatch) dispatch({ type: types.incrementBy, payload: 42 })<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u0441\u0440\u0435\u0434\u043d\u0438\u043a\u0430 (middleware) <code>redux<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import { redux } from 'zustand\/middleware'  const initialState = { count: 0 }  const [useStore, api] = create(redux(reducer, initialState))  const count = useStore(state => state.count) api.dispatch({ type: types.decrementBy, payload: 24 })<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430<\/em><\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u0440\u0435\u0434\u043d\u0438\u043a <code>devtools<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043a \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0443 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0435 <code>redux<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import { devtools } from 'zustand\/middleware'  \/\/ setState const useStore = create(devtools(store)) \/\/ \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u0438\u043f\u0435 \u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 const useStore = create(devtools(redux(reducer, initialState)))<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/em><\/p>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>createContext<\/code> \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0445\u0443\u043a\u0430 <code>useStore<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u043e\u043f\u0430 \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 (dependency injection) \u0438\u043b\u0438 \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u043f\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import create from 'zustand' import createContext from 'zustand\/context'  const { Provider, useStore } = createContext()  const createStore = () => create(\/* ... *\/)  const App = () => (  &lt;Provider createStore={createStore}>    {\/* ... *\/}  &lt;\/Provider> )  const Component = () => {  const state = useStore()  const stateSlice = useStore(selector)   \/\/ ... }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u0442\u044c \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u043d\u0435\u0435, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 <code>zustand<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c (\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439).<\/p>\n<p>  <\/p>\n<h2 id=\"praktika\">\u041f\u0440\u0430\u043a\u0442\u0438\u043a\u0430<\/h2>\n<p>  <\/p>\n<p>\u0421 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0435\u043d\u0438\u044f, \u044f \u0431\u0443\u0434\u0443 \u043a\u0440\u0430\u0442\u043e\u043a.<\/p>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0448\u0430\u0431\u043b\u043e\u043d <code>React-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/www.npmjs.com\/package\/create-snowpack-app\"><code>create-snowpack-app<\/code><\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">yarn create snowpack-app react-zustand --template @snowpack\/app-template-react --use-yarn --no-git # \u0438\u043b\u0438 # \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0444\u043b\u0430\u0433 `--use-yarn` \u043d\u0435 \u043d\u0443\u0436\u0435\u043d npx create-snowpack-app ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0435 \u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0443\u0434\u0443\u0448\u043a\u0438.<\/p>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c <a href=\"https:\/\/www.npmjs.com\/package\/json-server\"><code>json-server<\/code><\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">cd react-zustand yarn add json-server # \u0438\u043b\u0438 npm i json-server<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b <code>db.json<\/code> \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">{  \"todos\": [    {      \"id\": \"1\",      \"text\": \"Sleep\",      \"done\": true    },    {      \"id\": \"2\",      \"text\": \"Eat\",      \"done\": true    },    {      \"id\": \"3\",      \"text\": \"Code\",      \"done\": false    },    {      \"id\": \"4\",      \"text\": \"Repeat\",      \"done\": false    }  ] }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 <code>scripts<\/code> \u0444\u0430\u0439\u043b\u0430 <code>package.json<\/code> \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">\"server\": \"json-server -w db.json -d 1000\"<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li><code>-w | --watch<\/code> \u2014 \u0444\u0430\u0439\u043b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438;<\/li>\n<li><code>-d | --delay<\/code> \u2014 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430 \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>yarn server<\/code> \u0438\u043b\u0438 <code>npm run server<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0441\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <code>http:\/\/localhost:3000\/todos<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 <code>src<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">- components  - Loader.jsx - \u0438\u043d\u0434\u0438\u043a\u0430\u0442\u043e\u0440 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438  - Error.jsx - \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043e\u0448\u0438\u0431\u043e\u043a  - Boundary.jsx - \u043f\u0440\u0435\u0434\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435\u043b\u044c  - TodoForm.jsx - \u0444\u043e\u0440\u043c\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0443\u0434\u0443\u0448\u043a\u0438  - TodoInfo.jsx - \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430  - TodoList.jsx - \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u0443\u0434\u0443\u0448\u0435\u043a  - TodoItem.jsx - \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0442\u0443\u0434\u0443\u0448\u043a\u0438  - TodoControls.jsx - \u043f\u0430\u043d\u0435\u043b\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f  - index.js - \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439 \u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 - store  - index.js - \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 - App.css - App.jsx - index.jsx<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 (<code>store\/index.js<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import create from 'zustand'  const useStore = create((set, get) => ({  todos: [],  loading: false,  error: null,  info: {},  updateInfo() {    const todos = get().todos    const { length: total } = todos    const active = todos.filter((t) => !t.done).length    const done = total - active    const left = Math.round((active \/ total) * 100) + '%'    set({ info: { total, active, done, left } })  },  addTodo(newTodo) {    const todos = [...get().todos, newTodo]    set({ todos })  },  updateTodo(id) {    const todos = get().todos.map((t) =>      t.id === id ? { ...t, done: !t.done } : t    )    set({ todos })  },  removeTodo(id) {    const todos = get().todos.filter((t) => t.id !== id)    set({ todos })  },  completeActiveTodos() {    const todos = get().todos.map((t) => (t.done ? t : { ...t, done: true }))    set({ todos })  },  removeCompletedTodos() {    const todos = get().todos.filter((t) => !t.done)    set({ todos })  },  async fetchTodos() {    set({ loading: true })    try {      const response = await fetch(SERVER_URI)      if (!response.ok) throw response      set({ todos: await response.json() })    } catch (e) {      let error = e      \/\/ custom error      if (e.statusCode === 400) {        error = await e.json()      }      set({ error })    } finally {      set({ loading: false })    }  } }))  export default useStore<\/code><\/pre>\n<p>  <\/p>\n<p>\u0423 \u043d\u0430\u0441 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043b\u044f \u0442\u0443\u0434\u0443\u0448\u0435\u043a, \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438, \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438, \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0438 2 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 1 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u2014 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>  <\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f (<code>App.jsx<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import '.\/App.css' import React from 'react' \/\/ \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 import useStore from '.\/store' \/\/ \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b import {  Boundary,  TodoControls,  TodoForm,  TodoInfo,  TodoList } from '.\/components'  \/\/ \u043e\u0434\u043d\u0430 \u0438\u0437 \u0444\u0438\u0448\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043d\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b\u0438 \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0443\u0434\u0443\u0448\u0435\u043a \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \/\/ \u0435\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \/\/ \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 useStore.getState().fetchTodos()  const App = () => (  &lt;>    &lt;header>      &lt;h1>Zustand Todo App&lt;\/h1>    &lt;\/header>    &lt;main>      &lt;Boundary>        &lt;TodoForm \/>        &lt;TodoInfo \/>        &lt;TodoControls \/>        &lt;TodoList \/>      &lt;\/Boundary>    &lt;\/main>    &lt;footer>      &lt;p>&amp;copy; Not all rights reserved.&lt;br \/>      Sad, but true&lt;\/p>    &lt;\/footer>  &lt;\/> )  export default App<\/code><\/pre>\n<p>  <\/p>\n<p>\u0424\u043e\u0440\u043c\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0443\u0434\u0443\u0448\u043a\u0438 (<code>components\/TodoForm.jsx<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import React, { useEffect, useState } from 'react' \/\/ \u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \/\/ yarn add nanoid or \/\/ npm i nanoid import { nanoid } from 'nanoid' \/\/ \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 import useStore from '..\/store'  export const TodoForm = () => {  const [text, setText] = useState('')  const [submitDisabled, setSubmitDisabled] = useState(true)  \/* ? *\/  const addTodo = useStore(({ addTodo }) => addTodo)   useEffect(() => {    setSubmitDisabled(!text.trim())  }, [text])   const onChange = ({ target: { value } }) => {    setText(value)  }   const onSubmit = (e) => {    e.preventDefault()    if (submitDisabled) return    const newTodo = {      id: nanoid(),      text,      done: false    }    \/* ? *\/    addTodo(newTodo)    setText('')  }   return (    &lt;form className='todo-form' onSubmit={onSubmit}>      &lt;label htmlFor='text'>New todo text&lt;\/label>      &lt;div>        &lt;input          type='text'          required          value={text}          onChange={onChange}          style={            !submitDisabled ? { borderBottom: '2px solid var(--success)' } : {}          }        \/>        &lt;button className='btn-add' disabled={submitDisabled}>          Add        &lt;\/button>      &lt;\/div>    &lt;\/form>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043f\u0438\u0441\u043e\u043a \u0442\u0443\u0434\u0443\u0448\u0435\u043a (<code>components\/TodoList.jsx<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import React, { useLayoutEffect, useRef } from 'react' \/\/ \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \/\/ yarn add gsap or \/\/ npm i gsap import { gsap } from 'gsap' \/\/ \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 import useStore from '..\/store' import { TodoItem } from '.\/TodoItem'  export const TodoList = () => {  \/* ? *\/  const todos = useStore(({ todos }) => todos)  const todoListRef = useRef()  const q = gsap.utils.selector(todoListRef)   useLayoutEffect(() => {    if (todoListRef.current) {      gsap.fromTo(        q('.todo-item'),        {          x: 100,          opacity: 0        },        {          x: 0,          opacity: 1,          stagger: 1 \/ todos.length        }      )    }  }, [])   \/* ? *\/  return (    todos.length > 0 &amp;&amp; (      &lt;ul className='todo-list' ref={todoListRef}>        {todos.map((todo) => (          &lt;TodoItem key={todo.id} todo={todo} \/>        ))}      &lt;\/ul>    )  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0442\u0443\u0434\u0443\u0448\u043a\u0438 (<code>components\/TodoItem.jsx<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import React from 'react' import { gsap } from 'gsap' \/\/ \u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 import shallow from 'zustand\/shallow' \/\/ \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 import useStore from '..\/store'  export const TodoItem = ({ todo }) => {  \/* ? *\/  const { updateTodo, removeTodo } = useStore(    ({ updateTodo, removeTodo }) => ({      updateTodo,      removeTodo    }),    shallow  )   const remove = (id, target) => {    gsap.to(target, {      opacity: 0,      x: -100,      \/\/ \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0442\u0443\u0434\u0443\u0448\u043a\u0443 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438      onComplete() {        \/* ? *\/        removeTodo(id)      }    })  }   const { id, text, done } = todo   return (    &lt;li className='todo-item'>      &lt;input type='checkbox' onChange={() => {        \/* ? *\/        updateTodo(id)      }} checked={done} \/>      &lt;span        style={done ? { textDecoration: 'line-through' } : {}}        className='todo-text'      >        {text}      &lt;\/span>      &lt;button        className='btn-remove'        onClick={(e) => {          \/* ? *\/          remove(id, e.target.parentElement)        }}      >        \u2716      &lt;\/button>    &lt;\/li>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0430\u043d\u0435\u043b\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f (<code>components\/TodoControls.jsx<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import React from 'react' \/\/ \u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 import shallow from 'zustand\/shallow' \/\/ \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 import useStore from '..\/store'  export const TodoControls = () => {  \/* ? *\/  const { todos, completeActiveTodos, removeCompletedTodos } =    useStore(      ({ todos, completeActiveTodos, removeCompletedTodos }) => ({        todos,        completeActiveTodos,        removeCompletedTodos      }),      shallow    )   if (!todos.length) return null   return (    &lt;div className='todo-controls'>      &lt;button className='btn-complete' onClick={completeActiveTodos}>        Complete all todos      &lt;\/button>      &lt;button className='btn-remove' onClick={removeCompletedTodos}>        Remove completed todos      &lt;\/button>    &lt;\/div>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 (<code>components\/TodoInfo.jsx<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import React, { useEffect } from 'react' \/\/ \u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 import shallow from 'zustand\/shallow' \/\/ \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 import useStore from '..\/store'  export const TodoInfo = () => {  \/* ? *\/  const { todos, info, updateInfo } = useStore(    ({ todos, info, updateInfo }) => ({ todos, info, updateInfo }),    shallow  )   \/\/ \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0442\u0443\u0434\u0443\u0448\u0435\u043a  useEffect(() => {    \/* ? *\/    updateInfo()  }, [todos])   if (!info || !todos.length) return null   return (    &lt;div className='todo-info'>      {['Total', 'Active', 'Done', 'Left'].map((k) => (        &lt;span key={k}>          {k}: {info[k.toLowerCase()]}        &lt;\/span>      ))}    &lt;\/div>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u0440\u0435\u0434\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435\u043b\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import React from 'react' \/\/ \u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 import shallow from 'zustand\/shallow' \/\/ \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 import useStore from '..\/store' \/\/ \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b import { Error } from '.\/Error' import { Loader } from '.\/Loader'  export const Boundary = ({ children }) => {  \/* ? *\/  const { loading, error } = useStore(    ({ loading, error }) => ({ loading, error }),    shallow  )   \/* ? *\/  \/\/ yarn add react-loader-spinner  if (loading) return &lt;Loader width={50} \/>   \/* ? *\/  if (error) return &lt;Error error={error} \/>   return &lt;>{children}&lt;\/> }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u044b <code>yarn start<\/code> \u0438\u043b\u0438 <code>npm start<\/code> \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<p>  <img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/lq\/74\/p-\/lq74p-tzitpeycg4wvbpcvnthrc.png\"\/>  <\/p>\n<p>  <\/p>\n<p>  <img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/hm\/hc\/id\/hmhcid_if6nohrec7zxgtth4ado.png\"\/>  <\/p>\n<p>  <\/p>\n<p>  <img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/sr\/td\/ua\/srtduauqsvvdffeumkrdibtv6vq.png\"\/>  <\/p>\n<p>  <\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u0430\u043a \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u043d\u0430\u0441\u0447\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u2014 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u0432\u044b. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c.<\/p>\n<p>  <\/p>\n<p>\u0420\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c \u0444\u0430\u0439\u043b <code>components\/TodoControls.jsx<\/code> \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ ... import { nanoid } from 'nanoid'  export const TodoControls = () => {  const {    \/\/ ...    addTodo,    updateTodo  } = useStore(    ({      \/\/ ...      addTodo,      updateTodo    }) => ({      \/\/ ...      addTodo,      updateTodo    }),    shallow  )   \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f 2500 \u0442\u0443\u0434\u0443\u0448\u0435\u043a  const createManyTodos = () => {    const times = []    for (let i = 0; i &lt; 25; i++) {      const start = performance.now()      for (let j = 0; j &lt; 100; j++) {        const id = nanoid()        const todo = {          id,          text: `Todo ${id}`,          done: false        }        addTodo(todo)      }      const difference = performance.now() - start      times.push(difference)    }    const time = Math.round(times.reduce((a, c) => (a += c), 0) \/ 25)    console.log('Create time:', time)  }   \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0442\u0443\u0434\u0443\u0448\u0435\u043a  const updateAllTodos = () => {    const todos = useStore.getState().todos    const start = performance.now()    for (let i = 0; i &lt; todos.length; i++) {      updateTodo(todos[i].id)    }    const time = Math.round(performance.now() - start)    console.log('Update time:', time)  }   \/\/ if (!todos.length) return null   return (    &lt;div className='todo-controls'>      {\/* ... *\/}      &lt;button className='btn-create' onClick={createManyTodos}>        Create 2500 todos      &lt;\/button>      &lt;button className='btn-update' onClick={updateAllTodos}>        Update all todos      &lt;\/button>    &lt;\/div>  ) }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 <code>App.jsx<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ useStore.getState().fetchTodos()<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u0436\u0438\u043c\u0430\u0435\u043c \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 <code>Create 2500 todos<\/code>:<\/p>\n<p>  <img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/yn\/zm\/sq\/ynzmsq_6swwasektg589xldv6yo.png\"\/>  <\/p>\n<p>  <\/p>\n<p>  <\/p>\n<p>\u0412\u0440\u0435\u043c\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f 2500 \u0442\u0443\u0434\u0443\u0448\u0435\u043a \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 6-7 \u043c\u0441.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430\u0436\u0438\u043c\u0430\u0435\u043c <code>Update all todos<\/code>:<\/p>\n<p>  <img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/e8\/ti\/p6\/e8tip651kkazth1gjjmonvipmr4.png\"\/>  <\/p>\n<p>  <\/p>\n<p>  <\/p>\n<p>\u0412\u0440\u0435\u043c\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f 2500 \u0442\u0443\u0434\u0443\u0448\u0435\u043a \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 1100-1200 \u043c\u0441.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u043b\u0438\u0437\u043a\u0438 \u043a \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c &#171;\u0447\u0438\u0441\u0442\u043e\u0433\u043e&#187; <code>React<\/code> \u2014 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u043a\u0438 <code>useContext\/useReducer<\/code>, \u0438 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u044f\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 <code>Redux<\/code> \u0432 \u043b\u0438\u0446\u0435 <code>Redux Toolkit<\/code> (\u0441\u043c. <a href=\"https:\/\/habr.com\/ru\/company\/timeweb\/blog\/572248\/\">\u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e<\/a>).<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, <code>zustand<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0430\u0448\u0435\u0433\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f. \u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u0438\u0439 \u0434\u0435\u043d\u044c \u044d\u0442\u043e \u043b\u0443\u0447\u0448\u0438\u0439 \u0438\u0437 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 <code>React-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0436\u0430\u043b\u0443\u0439, \u044d\u0442\u043e \u0432\u0441\u0435, \u0447\u0435\u043c \u044f \u0445\u043e\u0442\u0435\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441 \u0432\u0430\u043c\u0438 \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>  <\/p>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0438 happy coding!<\/p>\n<p>  <\/p>\n<hr\/>\n<p>  <\/p>\n<p><a href=\"https:\/\/cloud.timeweb.com\/?utm_source=habr&amp;utm_medium=banner&amp;utm_campaign=cloud&amp;utm_content=direct&amp;utm_term=low\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/wn\/cq\/lp\/wncqlp9abeml4npwzsybuvhzcta.png\"\/><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><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\/company\/timeweb\/blog\/646339\/\"> https:\/\/habr.com\/ru\/company\/timeweb\/blog\/646339\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/pu\/no\/zh\/punozh3zkth3cqyppf_przd5wco.png\"\/>  <\/p>\n<p>  <\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0434\u0440\u0443\u0437\u044c\u044f!<\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u0434\u043d\u044f\u0445, \u0431\u043e\u0440\u043e\u0437\u0434\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u044b \u0421\u0435\u0442\u0438 \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u0432\u0434\u043e\u0445\u043d\u043e\u0432\u0435\u043d\u0438\u044f, \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 <a href=\"https:\/\/github.com\/pmndrs\/zustand\"><code>Zustand<\/code><\/a>, \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c <code>React-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/code>, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043b\u043d\u043e (\u0441\u0440\u0435\u0434\u0438 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043c\u043d\u043e\u0433\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432) \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u043c\u043e\u0438\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c \u043e, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u043c, \u0442\u043e \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u043e\u043c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c\u0443 <code>React<\/code> state manager (\u0441\u043c., \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/habr.com\/ru\/company\/timeweb\/blog\/597109\/\">\u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e<\/a>).<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043a\u0430\u0437\u0443 \u043e \u043d\u0435\u043c \u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438, \u0437\u0430\u0442\u0435\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u2014 \u043f\u043e \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u0438, &#171;\u0437\u0430\u043f\u0438\u043b\u0438\u043c&#187; \u043f\u0440\u043e\u0441\u0442\u0443\u044e &#171;\u0442\u0443\u0434\u0443\u0448\u043a\u0443&#187;.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0435\u0441\u043e\u0447\u043d\u0438\u0446\u0430:<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<div class=\"oembed\">\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/61e6e4a36ea11f3bbbaf2164\" data-style=\"\" id=\"61e6e4a36ea11f3bbbaf2164\" width=\"\"><\/div>\n<\/div>\n<\/div><\/div>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/harryheman\/Blog-Posts\/tree\/master\/react-zustand\">\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442.<\/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-328248","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/328248","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=328248"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/328248\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}