{"id":13043,"date":"2021-01-24T09:59:14","date_gmt":"2021-01-24T00:59:14","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13043"},"modified":"2023-05-27T22:17:46","modified_gmt":"2023-05-27T13:17:46","slug":"%e3%80%90react%e3%80%91%e3%80%8credux%e3%80%8d%e3%81%a7%e9%9d%9e%e5%90%8c%e6%9c%9f%e5%87%a6%e7%90%86%e3%82%92%e5%ae%9f%e8%a3%85%e3%81%99%e3%82%8b%e3%80%82","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13043","title":{"rendered":"\u3010redux\u3011redux-thunk"},"content":{"rendered":"<h2>redux-thunk\u3068\u306f\uff1f<\/h2>\n<h3>actionCreator\u3067\u95a2\u6570\u3092\u8fd4\u305b\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/h3>\n<p>\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3001ActionCreater\u5185\u90e8\u306e\u30ed\u30b8\u30c3\u30af\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u901a\u5e38\u3001ActionCreator\u306f\u30d4\u30e5\u30a2\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3055\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u306e\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u66f8\u304f\u306e\u306f\u4e0d\u53ef\u80fd<\/strong><\/span>\u306a\u306e\u3067\u3059\u304c\u3001redux-thunk\u3092\u4f7f\u3048\u3070\u305d\u308c\u304c\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306b\u3001redux-thunk\u306ereadme\u306eMotivation\u306b\u3082\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u304c\u3001<span style=\"color: #ff0000;\"><strong>\u300cactioncreator\u3092\u30a2\u30af\u30b7\u30e7\u30f3\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\u306e\u4ee3\u308f\u308a\u306b\u95a2\u6570\u3092\u8fd4\u3059\u300d<\/strong><\/span>\u3088\u3046\u306b\u5b9f\u88c5\u3067\u304d\u308b\u3068\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>actionCreator\u3067\u8fd4\u3059\u95a2\u6570\u306e\u5f15\u6570\u306bdispatch\u3084getState\u3092\u6301\u305f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/h3>\n<p>actionCreator\u3067\u95a2\u6570\u3092\u8fd4\u305b\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u305d\u306e\u8fd4\u3059\u95a2\u6570\u306e\u5f15\u6570\u3068\u3057\u3066dispatch\u3084getState\u3092\u6301\u305f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u306a\u306e\u3067\u3001actionCreator\u5074\u3067dispatch\u3055\u305b\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u975e\u5e38\u306b\u81ea\u7531\u5ea6\u304c\u9ad8\u3044\u306e\u3067\u3046\u307e\u304f\u8003\u3048\u306a\u3044\u3068fat\u306a\u30a2\u30af\u30b7\u30e7\u30f3\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u6ce8\u610f\u3002<\/p>\n<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>redux-thunk\u306fredux\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">yarn add redux-thunk<\/pre>\n<h2>\u7d44\u307f\u8fbc\u307f<\/h2>\n<p>react\u3067\u3042\u308c\u3070\u300csrc\/index.js\u300d\u306a\u3069\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { createStore, applyMiddleware } from 'redux';\r\nimport thunk from 'redux-thunk';<\/pre>\n<p>redux-thunk\u306f\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u306b\u306a\u308b\u306e\u3067\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3092\u9069\u7528\u3059\u308b\u305f\u3081\u306eredux\u306e\u95a2\u6570\u3067\u3042\u308b<span style=\"color: #ff0000;\"><strong>applyMiddleware<\/strong><\/span>\u3082import\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002createStore\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\u4e8c\u5f15\u6570\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">const store = createStore(reducer, applyMiddleware(thunk))<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067Redux\u306eStore\u306e\u4e2d\u306b\u975e\u540c\u671f\u51e6\u7406\u304c\u7d44\u307f\u8fbc\u307e\u308c\u307e\u3059\u3002<\/p>\n<h3>actions\u3078\u306e\u7d44\u307f\u8fbc\u307f<\/h3>\n<p>\u975e\u540c\u671f\u51e6\u7406\u306e\u5834\u5408\u306f\u3001<span style=\"color: #ff0000;\"><strong>dispatch\u306e\u547c\u3073\u51fa\u3057\u3092\u300c<\/strong><\/span><span style=\"color: #ff0000;\"><strong>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5074\u300d\u3067\u306f\u306a\u304f\u300cactionCreator\u3067\u8fd4\u3059\u95a2\u6570\u5074\u300d\u3067\u884c\u306a\u3063\u3066\u3044\u308b<\/strong><\/span>\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"lang:default decode:true\">import axios from 'axios'\r\nexport const READ_EVENTS = 'READ_EVENTS'\r\n\r\nconst ROOT_URL = 'https:\/\/xxx.com\/api\/v1'\r\nconst QUERYSTRING = '?token=token123'\r\n\r\nexport const readEvents = () =&gt; async dispatch =&gt; {\r\n  const response = await axios.get(`${ROOT_URL}\/events${QUERYSTRING}`)\r\n  dispatch({ type: 'READ_EVENTS', response })\r\n}<\/pre>\n<h2>\u306a\u305credux\u3067\u306f\u300credux thunk\u300d\u3084\u3001\u300credux saga\u300d\u306a\u3069\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u5fc5\u8981\u306a\u306e\u304b\u3002<\/h2>\n<p>Redux\u306fAction Creator\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u305f\u30a2\u30af\u30b7\u30e7\u30f3\u3092Store\u306b\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u3059\u308b\u3053\u3068\u3067\u5358\u7d14\u306a\u66f4\u65b0\u3092\u884c\u3063\u3066\u3044\u308b\u305f\u3081\u3067\u3059\u3002\u305d\u3053\u3067\u3001Redux-thunk\u306e\u3088\u3046\u306a\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067Store\u306e\u6a5f\u80fd\u3092\u62e1\u5f35\u3057\u3066\u3001\u975e\u540c\u671f\u30ed\u30b8\u30c3\u30af\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>Redux Thunk<\/h3>\n<p>\u901a\u5e38\u3001Action Creator\u306fAction\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u304c\u3001Redux Thunk\u3092\u4f7f\u7528\u3059\u308b\u3068\u300cThunk\u300d\u3068\u3044\u3046\u95a2\u6570\u3092\u8fd4\u3059\u3088\u3046\u306b\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308aAction\u306e\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u3092\u9045\u3089\u305b\u305f\u308a\u3001\u7279\u5b9a\u306e\u6761\u4ef6\u304c\u6e80\u305f\u3055\u308c\u305f\u5834\u5408\u306e\u307f\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u3092\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>Action Creator\u304cThunk\u3092\u8fd4\u3059\u3053\u3068\u3067\u3001Redux\u306eReducer\u3001React\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u76f4\u63a5\u7684\u306a\u526f\u4f5c\u7528\u3092\u8d77\u3053\u3055\u306a\u3044\u30d4\u30e5\u30a2\u306a\u72b6\u614b\u3092\u4fdd\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001Reducer\u3084React\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30c6\u30b9\u30c8\u3001\u4fdd\u5b88\u6027\u3001\u62e1\u5f35\u6027\u3001\u518d\u5229\u7528\u6027\u306a\u3069\u3092\u9ad8\u3081\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002\uff09<\/p>\n<h4>\u8fd4\u3059\u95a2\u6570<\/h4>\n<p>\u8fd4\u3059\u95a2\u6570\u306e\u5f15\u6570\u3068\u3057\u3066dispatch\u3084getState\u3092\u6301\u305f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001actionCreator\u5074\u3067\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>Redux Thunk\u306e\u6b20\u70b9<\/h4>\n<p>Action Creator\u5074\u306b\u975e\u540c\u671f\u306a\u51e6\u7406\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001Action Creator\u304c\u80a5\u5927\u5316\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>Redux Saga<\/h3>\n<p>Redux Thunk\u306e\u3088\u3046\u306bAction Creator\u304c\u80a5\u5927\u5316\u3057\u3066\u3057\u307e\u3046\u3088\u3046\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3053\u3061\u3089\u3092\u4f7f\u3046\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"redux-thunk\u3068\u306f\uff1f actionCreator\u3067\u95a2\u6570\u3092\u8fd4\u305b\u308b\u3088\u3046\u306b\u306a\u308b\u3002 \u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3001ActionCreater\u5185\u90e8\u306e\u30ed\u30b8\u30c3\u30af\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u901a\u5e38\u3001ActionCreator\u306f\u30d4\u30e5\u30a2\u306a\u30aa\u30d6\u30b8\u30a7 [&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\/13043"}],"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=13043"}],"version-history":[{"count":12,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13043\/revisions"}],"predecessor-version":[{"id":20448,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13043\/revisions\/20448"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13043"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}