{"id":13420,"date":"2021-05-04T20:02:45","date_gmt":"2021-05-04T11:02:45","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13420"},"modified":"2023-05-27T22:10:05","modified_gmt":"2023-05-27T13:10:05","slug":"%e3%80%90react%e3%80%91%e3%80%8credux-tool-kit%e3%80%8d%e3%81%ae%e5%b0%8e%e5%85%a5","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13420","title":{"rendered":"\u3010React\/Redux\u3011\u300cRedux Tool Kit\u300d\u306e\u5c0e\u5165\uff08createSlice)"},"content":{"rendered":"<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067React\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u540c\u6642\u306bRedux Tool Kit\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u5c0e\u5165\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">npx create-react-app . --template redux<\/pre>\n<h2>\u8d77\u52d5<\/h2>\n<pre class=\"lang:default decode:true \">npm start<\/pre>\n<p>\u8d77\u52d5\u3059\u308b\u3068\u30d6\u30e9\u30a6\u30b6\u3067\u753b\u9762\u304c\u7acb\u3061\u4e0a\u304c\u308a\u901a\u5e38\u306ecreate-react-app\u3068\u306f\u5225\u306eRedux\u306b\u3088\u308b\u7c21\u6613\u7684\u306a\u5b9f\u88c5\u304c\u884c\u308f\u308c\u305f\u30ab\u30a6\u30f3\u30bf\u30fc\u30a2\u30d7\u30ea\u304c\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u5165\u529b\u5024\u3092\u5358\u7d14\u306b\u30ab\u30a6\u30f3\u30c8\u3092\u884c\u3046\u3002<\/li>\n<li>\u975e\u540c\u671f\u30671\u79d2\u5f8c\u306b\u30ab\u30a6\u30f3\u30c8\u3092\u884c\u3046\u3002<\/li>\n<\/ul>\n<h2>Slice\u306e\u4f5c\u6210<\/h2>\n<p>xxxSlice.js\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002RTK\u306ecreateSlice\u3068\u3044\u3046\u69cb\u6587\u3092\u4f7f\u3063\u3066Slice\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\uff08xxx\u306f\u540d\u524d\uff09\u3001\u306a\u304aslice\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u306a\u3069\u306f\u5b9a\u7fa9\u3057\u305f\u3089\u5168\u3066export\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { createSlice } from '@reduxjs\/toolkit';\r\n\r\nexport const xxxSlice = createSlice({\r\n  name: 'xxx',\r\n  initialState: { value: 0 },\r\n  reducers: {\r\n    increment(\u30a2\u30af\u30b7\u30e7\u30f3\u540d): (state,action) =&gt; {\r\n      state.value += 1;\r\n    },\r\n  },\r\n});\r\n\r\n\/\/ slice\u3067\u5b9a\u7fa9\u3057\u305faction\u306f\u5168\u3066export\u3059\u308b\u3002\r\nexport const { increment(\u30a2\u30af\u30b7\u30e7\u30f3\u540d) } = xxxSlice.actions;\r\n\/\/ useSelector\u3067\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089\u53c2\u7167\u3057\u305f\u3044state\u306e\u5024\u3092\u4ee3\u5165\r\nexport const selectXXX = (state) =&gt; state.xxx.value;\r\n\/\/ reducer\u3092export\u3059\u308b\u3002\r\nexport default xxxSlice.reducer;<\/pre>\n<h3>\u3061\u306a\u307f\u306b\u3001createSlice\u3068\u306f\uff1f<\/h3>\n<p>reducer\u3092\u4f5c\u6210\u3059\u308b\u3060\u3051\u3067\u81ea\u52d5\u7684\u306baction type\u3084\u3001action creator\u3082\u751f\u6210\u3057\u3066\u304f\u308c\u307e\u3059\u3002\u3064\u307e\u308a\u3001<span style=\"color: #ff0000;\"><strong>\u5f93\u6765\u306eredux\u3067\u3044\u3046\u3068\u3053\u308d\u306ereducer\u3068action\u3092\u4e00\u767a\u3067\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/strong><\/span><\/p>\n<h4>reducers\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h4>\n<p>reducers\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u305f\u3089\u30ad\u30fc\u304b\u3089\u81ea\u52d5\u7684\u306baction type\u3082\u751f\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>Store\u306e\u4f5c\u6210<\/h2>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306bconfigureStore\u3068\u3044\u3046ReduxToolKit\u306e\u95a2\u6570\u3067Slice\u540c\u58eb\u3092\u7d50\u5408\u3057\u3066\u4e00\u3064\u306eStore\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">import { configureStore } from '@reduxjs\/toolkit';\r\nimport xxxReducer from '..\/features\/xxx\/xxxSlice';\r\n\r\nexport const store = configureStore({\r\n  reducer: {\r\n    xxx: xxxReducer,\r\n  },\r\n});<\/pre>\n<h3>Store\u306e\u767b\u9332<\/h3>\n<p>store\u306findex.js\u306a\u3069\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u767b\u9332\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">    &lt;Provider store={store}&gt;\r\n      &lt;App \/&gt;\r\n    &lt;\/Provider&gt;<\/pre>\n<h2>useSelector<\/h2>\n<p>store\u304b\u3089\u72b6\u614b\u3092\u53c2\u7167\u3057\u305f\u3044\u5834\u5408\u306fuseSelector\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">import React from 'react';\r\nimport { useSelector } from 'react-redux';\r\nimport { \u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u305fstate } from '.\/xxxSlice';\r\n\r\nconst \u4f7f\u3044\u305f\u3044\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u540d = () =&gt; {\r\n  const state\u540d = useSelector(\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u305fstate);\r\n  return (\r\n    &lt;&gt;\r\n     state\u3092\u4f7f\u3063\u305f\u51e6\u7406\r\n    &lt;\/&gt;\r\n  );\r\n};\r\n\r\nexport default \u4f7f\u3044\u305f\u3044\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u540d;\r\n<\/pre>\n<h2>useDispatch<\/h2>\n<p>Store\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u547c\u3073\u51fa\u3057\u305f\u3044\u6642\u306fuseDispatch\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">import React from 'react';\r\nimport { useDispatch } from 'react-redux';\r\nimport { \u30a2\u30af\u30b7\u30e7\u30f3\u540d } from '.\/xxxSlice';\r\n\r\nconst \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u540d = () =&gt; {\r\n  const dispatch = useDispatch();\r\n\r\n  return (\r\n    &lt;div&gt;\r\n      &lt;button onClick={() =&gt; dispatch(\u30a2\u30af\u30b7\u30e7\u30f3\u540d())}&gt;\u30a2\u30af\u30b7\u30e7\u30f3&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n};\r\n\r\nexport default \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u540d;\r\n<\/pre>\n<div>\n<h2>createAsyncThunk<\/h2>\n<div>\u5f93\u6765\u306eRedux\u3092\u4f7f\u7528\u3057\u3066\u3044\u305f\u5834\u5408\u306fredux-thunk\u3068\u3044\u3046\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u306a\u3051\u308c\u3070\u975e\u540c\u671f\u95a2\u6570\u3092\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u306a\u304b\u3063\u305f\u3067\u3059\u3002<\/div>\n<div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb \u30c7\u30d5\u30a9\u30eb\u30c8\u3067React\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u540c\u6642\u306bRedux Tool Kit\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u5c0e\u5165\u3067\u304d\u307e\u3059\u3002 npx create-react-app . --template redux \u8d77\u52d5 npm st [&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\/13420"}],"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=13420"}],"version-history":[{"count":15,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13420\/revisions"}],"predecessor-version":[{"id":20428,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13420\/revisions\/20428"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13420"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}