{"id":5866,"date":"2024-02-10T11:44:51","date_gmt":"2024-02-10T02:44:51","guid":{"rendered":"https:\/\/www.searchlight8.com\/?p=5866"},"modified":"2024-02-10T12:06:46","modified_gmt":"2024-02-10T03:06:46","slug":"react-usereducer-usecontext","status":"publish","type":"post","link":"https:\/\/www.searchlight8.com\/wp-searchlight8\/react-usereducer-usecontext\/","title":{"rendered":"[React] useReducer + useContext"},"content":{"rendered":"\n<p>\u4eca\u56de\u306fReact\u306eHook\u30922\u3064\u7d44\u307f\u5408\u308f\u305b\u3066\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u7d44\u3093\u3067\u307f\u307e\u3059\u3002\u89aa\u30fb\u5b50\u30fb\u5b6b\u306e\uff13\u3064\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u4f5c\u308a\u89aa\u3068\u5b6b\u3067\u540c\u3058\u72b6\u614b\u3092\u30ad\u30fc\u30d7\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u89aa\u3068\u5b6b\u3067\u305d\u308c\u305e\u308c\u306b\u30dc\u30bf\u30f3\uff08\u52a0\u7b97\u30fb\u6e1b\u7b97\u30fb\u30ea\u30bb\u30c3\u30c8\uff09\u3092\u4f5c\u308a\u30ab\u30a6\u30f3\u30c8\u6570\u3092\u5171\u6709\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f5c\u6210\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u51fa\u529b\u30a4\u30e1\u30fc\u30b8<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"410\" src=\"https:\/\/www.searchlight8.com\/wp\/wp-content\/uploads\/2024\/02\/2024-02-10_11h16_58.png\" alt=\"\" class=\"wp-image-5878\" style=\"width:521px;height:auto\" srcset=\"https:\/\/www.searchlight8.com\/wp-content\/uploads\/2024\/02\/2024-02-10_11h16_58.png 683w, https:\/\/www.searchlight8.com\/wp-content\/uploads\/2024\/02\/2024-02-10_11h16_58-600x360.png 600w, https:\/\/www.searchlight8.com\/wp-content\/uploads\/2024\/02\/2024-02-10_11h16_58-480x288.png 480w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/figure>\n\n\n\n<p>\u89aa\u306e\u30dc\u30bf\u30f3\u3001\u5b6b\u306e\u30dc\u30bf\u30f3\u3069\u3061\u3089\u3092\u62bc\u3057\u3066\u3082\u3001\u89aa\u3068\u5b6b\u306b\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u30ab\u30a6\u30f3\u30c8\u6570\uff08\u73fe\u5728\u5024\uff09\u304c\u540c\u6642\u306b\u5909\u5316\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">useContext\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb<\/h4>\n\n\n\n<p>\u307e\u305a\u300csrc\/contexts\u300d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306bAppContext.js\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u308a\u307e\u3059\u3002useContext\u3092\u4f7f\u3046\u305f\u3081\u306e\u57fa\u672c\u7684\u306a\u8a18\u8ff0\u3092\u3057\u307e\u3059\u3002createContext\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3001context\u3092AppContext\u3068\u3044\u3046\u5909\u6570\u306b\u5165\u308c\u307e\u3059\u3002\u305d\u308c\u3092\u4ed6\u3067\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:js decode:true \" title=\"\/src\/contexts\/AppContext.js\">import {createContext} from 'react'\n \nconst AppContext = createContext()\nexport default AppContext<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">App.js\uff08\u89aa\uff09\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<p>\u6b21\u306b\u89aa\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308bApp.js\u306b\u4e0b\u8a18\u306e\u30d7\u30e9\u30b0\u30e9\u30e0\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>AppContext\u3068useRecucer\u305d\u3057\u3066B\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:js decode:true \" title=\"src\/App.js\">import '.\/App.css';\nimport AppContext from '.\/contexts\/AppContext';\nimport B from '.\/components\/B';\nimport { useReducer } from 'react'\n\nconst initialState = 0\nconst reducer = (prev, action) =&gt; {\n  switch (action) {\n    case 'plus':\n      return prev + 1\n    case 'minus':\n      return prev - 1\n    case 'reset':\n      return 0\n    default:\n      return prev\n  }\n}\n\nfunction App() {\n  const [count, dispatch] = useReducer(reducer, initialState)\n  return (\n    &lt;div className=\"App\"&gt;\n          &lt;h3&gt;A&lt;\/h3&gt; \n      &lt;p&gt;\u30ab\u30a6\u30f3\u30c8\u6570: {count}&lt;\/p&gt;\n      &lt;AppContext.Provider value={{ countContext: count, dispatchContext: dispatch }} &gt;\n\n        &lt;button onClick={() =&gt; dispatch('plus')}&gt;\u30d7\u30e9\u30b9&lt;\/button&gt;\n        &lt;button onClick={() =&gt; dispatch('minus')}&gt;\u30de\u30a4\u30ca\u30b9&lt;\/button&gt;\n        &lt;B \/&gt;\n      &lt;\/AppContext.Provider&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;\n<\/pre><\/div>\n\n\n\n<p>useReducer\u306e\u521d\u671f\u5024\u30920\u3001\u305d\u3057\u3066reducer\u306e\u51e6\u7406\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>function App() {\uff5d\u306e\u4e2d\u306buseReducer\u306e\u5f0f\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u91cd\u8981\u306a\u306e\u306f<\/p>\n\n\n\n<p>&lt;AppContext.Provider value={{ countContext: count, dispatchContext: dispatch }} &gt;&lt;\/AppContext.Provider&gt;<\/p>\n\n\n\n<p>\u3067\u56f2\u307e\u308c\u305f\u4e2d\u3067useContext\u304c\u4f7f\u3048\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u3053\u306e\u4e2d\u306b&lt;B \/&gt;\u304c\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001B\u306b\u542b\u307e\u308c\u308bC\u30e2\u30b8\u30e5\u30fc\u30eb\uff08\u5b6b\uff09\u3067\u3082useContext\u304c\u4f7f\u3048\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>useContext , useRecucer\u306e\u8a73\u3057\u3044\u8aac\u660e\u306f\u4e0b\u8a18\u306e\u30ea\u30f3\u30af\u3092\u53c2\u8003\u306b\u3057\u3066\u4e0b\u3055\u3044\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-search-light-python-react-django-php-linux wp-block-embed-search-light-python-react-django-php-linux\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.searchlight8.com\/react-usecontext\/\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-search-light-python-react-django-php-linux wp-block-embed-search-light-python-react-django-php-linux\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.searchlight8.com\/react-usereducer\/\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">B\uff08\u5b50\uff09\u30e2\u30b8\u30e5\u30fc\u30eb<\/h4>\n\n\n\n<p>B\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306fC\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3044\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u8868\u793a\u3057\u305f\u3068\u304d\u306bABC\u306e\u533a\u5225\u304c\u4ed8\u304d\u3084\u3059\u3044\u3088\u3046\u306b&lt;hr&gt;\u533a\u5207\u308a\u7dda\u3092\u5165\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"wrap-toggle:false lang:js decode:true \" title=\"src\/components\/B.js\">import C from '.\/C';\n\nimport React from 'react'\n\nconst B = () =&gt; {\n  return (\n    &lt;div&gt;\n        &lt;hr&gt;&lt;\/hr&gt;\n        &lt;h3&gt;B&lt;\/h3&gt;\n        &lt;hr&gt;&lt;\/hr&gt;\n      &lt;C \/&gt;\n    &lt;\/div&gt;\n  )\n}\n\nexport default B\n<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">C\uff08\u5b6b\uff09\u30e2\u30b8\u30e5\u30fc\u30eb<\/h4>\n\n\n\n<p>\u307e\u305a\u3001useContext , AppContext\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u3053\u306e\uff12\u3064\u306f\u8a18\u8ff0\u65b9\u6cd5\u304c\u9055\u3046\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:js decode:true \" title=\"src\/components\/C.js\">import React from 'react'\nimport {useContext} from 'react'\nimport AppContext from '..\/contexts\/AppContext'\n\nconst C = () =&gt; {\n    const{countContext ,dispatchContext} = useContext(AppContext)\n  return (\n    &lt;div&gt;\n         &lt;h3&gt;C\uff08\u5b6b\uff09&lt;\/h3&gt; \n         &lt;button onClick ={()=&gt;dispatchContext('plus')}&gt;\u30d7\u30e9\u30b9&lt;\/button&gt;\n         &lt;button onClick ={()=&gt;dispatchContext('plus')}&gt;\u30de\u30a4\u30ca\u30b9&lt;\/button&gt;\n         &lt;button onClick ={()=&gt;dispatchContext('reset')}&gt;\u30ea\u30bb\u30c3\u30c8&lt;\/button&gt;\n         \u73fe\u5728\u5024 : {countContext}\n    &lt;\/div&gt;\n  )\n}\n\nexport default C\n<\/pre><\/div>\n\n\n\n<p>\u89aa\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067useContext\u3067\u4f7f\u3046\u5909\u6570\u3092\uff12\u3064\u5b9a\u7fa9\u3057\u307e\u3057\u305f\u3002&nbsp;<\/p>\n\n\n\n<p> &lt;AppContext.Provider value={{ countContext: count, dispatchContext: dispatch }} &gt;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>countContext: count<\/li>\n\n\n\n<li>dispatchContext: dispatch<\/li>\n<\/ul>\n\n\n\n<p>\u3053\u306e\u4e00\u884c\u3067useContext\u306e\u5024\u3092C\u30e2\u30b8\u30e5\u30fc\u30eb\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002const{countContext ,dispatchContext} = useContext(AppContext)<\/p>\n\n\n\n<p>\u3042\u3068\u306f\u30dc\u30bf\u30f3\u3084\u8868\u793a\u306b\u3053\u308c\u3089\u306e\u5909\u6570\u3092\u4f7f\u3048\u3070\u826f\u3044\u3060\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u300c\u30dc\u30bf\u30f3\u300d<\/p>\n\n\n\n<p>&lt;button onClick ={()=&gt;dispatchContext(&#8216;plus&#8217;)}&gt;\u30d7\u30e9\u30b9&lt;\/button&gt;<\/p>\n\n\n\n<p>\u300c\u30ab\u30a6\u30f3\u30bf\u30fc\u6570\u306e\u8868\u793a\u300d<\/p>\n\n\n\n<p>\u73fe\u5728\u5024 : {countContext}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89aa\u30fb\u5b50\u30fb\u5b6b\u3067\u540c\u6642\u306b\u5024\u3092\u5909\u66f4<\/h3>\n\n\n\n<p>\u3053\u308c\u307e\u3067\u306f\u89aa\uff08APP\uff09\u3068\u5b6b\uff08C\uff09\u3060\u3051\u3067\u30b9\u30c6\u30fc\u30c8\u3092\u5171\u6709\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u4eca\u5ea6\u306f\u5b50\uff08B)\u3067\u3082\u5171\u6709\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002APP\u30fbB\u30fbC\u305d\u308c\u305e\u308c\u306b\u30dc\u30bf\u30f3\u3092\u8a2d\u7f6e\u3057\u3001\u30ab\u30a6\u30f3\u30c8\u6570\u3092\u305d\u308c\u305e\u308c\u3067\u5171\u6709\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4e0a\u8a18\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u3067\u304d\u3066\u3044\u308c\u3070\u3068\u3066\u3082\u7c21\u5358\u3002\u30b3\u30d4\u30da\u3067\u52d5\u304b\u305b\u308b\u30ec\u30d9\u30eb\u3067\u3059\u3002\u5909\u66f4\u3059\u308b\u306e\u306fB\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:js decode:true \" title=\"src\/components\/B.js\">import C from '.\/C';\nimport {useContext} from 'react'\nimport AppContext from '..\/contexts\/AppContext'\n\nimport React from 'react'\n\nconst B = () =&gt; {\n  const{countContext ,dispatchContext} = useContext(AppContext)\n  return (\n    &lt;div&gt;\n        &lt;hr&gt;&lt;\/hr&gt;\n        &lt;h3&gt;B\uff08\u5b50\uff09&lt;\/h3&gt;\n        &lt;button onClick ={()=&gt;dispatchContext('plus')}&gt;\u30d7\u30e9\u30b9&lt;\/button&gt;\n         &lt;button onClick ={()=&gt;dispatchContext('plus')}&gt;\u30de\u30a4\u30ca\u30b9&lt;\/button&gt;\n         &lt;button onClick ={()=&gt;dispatchContext('reset')}&gt;\u30ea\u30bb\u30c3\u30c8&lt;\/button&gt;\n         \u73fe\u5728\u5024 : {countContext}\n        &lt;hr&gt;&lt;\/hr&gt;\n      &lt;C \/&gt;\n    &lt;\/div&gt;\n  )\n}\n\nexport default B\n<\/pre><\/div>\n\n\n\n<p>\u3053\u308c\u3067\uff13\u3064\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u5024\u3092\u5171\u6709\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"457\" src=\"https:\/\/www.searchlight8.com\/wp\/wp-content\/uploads\/2024\/02\/2024-02-10_11h57_48-1.png\" alt=\"\" class=\"wp-image-5884\" srcset=\"https:\/\/www.searchlight8.com\/wp-content\/uploads\/2024\/02\/2024-02-10_11h57_48-1.png 802w, https:\/\/www.searchlight8.com\/wp-content\/uploads\/2024\/02\/2024-02-10_11h57_48-1-600x342.png 600w, https:\/\/www.searchlight8.com\/wp-content\/uploads\/2024\/02\/2024-02-10_11h57_48-1-768x438.png 768w, https:\/\/www.searchlight8.com\/wp-content\/uploads\/2024\/02\/2024-02-10_11h57_48-1-480x274.png 480w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/figure>\n\n\n\n<p>useRecucer\u3068useContext\u3092\u4f7f\u3046\u3068\u30d0\u30b1\u30c4\u30ea\u30ec\u30fc\u304c\u7121\u304f\u306a\u308b\u306e\u3067\u3068\u3066\u3082\u826f\u3044\u3067\u3059\u306d\u3002jQuery\u6642\u4ee3\u306b\u4f5c\u3063\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u30d0\u30b1\u30c4\u30ea\u30ec\u30fc\u3060\u3089\u3051\u3067\u9014\u4e2d\u3067\u5acc\u306b\u306a\u308a\u307e\u3057\u305f\u3002useRecucer\u3068useContext\u306e\u7d44\u307f\u5408\u308f\u305b\u6280\u306fReactHooks\u306e\u51c4\u3055\u3092\u4f53\u611f\u3067\u304d\u308b\u826f\u3044\u4f8b\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306fReact\u306eHook\u30922\u3064\u7d44\u307f\u5408\u308f&#8230;<\/p>\n","protected":false},"author":1,"featured_media":4570,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[323],"tags":[324,447,448],"class_list":["post-5866","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","tag-react","tag-usecontext","tag-usereucer","wpcat-323-id"],"_links":{"self":[{"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/posts\/5866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/comments?post=5866"}],"version-history":[{"count":15,"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/posts\/5866\/revisions"}],"predecessor-version":[{"id":5886,"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/posts\/5866\/revisions\/5886"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/media\/4570"}],"wp:attachment":[{"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/media?parent=5866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/categories?post=5866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.searchlight8.com\/wp-searchlight8\/wp-json\/wp\/v2\/tags?post=5866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}