{"id":13155,"date":"2021-02-08T18:14:40","date_gmt":"2021-02-08T09:14:40","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13155"},"modified":"2023-05-27T22:16:21","modified_gmt":"2023-05-27T13:16:21","slug":"%e3%80%90react%e3%80%91%e3%80%8credux%e3%80%8d%e3%81%ae%e3%80%8creducer%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13155","title":{"rendered":"\u3010Redux\u3011\u300cReducer\u300d\u3001\u300cAction\u300d\u306e\u5b9f\u88c5\u65b9\u6cd5"},"content":{"rendered":"<h2>Reducer\u3068\u306f\uff1f<\/h2>\n<p>Store\u306b\u5909\u5316\u3092\u52a0\u3048\u308b\u305f\u3081\u306b\u5fc5\u8981\u306aRedux\u306e\u8981\u7d20\u3067\u3059\u3002state\u3068action\u306e\u4e8c\u3064\u306e\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308a\u307e\u3059\u3002<\/p>\n<h3>Reducer\u306e\u4f5c\u6210\u624b\u9806<\/h3>\n<h4>1.redux\u30e9\u30a4\u30d6\u30e9\u30ea\u304b\u3089combineReducers\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u3002<\/h4>\n<pre class=\"lang:default decode:true\">import { combineReducers } from 'redux';<\/pre>\n<p class=\"p1\">\u8907\u6570\u306e<span class=\"s1\">Reducer<\/span>\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u3066\u4e00\u62ec\u3067\u72b6\u614b\u7ba1\u7406\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306e\u95a2\u6570\u3067\u3059\u3002<\/p>\n<h4>2.\u500b\u5225\u306eReducer\u3092\u5ba3\u8a00\u3059\u308b\u3002<\/h4>\n<p>Redux\u3067\u306fStore\u306b\u5909\u5316\u3092\u52a0\u3048\u308b\u65b9\u6cd5\u3092\u4e8b\u524d\u306bReducer\u306eAction\u306e\u30bf\u30a4\u30d7\u3068\u3057\u3066\u5ba3\u8a00\u3057\u3066\u304a\u304d\u3001\u6975\u3081\u3066\u9650\u5b9a\u3055\u305b\u308b\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">const initialState = { value: 0 }\r\nexport function test(state=initialState, action){\r\n    switch(action.type) {\r\n      case \"A\":\r\n        return state + 1;\r\n      case \"B\":\r\n        return state + 2;\r\n      default:\r\n        return state;\r\n    }\r\n}<\/pre>\n<h5>test<\/h5>\n<p>Reducer\u540d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h5>state = initialState<\/h5>\n<p>Reducer(\u4e0a\u8a18\u30b5\u30f3\u30d7\u30eb\u3067\u8a00\u3048\u3070\u300ctest\u300d)\u306e\u69cb\u9020\u3067\u3059\u3002Reducer\u521d\u671f\u5024\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002Store\u306e\u4e2d\u306btest\u3068\u3044\u3046\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u3042\u308a\u305d\u306e\u30d7\u30ed\u30d1\u30c6\u30a3 \u3068\u3057\u3066value\u3068\u3044\u3046\u5024\u3092\u6301\u3063\u3066\u3044\u308b\u3068\u8aad\u307f\u53d6\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h5>action<\/h5>\n<p>Store\u306b\u5909\u5316\u3092\u52a0\u3048\u308b\u305f\u3081\u306eDispatch\u3055\u308c\u305fAction\u3067\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u3067\u306fA\u3068\u304bB\u3068\u304b\u66f8\u3044\u3066\u3044\u307e\u3059\u304c\u3001\u4f8b\u3048\u3070TODO\u30ea\u30b9\u30c8\u3078\u306e\u8ffd\u52a0\u30a2\u30af\u30b7\u30e7\u30f3\u3060\u3063\u305f\u308a\u3001\u524a\u9664\u30a2\u30af\u30b7\u30e7\u30f3\u3060\u3063\u305f\u308a\u304c\u60f3\u5b9a\u3055\u308c\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<h5>case\u3067\u306e\u5404\u51e6\u7406<\/h5>\n<p>\u57fa\u672c\u7684\u306b\u7a7a\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u7528\u610f\u3057\u3066\u304a\u3044\u3066\u305d\u308c\u3092\u30b3\u30d4\u30fc\u3057\u3066\u30b3\u30d4\u30fc\u306b\u5909\u66f4\u3092\u53cd\u6620\u3057\u3066\u8fd4\u3057\u307e\u3059\u3002\uff08\u3053\u3046\u3059\u308b\u305f\u3081\u306b\u7d14\u7c8b\u95a2\u6570\u3068\u547c\u3070\u308c\u308b\u3002\uff09<\/p>\n<p>\u306a\u304a\u3001Reducer\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u306a\u3044Action\u304c\u6307\u5b9a\u3055\u308c\u305f\u5834\u5408\u306fstate\u3092\u305d\u306e\u307e\u307e\u8fd4\u3059\u3088\u3046\u306b\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u4e00\u822c\u7684\u3067\u3042\u308a\u4f55\u3082\u8d77\u3053\u3057\u307e\u305b\u3093\u3002<\/p>\n<h4>3.combineReducers\u3067\u8907\u6570\u306eReducer\u3092\u7d44\u307f\u5408\u308f\u305b\u30661\u3064\u306eReducer\u306b\u307e\u3068\u308b\u3002<\/h4>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">export default combineReducers({\r\n  test\r\n});<\/pre>\n<h3>combineReducers\u3068\u306f\uff1f<\/h3>\n<p>Redux\u306eReducer\u306f\u95a2\u5fc3\u3054\u3068\u3054\u3068\u306b\u5206\u5272\u3057\u3001Reducer\u3054\u3068\u306b\u5225\u3005\u306e\u540d\u524d\u7a7a\u9593\u3067\u51e6\u7406\u304c\u5b9f\u884c\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30a4\u30e1\u30fc\u30b8\u3002<\/p>\n<p>\u305d\u308c\u3089\u306eReducer\u3092\u7d71\u5408\u3059\u308b\u3002<\/p>\n<h4>combineReducers\u304c\u3084\u3063\u3066\u3044\u308b\u3053\u3068<\/h4>\n<ul>\n<li>\u5404reducer\u3092\u547c\u3073\u51fa\u3057\u3066\u521d\u671f\u72b6\u614b\u3092\u53d6\u308a\u51fa\u3059\u3002<\/li>\n<li>\u521d\u671f\u72b6\u614b\u3092\u307e\u3068\u3081\u3066\u521d\u671f\u72b6\u614b\u30c4\u30ea\u30fc\u3092\u4f5c\u308b\u3002<\/li>\n<li>reducer\u306e\u51e6\u7406\u3092\u307e\u3068\u3081\u305fcombination\u95a2\u6570\u3092\u8fd4\u3059\u3002<\/li>\n<\/ul>\n<h4>combination\u95a2\u6570<\/h4>\n<p>reducer\u540d\u3092\u30ad\u30fc\u306b\u3057\u305f\u72b6\u614b\u30c4\u30ea\u30fc\u304b\u3089\u305d\u306ereducer\u306e\u90e8\u5206\u72b6\u614b\u3092\u53d6\u308a\u51fa\u3057\u3066\u6e21\u3059\u3002<\/p>\n<h4>\u6ce8\u610f\u70b9<\/h4>\n<p>combineReducer\u3092\u4f7f\u3063\u305f\u304b\u3089\u3068\u8a00\u3063\u3066\u3001Reducer\u9593\u306e\u30c7\u30fc\u30bf\u5171\u6709\u304c\u3067\u304d\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3002<\/p>\n<h2>Action<\/h2>\n<h3>\u5b9f\u88c5\u65b9\u6cd5<\/h3>\n<p>Store\u306b\u5909\u5316\u3092\u52a0\u3048\u308b\u305f\u3081\u306e\u5177\u4f53\u7684\u306a\u95a2\u6570\u7fa4\u306a\u3069\u306f\u4e00\u7b87\u6240\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">export function a(test) {\r\n  return {\r\n    type: 'A',\r\n    test\r\n  }\r\n}\r\n\r\nexport function b(test) {\r\n  return {\r\n    type: 'B',\r\n    test\r\n  }\r\n}<\/pre>\n<h4>Action Creator<\/h4>\n<p>\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u8fd4\u3057\u3066\u3044\u308b\u95a2\u6570\u3092\u300cAction Creator\u300d\u3068\u547c\u3073\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u308b\u305f\u3081\u306e\u30ed\u30b8\u30c3\u30af\u3092\u8a18\u8ff0<\/strong><\/span>\u3057\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5074\u3067\u5229\u7528\u3059\u308b\u306e\u3067export\u3057\u3066\u4f7f\u3046\u306e\u304c\u666e\u901a\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">export function a(test) {\r\n return Action\r\n}<\/pre>\n<h5>redux-thunk<\/h5>\n<p>\u3082\u3057\u3001Action Creator\u5185\u3067<span style=\"color: #ff0000;\"><strong>\u975e\u540c\u671f\u306a\u51e6\u7406\u3092\u8a18\u8ff0\u3057\u305f\u3044\u5834\u5408<\/strong><\/span>\u306f\u3053\u308c\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h4>Action<\/h4>\n<p>\u300cAction Creator\u300d\u3067\u8fd4\u3055\u308c\u3066\u3044\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002\u5177\u4f53\u7684\u306b\u306f\u4ee5\u4e0b\u306e\u90e8\u5206\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"lang:default decode:true \">{\r\n    type: 'A',\r\n    test\r\n}<\/pre>\n<h5>type<\/h5>\n<p>\u6307\u5b9a\u306f\u5fc5\u9808\u3067\u3059\u3002Reducer\u5074\u3067\u3069\u306eAction\u304b\u3069\u3046\u304b\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Reducer\u3068\u306f\uff1f Store\u306b\u5909\u5316\u3092\u52a0\u3048\u308b\u305f\u3081\u306b\u5fc5\u8981\u306aRedux\u306e\u8981\u7d20\u3067\u3059\u3002state\u3068action\u306e\u4e8c\u3064\u306e\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308a\u307e\u3059\u3002 Reducer\u306e\u4f5c\u6210\u624b\u9806 1.redux\u30e9\u30a4\u30d6\u30e9\u30ea\u304b\u3089combineReducers\u3092 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[116,117],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13155"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13155"}],"version-history":[{"count":10,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13155\/revisions"}],"predecessor-version":[{"id":20443,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13155\/revisions\/20443"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13155"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}