{"id":13509,"date":"2021-05-23T12:03:30","date_gmt":"2021-05-23T03:03:30","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13509"},"modified":"2023-05-27T22:09:47","modified_gmt":"2023-05-27T13:09:47","slug":"%e3%80%90react-redux%e3%80%91%e3%80%8credux-tool-kit%e3%80%8d%e3%81%a7%e9%9d%9e%e5%90%8c%e6%9c%9f%e3%81%aeslice%e3%82%92%e4%bd%9c%e3%82%8b%e5%a0%b4%e5%90%88","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13509","title":{"rendered":"\u3010React\/Redux\u3011\u300cRedux Tool Kit\u300d\u3067\u975e\u540c\u671f\u306eSlice\u3092\u4f5c\u308b\u5834\u5408\u3001builder\u30aa\u30d6\u30b8\u30a7\u30af\u30c8"},"content":{"rendered":"<p>Redux\u3067\u975e\u540c\u671f\u306e\u5834\u5408\u306eSlice\u306e\u4f5c\u308a\u65b9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { createSlice, createAsyncThunk } from '@reduxjs\/toolkit';\r\nimport axios from 'axios';\r\n\r\n\/\/ Slice\u306e\u5916\u3067\u30d5\u30a7\u30c3\u30c1\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u306f\u8a18\u8ff0\u3059\u308b\u3088\u3046\u306b\u63a8\u5968\u3055\u308c\u3066\u3044\u308b\u3002\r\nexport const fetchAsyncGet = createAsyncThunk('fetch\/get', async () =&gt; {\r\n  const res = await axios.get('API\u306e\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8URL');\r\n  return res.data;\r\n});\r\n\r\nconst fetchSlice = createSlice({\r\n  name: 'fetch',\r\n  initialState: { tasks: [] },\r\n  reducers: {},\r\n  extraReducers: (builder) =&gt; {\r\n    builder.addCase(fetchAsyncGet.fulfilled, (state, action) =&gt; {\r\n      return {\r\n        ...state,\r\n        tasks: action.payload,\r\n      };\r\n    });\r\n  },\r\n});\r\nexport const selectTasks = (state) =&gt; state.fetch.tasks;\r\nexport default fetchSlice.reducer;\r\n<\/pre>\n<h2>createAsyncThunk<\/h2>\n<p>\u5f93\u6765\u306eredux\u3060\u3068redux-thunk\u306a\u3069\u306e\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3067\u884c\u3063\u3066\u3044\u305f\u7269\u304cRedux Tool Kit\u6a19\u6e96\u6a5f\u80fd\u3067\u307e\u304b\u306a\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u306a\u304a\u3001Slice\u306e\u5916\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">createAsyncThunk('\u30a2\u30af\u30b7\u30e7\u30f3\u306e\u540d\u524d',\u975e\u540c\u671f\u95a2\u6570)<\/pre>\n<h3>\u5f15\u6570<\/h3>\n<table>\n<tbody>\n<tr>\n<th>\u5f15\u6570<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>\u30a2\u30af\u30b7\u30e7\u30f3\u306e\u540d\u524d<\/td>\n<td>\u300c\u30b9\u30e9\u30a4\u30b9\u306e\u540d\u524d\/\u30e1\u30bd\u30c3\u30c9\u540d\u300d(fetch\/post\u306a\u3069)\u3068\u3044\u3046\u547d\u540d\u898f\u5247\u304c\u3088\u304f\u4f7f\u308f\u308c\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u975e\u540c\u671f\u95a2\u6570<\/td>\n<td>async\/await\u306a\u3069\u3067\u975e\u540c\u671f\u306e\u95a2\u6570\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u623b\u308a\u5024<\/h3>\n<p>3\u3064\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u306e\u3069\u308c\u304b\u3092\u8fd4\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u30b9\u30c6\u30fc\u30bf\u30b9<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>\n<div>\n<div>\n<div>\u00a0<span style=\"font-family: inherit; font-size: inherit;\">pending<\/span><\/div>\n<\/div>\n<\/div>\n<\/td>\n<td>\u51e6\u7406\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>fulfilled<\/td>\n<td>\u6b63\u5e38\u7d42\u4e86\u3057\u305f\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>rejected<\/td>\n<td>\u7570\u5e38\u7d42\u4e86<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>extraReducers<\/h2>\n<p>\u975e\u540c\u671f\u95a2\u6570\u3092\u6271\u3046\u5834\u5408\u306f\u3001\u901a\u5e38\u306ereducers\u3067\u306f\u306a\u304fextraReducers\u3068\u3044\u3046\u3082\u306e\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">  extraReducers: (builder) =&gt; {\r\n    builder.addCase(\u30a2\u30af\u30b7\u30e7\u30f3\uff1acreateAsyncThunk\u3067\u5b9a\u7fa9\u3057\u305f\u30e1\u30bd\u30c3\u30c9.createAsyncThunk\u306e\u30b9\u30c6\u30fc\u30bf\u30b9, reducer\u306e\u51e6\u7406);\r\n  },<\/pre>\n<h3>builder.addCase<\/h3>\n<p>createAsyncThunk\u3067\u5b9a\u7fa9\u3057\u305f\u30e1\u30bd\u30c3\u30c9\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u306b\u3088\u3063\u3066\u51e6\u7406\u3055\u305b\u305f\u3044\u51e6\u7406\u5185\u5bb9\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<h3>action.payload<\/h3>\n<p>\u975e\u540c\u671f\u51e6\u7406\u306e\u5834\u5408\u306f\u3001createAsyncThunk\u3067\u5b9a\u7fa9\u3057\u305f\u30e1\u30bd\u30c3\u30c9\u306ereturn\u3057\u305f\u5024\u304caction.payload\u306b\u5165\u308a\u307e\u3059\u3002<\/p>\n<h2>builder\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u306f\uff1f<\/h2>\n<p>action\u3054\u3068\u306ereducer\u3092\u751f\u6210\u3059\u308b\u4ed5\u7d44\u307f\u3002\u57fa\u672c\u7684\u306b\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306ecase\u6587\u3068\u4f3c\u305f\u3088\u3046\u306a\u611f\u3058\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u30e1\u30bd\u30c3\u30c9<\/h2>\n<p>\u3061\u306a\u307f\u306b\u3001<span style=\"color: #ff0000;\"><strong>\u5fc5\u305a\u300caddCase \u2192 addMatcher \u2192 addDefaultCase\u300d\u306e\u9806\u306b\u8a18\u8ff0<\/strong><strong>\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044<\/strong><\/span>\u306e\u3067\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>addCase<\/h3>\n<p>\u7279\u5b9a\u306eaction type\u306ereducer\u3092\u5b9f\u88c5\u3059\u308b\u969b\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h4>\u7b2c\u4e00\u5f15\u6570<\/h4>\n<p>action type\u6587\u5b57\u5217 \u307e\u305f\u306f createAction\u3067\u751f\u6210\u3055\u308c\u305faction Creator\u3002<\/p>\n<h4>\u7b2c\u4e8c\u5f15\u6570<\/h4>\n<p>\u7b2c\u4e00\u5f15\u6570\u306eaction\u306b\u5bfe\u3059\u308breducer\u95a2\u6570<\/p>\n<h3>addMatcher<\/h3>\n<p>\u3042\u308b\u6761\u4ef6\u306b\u8a72\u5f53\u3059\u308baction type\u306ereducer\u3092\u5b9f\u88c5\u3059\u308b\u969b\u306b\u4f7f\u3044\u307e\u3059\u3002\uff08\u4f8b\u3048\u3070\u3001xxx\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u542b\u3080action type\u306e\u307f\u5b9f\u884c\u3059\u308b\u306a\u3069\uff09<\/p>\n<h4>\u7b2c\u4e00\u5f15\u6570<\/h4>\n<p>matcher\u95a2\u6570<\/p>\n<h4>\u7b2c\u4e8c\u5f15\u6570<\/h4>\n<p>\u7b2c\u4e00\u5f15\u6570\u306eaction\u306b\u5bfe\u3059\u308breducer\u95a2\u6570<\/p>\n<h3>addDefaultCase<\/h3>\n<p>addCase\u3067\u3082addMatcher\u306b\u3082\u8a72\u5f53\u3057\u306a\u304b\u3063\u305faction type\u306eReducer\u3092\u5b9f\u88c5\u3059\u308b\u969b\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Redux\u3067\u975e\u540c\u671f\u306e\u5834\u5408\u306eSlice\u306e\u4f5c\u308a\u65b9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 import { createSlice, createAsyncThunk } from '@reduxjs\/toolkit'; import ax [&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\/13509"}],"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=13509"}],"version-history":[{"count":9,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13509\/revisions"}],"predecessor-version":[{"id":20427,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13509\/revisions\/20427"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13509"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}