{"id":13142,"date":"2021-02-08T16:55:32","date_gmt":"2021-02-08T07:55:32","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13142"},"modified":"2023-05-27T22:16:34","modified_gmt":"2023-05-27T13:16:34","slug":"%e3%80%90react%e3%80%91%e3%80%8credux%e3%80%8d%e3%81%ae3%e5%8e%9f%e5%89%87","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13142","title":{"rendered":"\u3010Redux\u3011Redux\u306e\u57fa\u672c\u3068\u52d5\u4f5c\u306e\u4ed5\u7d44\u307f\u30013\u539f\u5247\uff08\u5358\u4e00\u60c5\u5831\u6e90\u3001\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u3001\u7d14\u7c8b\u95a2\u6570\uff09"},"content":{"rendered":"<h2>Redux\u306e\u6982\u8981<\/h2>\n<p>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30b5\u30a4\u30ba\u304c\u5927\u304d\u304f\u306a\u3063\u305f\u969b\u3084\u8907\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u9593\u3067\u72b6\u614b\u3092\u7ba1\u7406\u3084\u5171\u6709\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b2015\u5e74\u306b\u516c\u958b\u3055\u308c\u305f\u72b6\u614b\u7ba1\u7406\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<h2>\u30e1\u30ea\u30c3\u30c8<\/h2>\n<h3>\u7279\u5b9a\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u914d\u4e0b\u306bstate\u3092\u304a\u304b\u306a\u3044\u306e\u3067\u3001\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3092\u9632\u304e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u4e0a\u304c\u308b\u3002<\/h3>\n<p>Redux\u306f\u7279\u5b9a\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u914d\u4e0b\u306b\u3042\u308b\u308f\u3051\u3067\u306a\u3044Store\u3067state\u3092\u7ba1\u7406\u3092\u3057\u307e\u3059\u3002React\u306f\u3001\u89aa\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u4fdd\u6301\u3057\u3066\u3044\u308bstate\u304c\u66f4\u65b0\u3055\u308c\u308b\u3068\u5b50\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3082\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u308b\u3068\u3044\u3046\u6027\u8cea\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u304c\u305d\u308c\u3092\u9632\u3052\u307e\u3059\u3002<\/p>\n<h3>\u30c6\u30b9\u30c8\u304c\u66f8\u304d\u3084\u3059\u3044\u3002<\/h3>\n<p>Action Creator\u3084Reducer\u306a\u3069\u7d14\u7c8b\u95a2\u6570\u3068\u3057\u3066\u66f8\u3051\u308b\u306e\u3067\u30c6\u30b9\u30c8\u304c\u66f8\u304d\u3084\u3059\u3044\u3002<\/p>\n<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>redux\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">yarn add redux react-redux<\/pre>\n<h2>Redux\u306e\u4e3b\u306a\u8981\u7d20<\/h2>\n<h3>\u6982\u8981\u56f3<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-13146\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2021\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2021-02-08-17.15.35-1024x383.png\" alt=\"\" width=\"1024\" height=\"383\" \/><\/p>\n<h3>State<\/h3>\n<p>Redux\u30a2\u30d7\u30ea\u306e\u72b6\u614b\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>Action<\/h3>\n<p>State\u306e\u5909\u66f4\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u6301\u3063\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002\u72b6\u614b\u5909\u66f4\u306fAction\u3092\u4ecb\u3057\u3066\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002type\u30d7\u30ed\u30d1\u30c6\u30a3\u306f\u5fc5\u9808\u3067\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u30c7\u30fc\u30bf(payload)\u3092\u6301\u3063\u305f\u30d7\u30ed\u30d1\u30c6\u30a3 \u3092\u6301\u3064\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<h3>Reducer<\/h3>\n<p>\u53d7\u3051\u53d6\u3063\u305fAction\u3068\u305d\u306e\u6642\u70b9\u306eState\u304b\u3089\u65b0\u3057\u3044State\u3092\u751f\u6210\u3057\u3066\u8fd4\u3059\u95a2\u6570\u3067\u3059\u3002\u7d14\u7c8b\u95a2\u6570\u3068\u547c\u3070\u308c\u307e\u3059\u3002<\/p>\n<p>Action\u3092\u4f7f\u3063\u3066\u72b6\u614b\u5909\u66f4\u3055\u305b\u308b\u306b\u306fReducer\u3068\u547c\u3070\u308c\u308b\u95a2\u6570\u306bAction\u3092\u9001\u4fe1\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>Action Creator<\/h3>\n<p>Action\u3092\u8fd4\u3059\u95a2\u6570\u3067\u3059\u3002<\/p>\n<h3>Store<\/h3>\n<p>State\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067Redux\u30a2\u30d7\u30ea\u306b\u4e00\u3064\u5b58\u5728\u3057\u307e\u3059\u3002createStore\u3092\u4f7f\u3063\u3066\u4f5c\u3089\u308c\u307e\u3059\u3002Store\u306f\u4e0b\u8a18\u30e1\u30bd\u30c3\u30c9\u3092\u6301\u3061\u307e\u3059\u3002<\/p>\n<h4>dispatch(action)<\/h4>\n<p>State\u5909\u66f4\u306e\u305f\u3081\u306eAction\u3092Store\u306b\u9001\u4ed8\u3059\u308b\u3002\u3082\u3057\u3001\u975e\u540c\u671f\u306a\u3069\u306e\u526f\u4f5c\u7528\u306e\u3042\u308b\u51e6\u7406\u3092\u631f\u3080\u5834\u5408\u306fmiddleware\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h4>getState()<\/h4>\n<p>State\u5024\u306e\u53d6\u5f97<\/p>\n<h4>subscribe(listener)<\/h4>\n<p>State\u5909\u66f4\u6642\u306b\u547c\u3070\u308c\u308b\u95a2\u6570\u3092\u767b\u9332\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>Redux\u306e\u52d5\u4f5c\u306e\u4ed5\u7d44\u307f<\/h2>\n<h3>state\u3067\u4fdd\u6301\u3057\u3066\u3044\u308b\u72b6\u614b\u3092\u6e21\u3059\u3002<\/h3>\n<ul>\n<li>Redux\u30b9\u30c8\u30a2\u306e\u72b6\u614b\u3092\u6e21\u3059\u305f\u3081\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4f7f\u3046\u306e\u3067\u306f\u306a\u304f\u3001\u66f4\u65b0\u306e\u30c1\u30a7\u30c3\u30af\u3092\u3059\u308b\u305f\u3081\u306bRedux\u30b9\u30c8\u30a2\u3078\u306e\u30b5\u30d6\u30b9\u30af\u30ea\u30d7\u30b7\u30e7\u30f3\u306e\u305f\u3081\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3002<\/li>\n<li>\u5e38\u306b\u540c\u3058\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u5024\u3092\u300c&lt;ReactReduxContext.Provider&gt;\u300d\u306b\u6e21\u3057\u3066\u3044\u308b\u3002<\/li>\n<\/ul>\n<h3>dispatch\u306e\u52d5\u4f5c<\/h3>\n<ul>\n<li>\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u3055\u308c\u308b\u305f\u3073\u306bRedux\u30b9\u30c8\u30a2\u306f\u3059\u3079\u3066\u306e\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d0\u30fc\u306b\u901a\u77e5\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3059\u308b\u3002\u30b5\u30d6\u30b9\u30af\u30ea\u30d7\u30b7\u30e7\u30f3\u306fRedux\u30b9\u30c8\u30a2\u306estate\u304c\u66f4\u65b0\u3055\u308c\u308b\u5ea6\u306b\u5b9f\u884c\u3055\u308c\u308b\u306e\u3067\u3067\u304d\u308b\u3060\u3051\u9ad8\u901f\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/li>\n<\/ul>\n<h4>\u30b5\u30d6\u30b9\u30af\u30ea\u30d7\u30b7\u30e7\u30f3\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u30d7\u30ed\u30bb\u30b9<\/h4>\n<ul>\n<li>\u5b8c\u5168\u306bReact\u306e\u5916\u3067\u884c\u308f\u308c\u308b\u3002<\/li>\n<\/ul>\n<h3>UI\u30ec\u30a4\u30e4\u30fc<\/h3>\n<ul>\n<li>\u5e38\u306b\u3001Redux\u30b9\u30c8\u30a2\u306b\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d6\u3057\u3066\u6700\u65b0\u306estate\u3092\u8aad\u307f\u53d6\u308a\u5024\u3092\u5dee\u5206\u3057\u3066\u30c7\u30fc\u30bf\u304c\u66f4\u65b0\u3055\u308c\u3066\u3044\u305f\u3089\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3092\u884c\u3046\u3002(mapState\u3084useSelector\u306e\u623b\u308a\u5024\u306b\u57fa\u3065\u3044\u3066React\u306f\u5dee\u5206\u304c\u3042\u308c\u3070\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3092\u884c\u3046\u3002)<\/li>\n<\/ul>\n<h2>\u4e09\u539f\u5247<\/h2>\n<h3>\u7b2c\u4e00\u539f\u5247(Single source of truth)<\/h3>\n<p>\u65e5\u672c\u8a9e\u306b\u8a33\u3059\u3068\u300c\u5358\u4e00\u60c5\u5831\u6e90\u300d\u3067\u3059\u3002\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u72b6\u614b\u306f\u305f\u3060\u4e00\u3064\u306eStore\u3067\u7ba1\u7406\u3059\u308b(\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3\u8a2d\u8a08)\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u7b2c\u4e8c\u539f\u5247(State is read-only)<\/h3>\n<p>State\u3092\u76f4\u63a5\u5909\u66f4\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002Reducer\u3092\u901a\u3058\u3066\u306e\u307f\u8a31\u53ef\u3057\u307e\u3059\u3002State\u3092\u5909\u66f4\u3059\u308b\u305f\u3081\u306b\u306fStore\u306eReducer\u306b\u5bfe\u3057\u3066Action\u3092dispatch\uff08\u9001\u4ed8\uff09\u3057\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3053\u308c\u3092state\u306f\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u8868\u73fe\u3092\u3057\u307e\u3059\u3002\u8a73\u3057\u304f\u306f\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u3082\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"xcqNpcAkG4\"><p><a href=\"http:\/\/www.code-magagine.com\/?p=13832\">\u3010Redux\u3011\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u3068\u306f\uff1f<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/www.code-magagine.com\/?p=13832&#038;embed=true#?secret=xcqNpcAkG4\" data-secret=\"xcqNpcAkG4\" width=\"500\" height=\"282\" title=\"&#8220;\u3010Redux\u3011\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u3068\u306f\uff1f&#8221; &#8212; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30de\u30ac\u30b8\u30f3\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<h3>\u7b2c\u4e09\u539f\u5247(Changes are made with pure functions)<\/h3>\n<p>State\u306e\u5909\u66f4\u306fReducer\u306e<span style=\"color: #ff0000;\"><strong>pure functions(\u7d14\u7c8b\u95a2\u6570)<\/strong><\/span>\u306b\u3088\u3063\u3066\u884c\u308f\u308c\u307e\u3059\u3002\u7d14\u7c8b\u95a2\u6570\u3068\u306f\u5f15\u6570\u306b\u3069\u306e\u3088\u3046\u306a\u5024\u3092\u4e0e\u3048\u3066\u3082\u5fc5\u305a\u540c\u3058\u5024\u3092\u623b\u308a\u5024\u3068\u3057\u3066\u8fd4\u3059\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u3092\u300cReducer\u306f\u526f\u4f5c\u7528\u304c\u306a\u3044\u300d\u3068\u3044\u3046\u547c\u3073\u65b9\u3092\u3057\u307e\u3059\u3002<\/p>\n<h4>\u7d14\u7c8b\u95a2\u6570\u3068\u306f\uff1f<\/h4>\n<p>\u540c\u3058\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308c\u3070\u3044\u3064\u5b9f\u884c\u3057\u3066\u3082\u4f55\u5ea6\u5b9f\u884c\u3057\u3066\u3082\u5fc5\u305a\u540c\u3058\u7d50\u679c\u3092\u8fd4\u3059\u95a2\u6570\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>Redux\u306eReducer\u306f\u7d14\u7c8b\u95a2\u6570\u306e\u601d\u60f3\u304c\u53d6\u308a\u5165\u308c\u3089\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h5>\u30e1\u30ea\u30c3\u30c8<\/h5>\n<p>\u95a2\u6570\u306e\u518d\u5229\u7528\u6027\u3084\u5358\u4f53\u30c6\u30b9\u30c8\u304c\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u3068\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb<\/h4>\n<p>JavaScript\u306e\u30c7\u30fc\u30bf\u578b\u306f\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u3068\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\u306b\u5206\u304b\u308c\u307e\u3059\u3002<\/p>\n<h5>\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\uff08\u5909\u66f4\u53ef\u80fd\uff09<\/h5>\n<p>\u5909\u6570\u540c\u58eb\u3092\u4ee3\u5165\u3057\u305f\u5834\u5408\u306b\u4ee3\u5165\u5148\u306e\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308b\u3068\u4ee3\u5165\u5143\u306e\u30c7\u30fc\u30bf\u307e\u3067\u5024\u304c\u5909\u308f\u308a\u307e\u3059\u3002<\/p>\n<h6>\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u578b<\/h6>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001\u914d\u5217\u3001\u95a2\u6570<\/p>\n<h5>\u30a4\u30df\u30e5\u30fc\u30bf\u30d6\u30eb\uff08\u5909\u66f4\u4e0d\u53ef\uff09<\/h5>\n<p>\u5909\u6570\u540c\u58eb\u3092\u4ee3\u5165\u3057\u305f\u5834\u5408\u306b\u4ee3\u5165\u5148\u306e\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308b\u3068\u4ee3\u5165\u5143\u306e\u30c7\u30fc\u30bf\u307e\u3067\u5024\u304c\u5909\u308f\u3089\u306a\u3044\u3067\u3059\u3002<\/p>\n<h6>\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u578b<\/h6>\n<p>\u6587\u5b57\u5217\u3001\u6570\u5024\u3001\u771f\u507d\u5024\u3001null\u3001undefined\u3001\u30b7\u30f3\u30dc\u30eb<\/p>\n<p>Reducer\u306f\u73fe\u5728\u306e\u72b6\u614b\u3068\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u5229\u7528\u3057\u3066\u73fe\u5728\u306e\u72b6\u614b\u306b\u5909\u66f4\u3092\u52a0\u3048\u308b\u3053\u3068\u306a\u304f\u65b0\u3057\u3044\u72b6\u614b\u3092\u8fd4\u3057\u307e\u3059\u3002\u3053\u306e\u610f\u5473\u3067\u7d14\u7c8b\u95a2\u6570\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>3\u539f\u5247\u304c\u3042\u308b\u3053\u3068\u3067\u306e\u30e1\u30ea\u30c3\u30c8<\/h3>\n<p>\u30c7\u30fc\u30bf\u306e\u6d41\u308c\u304c\u4e00\u65b9\u5411\u306b\u306a\u308a\u8907\u96d1\u306b\u306a\u3089\u305a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u6d41\u308c\u304c\u8ffd\u3044\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Redux\u306e\u6982\u8981 \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30b5\u30a4\u30ba\u304c\u5927\u304d\u304f\u306a\u3063\u305f\u969b\u3084\u8907\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u9593\u3067\u72b6\u614b\u3092\u7ba1\u7406\u3084\u5171\u6709\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b2015\u5e74\u306b\u516c\u958b\u3055\u308c\u305f\u72b6\u614b\u7ba1\u7406\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002 \u30e1\u30ea\u30c3\u30c8 \u7279\u5b9a\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u914d\u4e0b\u306bstate\u3092\u304a\u304b\u306a\u3044 [&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\/13142"}],"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=13142"}],"version-history":[{"count":16,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13142\/revisions"}],"predecessor-version":[{"id":20444,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13142\/revisions\/20444"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13142"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}