{"id":13391,"date":"2021-05-02T19:53:47","date_gmt":"2021-05-02T10:53:47","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13391"},"modified":"2023-05-27T22:10:29","modified_gmt":"2023-05-27T13:10:29","slug":"%e3%80%90redux%e3%80%91%e3%82%88%e3%81%8f%e4%bd%bf%e3%82%8f%e3%82%8c%e3%82%8b%e3%83%a9%e3%82%a4%e3%83%96%e3%83%a9%e3%83%aa%e4%b8%80%e8%a6%a7","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13391","title":{"rendered":"\u3010React\/Redux\u3011\u3088\u304f\u4f7f\u308f\u308c\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u4e00\u89a7(redux-tool-kit\u3001redux-form\u3001reselect\u306a\u3069)"},"content":{"rendered":"<h2>\u5168\u822c<\/h2>\n<h3>Redux Tool Kit<\/h3>\n<p>Redux\u306e\u5b66\u7fd2\u30b3\u30b9\u30c8\u304c\u9ad8\u3044\u3053\u3068\u3092\u307f\u3066Redux\u306e\u958b\u767a\u30c1\u30fc\u30e0\u304c2019\u5e7410\u6708\u306b\u30ea\u30ea\u30fc\u30b9\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u3092\u4f7f\u3046\u3053\u3068\u3067\u7c21\u5358\u306bRedux\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>Redux Tool Kit\u306e\u30b3\u30de\u30f3\u30c9\u4e00\u3064\u3067Redux &amp; Hooks\u00a0&amp; TypeScript\u306e\u74b0\u5883\u304c\u6574\u3044\u307e\u3059\u3002Redux\u306eStore\u3092slice\u3068\u3044\u3046\u5358\u4f4d\u306b\u5206\u5272\u3057\u307e\u3059\u3002<\/p>\n<h4>\u7279\u5fb4<\/h4>\n<h5>\u5404\u7a2e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5185\u8535\u3057\u3066\u3044\u308b\u3002<\/h5>\n<ul>\n<li>Immer<\/li>\n<li>redux thunk<\/li>\n<li>reselect<\/li>\n<\/ul>\n<h4>slice<\/h4>\n<p>Store\u3092\u5206\u5272\u3057\u305f\u4e00\u3064\u306e\u5358\u4f4d\u3067\u3059\u3002\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u6a5f\u80fd\u3054\u3068\u306b\u4e00\u3064\u4f5c\u308a\u307e\u3059\u3002\uff08\u4f8b\u3048\u3070\u3001\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u7528\u306eslice\u3001TODO\u6a5f\u80fd\u7528\u306eslice\u306a\u3069\uff09slice\u306f\u4e00\u3064\u306eReducer\u3092\u6301\u3061\u307e\u3059\u3002<\/p>\n<p>\u30a4\u30e1\u30fc\u30b8\u7684\u306b\u306f\u3001\u5f93\u6765\u306eRedux\u306b\u3042\u3063\u305f<span style=\"color: #ff0000;\"><strong>\u300cReducer + state + action Creator\u300d<\/strong><\/span>\u306e\u3088\u3046\u306a\u3082\u306e\u3002<\/p>\n<h4>\u30a4\u30e1\u30fc\u30b8\u56f3<\/h4>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-13416\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2021\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2021-05-04-13.55.27-1024x751.png\" alt=\"\" width=\"512\" height=\"375\" srcset=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2021\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2021-05-04-13.55.27-1024x751.png 1024w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2021\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2021-05-04-13.55.27-300x220.png 300w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2021\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2021-05-04-13.55.27-768x563.png 768w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2021\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2021-05-04-13.55.27.png 1064w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/p>\n<h5>createSlice<\/h5>\n<p>slice\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u3053\u308c\u3092\u4f7f\u3046\u3068\u521d\u671f\u5024\u3068\u3001action creator\u3068Reducer\u306e\u751f\u6210\u3092\u4e00\u767a\u3067\u884c\u3063\u3066\u304f\u308c\u308b\u3002<\/p>\n<h5>configureStore<\/h5>\n<p>Redux\u306eStore\u306b\u4f5c\u6210\u3057\u305fslice\u3092\u767b\u9332\u3057\u307e\u3059\u3002\u88cf\u5074\u3067combineReducer\u304c\u50cd\u3044\u3066\u3044\u3066\u8907\u6570\u306eslice\u3092\u7d50\u5408\u3055\u305b\u3066Store\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<h5>useDispatch<\/h5>\n<p>action\u3092Redux\u306eStore\u306e\u4e2d\u306b\u4f1d\u9054\u3057\u307e\u3059\u3002<\/p>\n<h5>useSelect<\/h5>\n<p>React\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089Redux\u306estate\u3092\u76f4\u63a5\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h2>\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3<\/h2>\n<h3>typescript-fsa<\/h3>\n<p>\u5b9a\u7fa9\u652f\u63f4\u30e9\u30a4\u30d6\u30e9\u30ea\u3002TypeScript\u3067\u306eAction\u306e\u5b9a\u7fa9\u3092\u3057\u3066\u304f\u308c\u308b\u3002<\/p>\n<h3>redux-actions<\/h3>\n<p>redux-actions\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u3053\u3068\u3067\u4e0b\u8a18\u306e\u3088\u3046\u306b\u7c21\u6f54\u306b\u8a18\u8ff0\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u5b9f\u52d9\u3067\u306f\u3053\u3061\u3089\u306e\u66f8\u304d\u65b9\u304c\u4f7f\u308f\u308c\u3066\u3044\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<h4>Actions\u5074<\/h4>\n<p>createActions\u3067Action-Creator\u306e\u8a18\u8ff0\u3092\u697d\u306b\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { createActions } from 'redux-actions'\r\n\r\nexport const Actions = createActions(\r\n  {aMethod: (args) =&gt; (args)},\r\n  {bMethod: (args) =&gt; (args) },\r\n)\r\n<\/pre>\n<h4>Reducer\u5074<\/h4>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306bReducer\u3092\u7c21\u6f54\u306b\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { handleActions } from 'redux-actions'\r\nimport { Actions } from '.\/Actions' \/\/\u4f5c\u6210\u3057\u305fActions\u3092\u8aad\u8fbc\r\n\r\nconst initialState = { \r\n    value: null,\r\n}\r\n\r\nexport const reducer = handleActions({\r\n  [Actions.aMethod]: (state, action) =&gt; ({\r\n    ...state,\r\n    value: action.payload,\r\n  }),\r\n  [Actions.bMethod]: (state, action) =&gt; ({\r\n    ...state,\r\n    value: action.payload,\r\n  }),\r\n}, initialState)\r\n\r\nexport default combineReducers({\r\n  reducer\r\n});<\/pre>\n<h2>\u975e\u540c\u671f\u51e6\u7406\u95a2\u9023<\/h2>\n<p>\u4ee5\u4e0b\u304c2\u5927\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002\u5f93\u6765\u306eRedux\u3092\u5c0e\u5165\u3059\u308b\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3069\u3061\u3089\u304b\u3092\u4f7f\u3046\u306e\u304c\u666e\u901a\u3067\u3057\u305f\u3002\u3057\u304b\u3057\u3001RTK\u3092\u5c0e\u5165\u3059\u308c\u3070\u3053\u308c\u3089\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5c0e\u5165\u3057\u306a\u304f\u3066\u3082\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<h3>redux-thunk<\/h3>\n<p>RTK\u306b\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u8a2d\u5b9a\u3057\u306a\u304f\u3066\u3082\u826f\u304f\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<h3>redux-saga<\/h3>\n<h2>immutable\u7cfb<\/h2>\n<h3>immer<\/h3>\n<h3>immutablejs<\/h3>\n<h2>\u30ad\u30e3\u30c3\u30b7\u30e5\u95a2\u9023<\/h2>\n<h3>reselect<\/h3>\n<p>dispatch\u3059\u308b\u95a2\u6570\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u3057\u3066\u3001\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u6642\u306b\u8a08\u7b97\u7d50\u679c\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u3067\u304d\u308b\u3002<\/p>\n<p>\u6700\u8fd1\u306fRTK(reselct\u3092\u4e2d\u306b\u542b\u3093\u3067\u3044\u308b\u3002)\u306e\u767b\u5834\u306b\u3088\u3063\u3066\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u3002<\/p>\n<h4>\u5b9f\u88c5<\/h4>\n<p>redux\u306estate\u306e\u5024\u3092\u4f7f\u3063\u3066\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067\u95a2\u6570\u3092\u4f5c\u3063\u305f\u5834\u5408\u306f\u3001state\u306e\u5024\u304c\u66f4\u65b0\u3055\u308c\u308b\u305f\u3073\u306b\u51e6\u7406\u304c\u52d5\u3044\u3066\u3057\u307e\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true \">import { createSelector } from 'reselect';\r\n\r\nconst \u5bfe\u8c61\u3068\u3059\u308bstate = state =&gt; state.\u5bfe\u8c61\u3068\u3059\u308b\u9805\u76ee\r\n\r\nexport const \u51e6\u7406\u3055\u308c\u305f\u7d50\u679c =  createSelector(\r\n  [\u5bfe\u8c61\u3068\u3059\u308bstate], \/\/inputSelectors\u3068\u547c\u3070\u308c\u308b\u3089\u3057\u3044\u3067\u3059\u3002\r\n  \u51e6\u7406\u5185\u5bb9\uff08\u30d5\u30a3\u30eb\u30bf\u30fc\u306a\u3069\uff09 \/\/resultFunc\u3068\u547c\u3070\u308c\u308b\u3089\u3057\u3044\u3067\u3059\u3002\r\n)<\/pre>\n<p>\u3053\u308c\uff08\u51e6\u7406\u3055\u308c\u305f\u7d50\u679c\uff09\u3092\u500b\u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8(container component)\u304b\u3089import\u3057\u3066\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h4>\u5229\u70b9<\/h4>\n<h5>\u4e00\u5ea6\u5b9f\u884c\u3057\u305f\u95a2\u6570\u306f\u30ad\u30e3\u30c3\u30b7\u30e5\u3057\u3066\u304f\u308c\u308b\u3002<\/h5>\n<p>\u901a\u5e38\u306estate\u306e\u5024\u3092\u305d\u306e\u307e\u307e\u4f7f\u7528\u3059\u308b\u5834\u5408\u306b\u6bd4\u3079\u3066\u30ad\u30e3\u30c3\u30b7\u30e5\u3055\u308c\u308b\u306e\u3067\u5bfe\u8c61\u306estate\u306b\u66f4\u65b0\u304c\u306a\u3051\u308c\u3070\u969b\u5b9f\u884c\u3055\u308c\u308b\u3053\u3068\u304c\u306a\u304f\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u3088\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>\u6ce8\u610f\u70b9<\/h4>\n<p>\u305f\u3060\u3001reselect\u306e\u30e1\u30e2\u5316\u306b\u3082\u5c11\u306a\u304b\u3089\u305a\u8ca0\u8377\u306f\u304b\u304b\u308b\u3082\u306e\u306a\u306e\u3067\u3001Store\u306e\u5024\u3092\u305f\u3060\u305d\u306e\u307e\u307e\u53d6\u308a\u51fa\u3059\u3060\u3051\u3067\u3042\u308c\u3070\u7121\u7406\u306b\u4f7f\u3046\u5fc5\u8981\u306f\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u62bd\u51fa\u3057\u305f\u5024\u306b\u4f55\u304b\u3057\u3089\u52a0\u5de5\u3059\u308b\u5834\u5408\u306e\u307freselect\u3092\u4f7f\u3046\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30d5\u30a9\u30fc\u30e0\u95a2\u9023<\/h2>\n<h3>redux-form<\/h3>\n<p>React + Redux\u3067\u30d5\u30a9\u30fc\u30e0\u753b\u9762\u3092\u4f5c\u6210\u3059\u308b\u969b\u306b\u4fbf\u5229\u306aRedux\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<h4>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h4>\n<pre class=\"lang:default decode:true\">yarn add redux react-redux redux-form<\/pre>\n<h4>\u5c0e\u5165<\/h4>\n<p>Reducers\u306b\u5c0e\u5165\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">import { combineReducers } from \"redux\";\r\nimport { reducer as formReducer } from \"redux-form\";\r\n\r\nconst reducer = combineReducers({\r\n  form: formReducer\r\n});\r\n\r\nexport default reducer;<\/pre>\n<h3>formik<\/h3>\n<p>\u30d5\u30a9\u30fc\u30e0\u306e\u72b6\u614b\u7ba1\u7406\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u884c\u3048\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3002\u6700\u8fd1\u306fredux Form\u306b\u8ca0\u3051\u305a\u52a3\u3089\u305a\u4eba\u6c17\u304c\u4e0a\u304c\u3063\u3066\u304d\u3066\u3044\u308b\u3089\u3057\u3044\u3002<\/p>\n<h3>React Hooks Form<\/h3>\n<p>onSubmit\u6642\u306b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u884c\u308f\u305b\u3066\u30ad\u30fc\u30b9\u30c8\u30ed\u30fc\u30af\u3054\u3068\u306estate\u306e\u66f4\u65b0\u3092\u7121\u304f\u3057\u3066\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u3088\u304f\u3057\u3066\u3044\u308b\u3002\u3082\u3057\u3001\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u5fc5\u8981\u306a\u3051\u308c\u3070\u9078\u629e\u80a2\u306b\u5165\u308b\u3002<\/p>\n<h2>\u305d\u306e\u4ed6<\/h2>\n<h3>normalizr<\/h3>\n","protected":false},"excerpt":{"rendered":"\u5168\u822c Redux Tool Kit Redux\u306e\u5b66\u7fd2\u30b3\u30b9\u30c8\u304c\u9ad8\u3044\u3053\u3068\u3092\u307f\u3066Redux\u306e\u958b\u767a\u30c1\u30fc\u30e0\u304c2019\u5e7410\u6708\u306b\u30ea\u30ea\u30fc\u30b9\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u3092\u4f7f\u3046\u3053\u3068\u3067\u7c21\u5358\u306bRedux\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002 Redux Tool [&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\/13391"}],"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=13391"}],"version-history":[{"count":17,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13391\/revisions"}],"predecessor-version":[{"id":20429,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13391\/revisions\/20429"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13391"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}