{"id":13163,"date":"2021-02-08T18:48:07","date_gmt":"2021-02-08T09:48:07","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13163"},"modified":"2023-05-27T22:16:02","modified_gmt":"2023-05-27T13:16:02","slug":"%e3%80%90react%e3%80%91%e3%80%8credux%e3%80%8d%e3%81%ae%e3%80%8cstore%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13163","title":{"rendered":"\u3010Redux\u3011\u300cStore\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>\u30b9\u30c8\u30a2\u306e\u4f5c\u6210\u624b\u9806<\/h2>\n<p>\u307e\u305a\u306f\u3001\u300csrc\/store.js\u300d\u306a\u3069\u9069\u5f53\u306a\u5834\u6240\u306bRedux\u30b9\u30c8\u30a2\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<h3>Reducer\u3092\u547c\u3073\u51fa\u3059\u3002<\/h3>\n<p>Reducer\u306fStore\u5185\u90e8\u306b\u5b58\u5728\u3059\u308b\u6982\u5ff5\u3067\u3059\u304c\u3001store\u3068\u306f\u5225\u30d5\u30a1\u30a4\u30eb\u306b\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u4e00\u822c\u7684\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import reducers from '.\/reducers';<\/pre>\n<h3>createStore\u3092\u8aad\u307f\u8fbc\u3080<\/h3>\n<p>reducers\u3092\u5f15\u6570\u3068\u3057\u3066createStore\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067Store\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { createStore } from 'redux';\r\nconst store = createStore(reducers);<\/pre>\n<h3>Store\u3092\u5916\u90e8\u304b\u3089\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/h3>\n<pre class=\"lang:default decode:true\">export default store;<\/pre>\n<h2>React\u304b\u3089\u30b9\u30c8\u30a2\u3092\u547c\u3073\u51fa\u3059\u3002<\/h2>\n<p>\u3053\u3053\u304b\u3089\u306fReact\u5074\u306e\u300csrc\/index.js\u300d\u3092\u7de8\u96c6\u3057\u307e\u3059\u3002<\/p>\n<h3>Provider\u306bStore\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3001React\u5074\u304b\u3089Store\u306e\u4e2d\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3046\u305f\u3081\u306breact-redux\u306eProvider\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">import { Provider } from \"react-redux\";<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306bApp\u3092Provider\u306e\u5b50\u8981\u7d20\u3068\u3057\u307e\u3059\u3002\uff08\u3053\u308c\u3067\u5b50\u8981\u7d20\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089Store\u306e\u60c5\u5831\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\uff09<\/p>\n<pre class=\"lang:default decode:true\">ReactDOM.render(\r\n  &lt;Provider store={store}&gt;\r\n    &lt;App \/&gt;\r\n  &lt;\/Provider&gt;,\r\n  document.getElementById('root')\r\n);<\/pre>\n<h2>\u500b\u5225\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089\u30b9\u30c8\u30a2\u3092\u547c\u3073\u51fa\u3059\u3002<\/h2>\n<p>\u4f8b\u3048\u3070\u3001App.js\u304b\u3089\u547c\u3073\u51fa\u3059\u3068\u3057\u307e\u3059\u3002<\/p>\n<h3>connect\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/h3>\n<p>react-redux\u306e<span style=\"color: #ff0000;\"><strong>connect<\/strong><\/span>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { connect } from \"react-redux\";<\/pre>\n<p>App\u3092export\u3057\u3066\u3044\u305f\u306e\u3092connect\u3092export\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<h4>\u5909\u66f4\u524d<\/h4>\n<pre class=\"lang:default decode:true\">export default App;<\/pre>\n<h4>\u5909\u66f4\u5f8c<\/h4>\n<p>App\u3092\u5f15\u6570\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">export default connect()(App);<\/pre>\n<div>\n<h3>mapStateToProps\u3068\u8a00\u3046\u95a2\u6570\u3092\u8ffd\u52a0\u3057\u3066connect\u306e\u5f15\u6570\u306b\u6e21\u3059\u3002<\/h3>\n<h4>mapStateToProps<\/h4>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306a\u95a2\u6570\u3092App.js\u5185\u306b\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<div>\n<pre class=\"lang:default decode:true\">const mapStateToProps = state =&gt; {\r\n  return {\r\n    test: state.test.value\r\n  }\r\n};<\/pre>\n<h5>mapStateToProps\u306e\u7279\u5fb4<\/h5>\n<ul>\n<li>\u305f\u3060\u4e00\u3064\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u95a2\u6570<\/li>\n<li>state\u3068\u8a00\u3046\u5f15\u6570\u306b\u306f\u81ea\u52d5\u7684\u306b\u73fe\u5728\u306eStore\u306e\u4e2d\u8eab\u304c\u4e0e\u3048\u3089\u308c\u307e\u3059\u3002<\/li>\n<li>state\u306e\u4e2d\u8eab\u3092\u500b\u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306eprops\u306b\u6e21\u3057\u3066UI\u8868\u793a\u306b\u30c7\u30fc\u30bf\u3092\u4f7f\u3046\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/li>\n<li>\u4e0a\u8a18\u4f8b\u3067\u8a00\u3048\u3070\u3001Store\u306e\u300ctest\u300d\u306b\u3088\u3063\u3066test\u3068\u8a00\u3046props\u306b\u6e21\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h4>export\u6587\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3002<\/h4>\n<p>connect\u306e\u5f15\u6570\u306b\u95a2\u6570\u300cmapStateToProps\u300d\u3092\u6e21\u3059\u3053\u3068\u306b\u3088\u3063\u3066\u500b\u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304cStore\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<div>\n<pre class=\"lang:default decode:true\">export default connect(mapStateToProps)(App);<\/pre>\n<p>\u3053\u308c\u3067UI\u5074(jsx)\u304b\u3089<span style=\"color: #ff0000;\"><strong>\u300cthis.props.test\u300d<\/strong><\/span>\u306a\u3069\u3067\u30b9\u30c8\u30a2\u306e\u5024\u3092\u53d6\u308a\u51fa\u3059\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<\/div>\n<h2>React\u304b\u3089Redux\u306eStore\u306e\u5024\u306b\u5909\u5316\u3092\u52a0\u3048\u308b\u3002<\/h2>\n<div>Action\u3092Dispatch\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/div>\n<h3>Action\u3092\u5165\u308c\u305f\u95a2\u6570\u3092\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/h3>\n<p>\u300csrc\/action.js\u300d\u306a\u3069\u3067\u5ba3\u8a00\u3057\u305fActionCreator(Action\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u95a2\u6570)\u3092React\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n<div>\n<pre class=\"lang:default decode:true\">import { aMethod, bMethod } from '.\/actions';<\/pre>\n<h3>this.props.dispatch\u3092\u4f7f\u3063\u3066Action\u3092Dispatch\u3059\u308b\u3002<\/h3>\n<p>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u306e\u307f\u3067\u5229\u7528\u3059\u308bstate\u5909\u6570\u3092\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u5ba3\u8a00\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">  constructor() {\r\n    super();\r\n    this.state = {\r\n      input: \"\"\r\n    };\r\n  }<\/pre>\n<h4>jsx\u5185<\/h4>\n<pre class=\"lang:default decode:true\">  render() {\r\n    const { input } = this.state;\r\n    return (\r\n     &lt;div&gt;\r\n       &lt;input type=\"text\" onChange={e =&gt; this.setState({input: e.target.value})}\/&gt;\r\n        &lt;button onClick={()=&gt; this.props.dispatch(aMethod(this.state.input))}&gt;\r\n          a\r\n        &lt;\/button&gt;\r\n      &lt;\/div&gt;\r\n\u3000\u3000\u3000\u3000)<\/pre>\n<p>\u300cthis.state.input\u300d\u304c\u5165\u529b\u6b04\u306e\u5185\u5bb9\uff08onChange={e =&gt; this.setState({input: e.target.value})}\u3067\u30bb\u30c3\u30c8\u3057\u3066\u3044\u308b\u3002\uff09\u3092Action\uff08a\uff09\u306b\u6e21\u3057\u3001\u3055\u3089\u306bthis.props.dispatch\u306b\u6e21\u3059\u3053\u3068\u3067Action\u3092dispatch\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3067\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5074\u304b\u3089Store\u306b\u50cd\u304d\u304b\u3051\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u30b3\u30fc\u30c9\u306e\u898b\u901a\u3057\u3092\u3088\u304f\u3059\u308b\u3002<\/h2>\n<p>mapDispatchToProps\u3068\u8a00\u3046\u95a2\u6570\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u3067\u51fa\u3066\u304f\u308b\u300caMethod\u300d\u3001\u300cbMethod\u300d\u306fActionCreator(Action\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u95a2\u6570)\u306e\u3053\u3068\u3067\u3059\u3002\u307e\u305f\u3001\u5f15\u6570\u3067\u6e21\u3057\u3066\u3044\u308btest\u306fAction\u3092\u751f\u6210\u3059\u308b\u969b\u306b\u4f7f\u7528\u3059\u308bpayload(\u5f15\u6570\uff09\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">const mapDispatchToProps = dispatch =&gt; {\r\n  return {\r\n    onAMethod(test) {\r\n      dispatch(aMethod(test))\r\n    },\r\n    onBMethod(test) {\r\n      dispatch(bMethod(test))\r\n    }\r\n  }\r\n};<\/pre>\n<p>\u6b21\u306bconnect\u306e\u7b2c\u4e8c\u5f15\u6570\u306b\u4e0e\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">export default connect(mapStateToProps, mapDispatchToProps)(App);<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067this.props\u306e\u8981\u7d20\u3068\u3057\u3066Dispatch\u3092\u884c\u3046\u305f\u3081\u306e\u95a2\u6570\u3092\u4e0e\u3048\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u4fee\u6b63\u524d<\/h3>\n<pre class=\"lang:default decode:true\">&lt;button onClick={()=&gt; this.props.dispatch(aMethod(this.state.input))}&gt;<\/pre>\n<h3>\u4fee\u6b63\u5f8c<\/h3>\n<p>\u3053\u306e\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<\/div>\n<div>\n<pre class=\"lang:default decode:true \">&lt;button onClick={()=&gt; this.props.onAMethod(this.state.input)}&gt;<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u30b9\u30c8\u30a2\u306e\u4f5c\u6210\u624b\u9806 \u307e\u305a\u306f\u3001\u300csrc\/store.js\u300d\u306a\u3069\u9069\u5f53\u306a\u5834\u6240\u306bRedux\u30b9\u30c8\u30a2\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 Reducer\u3092\u547c\u3073\u51fa\u3059\u3002 Reducer\u306fStore\u5185\u90e8\u306b\u5b58\u5728\u3059\u308b\u6982\u5ff5\u3067\u3059\u304c\u3001store\u3068\u306f\u5225\u30d5\u30a1\u30a4\u30eb\u306b\u4f5c [&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\/13163"}],"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=13163"}],"version-history":[{"count":10,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13163\/revisions"}],"predecessor-version":[{"id":20442,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13163\/revisions\/20442"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13163"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}