{"id":1062,"date":"2023-03-12T10:19:25","date_gmt":"2023-03-12T02:19:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1062"},"modified":"2023-04-29T11:47:07","modified_gmt":"2023-04-29T03:47:07","slug":"react-redux-basic-usage-process","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/react-redux-basic-usage-process\/","title":{"rendered":"React+Redux\u57fa\u672c\u4f7f\u7528\u6d41\u7a0b"},"content":{"rendered":"<h2>Redux\u7b80\u4ecb<\/h2>\n<p>Redux\u662fReact\u751f\u6001\u4e2d\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\u3002React\u63d0\u51fa\u5c06\u5c55\u793a\u7ec4\u4ef6\u4e0e\u5bb9\u5668\u7ec4\u4ef6\u5206\u79bb\u7684\u601d\u60f3\uff0c\u964d\u4f4e\u4e86React\u4e0eRedux\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u3002<\/p>\n<p><!-- more --><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u524d\u7aef\/React+Redux.png\" alt=\"React+Redux\" \/><\/p>\n<ul>\n<li><code>Store<\/code>\uff1a\u6574\u4e2a\u5e94\u7528\u7684\u6570\u636e\u5b58\u50a8\u4e2d\u5fc3\uff0c\u96c6\u4e2d\u5927\u90e8\u5206\u9875\u9762\u9700\u8981\u7684\u72b6\u6001\u6570\u636e\uff1b<\/li>\n<li><code>ActionCreators<\/code>\uff1aview \u5c42\u4e0edata\u5c42\u7684\u4ecb\u8d28\uff1b<\/li>\n<li><code>Reducer<\/code>\uff1a\u63a5\u6536action\u5e76\u66f4\u65b0Store\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u6d41\u7a0b\u662f\u7528\u6237\u901a\u8fc7\u754c\u9762\u7ec4\u4ef6\u89e6\u53d1ActionCreator\uff0c\u643a\u5e26Store\u4e2d\u7684\u65e7State\u4e0eAction\u6d41\u5411Reducer\uff0cReducer\u8fd4\u56de\u65b0\u7684state\uff0c\u5e76\u66f4\u65b0\u754c\u9762\u3002<\/p>\n<h2>\u6848\u4f8b\u5b9e\u73b0<\/h2>\n<h3>\u4f9d\u8d56\u5e93<\/h3>\n<pre><code>yarn add prop-types\nyarn add react-redux\nyarn add redux\nyarn add redux-thunk<\/code><\/pre>\n<h3>\u9875\u9762\u7ec4\u4ef6<\/h3>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7eafReact\u4ee3\u7801 \uff0c\u7ed3\u6784\u6e05\u6670<\/p>\n<p>component\/person.js<\/p>\n<pre><code class=\"language-javascript\">import React, {Component} from &#039;react&#039;;\nimport PropTypes from &#039;prop-types&#039;\n\nclass Person extends Component {\n    \/\/\u58f0\u660e\u5c5e\u6027\n    static propTypes = {\n        name: PropTypes.string.isRequired,\n        nameCreator: PropTypes.func.isRequired,\n        age: PropTypes.number.isRequired,\n        ageCreator: PropTypes.func.isRequired,\n        nameAsync: PropTypes.func.isRequired\n    }\n    \/\/\u70b9\u51fb\u4e8b\u4ef6\n    handlerNameFunc = () =&gt; {\n        const inputName = this.refs.inputValueName.value;\n        this.props.nameCreator(inputName);\n    }\n    handlerAgeFunc = () =&gt; {\n        const inputAge = this.refs.inputValueAge.value;\n        this.props.ageCreator(inputAge);\n    }\n    handlerAsyncFunc = () =&gt; {\n        const inputName = this.refs.inputValueName.value;\n        this.props.nameAsync(inputName);\n    }\n\n    \/\/\u6e32\u67d3\u754c\u9762\n    render() {\n        const {name, age} = this.props;\n        return (\n            &lt;div&gt;\n                &lt;header className=&quot;App-header&quot;&gt;\n                    &lt;h1 className=&quot;App-title&quot;&gt;Welcome to React&lt;\/h1&gt;\n                &lt;\/header&gt;\n                &lt;label&gt; {name} &lt;\/label&gt;&lt;br\/&gt;\n                &lt;input ref=&quot;inputValueName&quot;\/&gt;\n                &lt;button onClick={this.handlerNameFunc}&gt;Name Confirm&lt;\/button&gt;\n                &lt;button onClick={this.handlerAsyncFunc}&gt;Async Confirm&lt;\/button&gt;\n                &lt;br\/&gt;\n\n                &lt;label&gt; {age} &lt;\/label&gt;&lt;br\/&gt;\n                &lt;input ref=&quot;inputValueAge&quot;\/&gt;\n                &lt;button onClick={this.handlerAgeFunc}&gt;Age Confirm&lt;\/button&gt;\n                &lt;br\/&gt;\n                &lt;br\/&gt;\n            &lt;\/div&gt;\n        );\n    }\n}\n\nexport default Person;<\/code><\/pre>\n<h3>ActionCreator<\/h3>\n<h4>redux\/action-type.js<\/h4>\n<p>ActionCreator\u4f1a\u4f20\u8fbe\u7528\u6237\u7684\u64cd\u4f5c\u4fe1\u606f\u4ee5\u53ca\u4e00\u4e9b\u6570\u636e\u3002\u5b9a\u4e49\u4e00\u4e9b\u5e38\u91cf\u4f9b\u6211\u4eec\u4f7f\u7528\uff0c\u8fd9\u91cc\u5c31\u4e24\u79cd\u64cd\u4f5c\uff0c\u4e00\u662f\u6dfb\u52a0\u540d\u5b57\uff0c\u4e8c\u662f\u6dfb\u52a0\u5e74\u9f84\uff0c\u5b9e\u9645\u90fd\u4e00\u6837\u3002\u4e3a\u4e86\u540e\u9762\u5b9e\u73b0reducer\u7684\u5408\u5e76\u5f3a\u884c\u5199\u4e86\u4fe9\u3002\u8fd9\u4e9b\u5e38\u91cf\u4e00\u822c\u90fd\u5b9a\u4e49\u5728actionTpye\u6587\u4ef6\u4e2d\u3002<\/p>\n<pre><code class=\"language-javascript\">export const NAME = &#039;NAME&#039;\nexport const AGE = &#039;AGE&#039;<\/code><\/pre>\n<h4>redux\/actions.js<\/h4>\n<p>\u63a5\u7740\u5c31\u662fActionCreator \uff0c\u5b9a\u4e49\u4e86\u4e00\u4e9b\u64cd\u4f5c\u7c7b\u578b\uff0c\u544a\u8bc9store\u81ea\u5df1\u662f\u5e72\u4ec0\u4e48\u7684\uff0c\u9700\u8981\u4ec0\u4e48\u6837\u7684\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-javascript\">import {NAME, AGE} from &quot;.\/action-type&quot;;\n\n\/\/\u5305\u542b\u6240\u6709\u7684action creator\nexport const nameCreator = (name) =&gt; ({type: NAME, data: name})\nexport const ageCreator = (age) =&gt; ({type: AGE, data: age})\nexport const nameAsync = (name) =&gt; {\n    return dispatch =&gt; {\n        setTimeout(() =&gt; {\n            dispatch(nameCreator(name))\n        }, 2000);\n    }\n}<\/code><\/pre>\n<h4>redux\/reducers.js<\/h4>\n<p>Reducer\u4f1a\u63a5\u6536\u5230action\u7684\u4fe1\u606f\u3002\u7136\u540e\u8fdb\u884c\u72b6\u6001\uff08\u6570\u636e\uff09\u7684\u5904\u7406\uff0c\u76f8\u5f53\u4e8ereact\u4e2d\u7684<code>setState()<\/code>\u7684\u529f\u80fd\u3002\u5982\u679c\u6709\u591a\u4e2areducer \uff0c\u53ef\u4ee5\u4f7f\u7528<code>combineReducers<\/code>\u65b9\u6cd5\u5c06\u5176\u5408\u5e76\uff0c\u5e76\u66b4\u9732\u51fa\u53bb\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/\u5305\u542bn\u4e2areducer\u51fd\u6570\u7684\u6a21\u5757\nimport {NAME, AGE} from &#039;.\/action-type&#039;\nimport {combineReducers} from &#039;redux&#039;\n\nfunction name(state = &#039;initRedux&#039;, action) { \/\/\u5f62\u53c2\u9ed8\u8ba4\u503c\n    switch (action.type) {\n        case NAME:\n            return action.data\n        default:\n            return state\n    }\n}\n\nfunction age(state = 0, action) {\n    switch (action.type) {\n        case AGE:\n            return action.data\n        default:\n            return state\n    }\n}\n\nexport const finalReducer = combineReducers({\n    name, age\n})<\/code><\/pre>\n<p>\u5176\u4e2dstate=&#8217;initRedux&#8217; \u3001state=0 \u76f8\u5f53\u4e8e\u6211\u4eec\u5728React\u7ec4\u4ef6\u5185\u90e8\u521d\u59cb\u5316state<\/p>\n<h4>redux\/store.js<\/h4>\n<p>\u4e00\u5207\u64cd\u4f5c\u8fd8\u662f\u57fa\u4e8eStore \u7684\u3002\u7c7b\u4f3c\u4e8e\u4e2d\u592e\u96c6\u6743\u3002\u6240\u4ee5\u8fd8\u8981\u628aStore\u5efa\u7acb\u51fa\u6765<\/p>\n<pre><code class=\"language-javascript\">import {createStore, applyMiddleware} from &#039;redux&#039;\nimport {finalReducer} from &#039;.\/reducers&#039;\nimport thunk from &#039;redux-thunk&#039;\n\n\/\/\u751f\u6210store\u5bf9\u8c61\nconst store = createStore(finalReducer, applyMiddleware(thunk)); \/\/\u5185\u90e8\u4f1a\u7b2c\u4e00\u6b21\u8c03\u7528reducer\u51fd\u6570\uff0c\u5f97\u5230\u521d\u59cbstate\n\nexport default store<\/code><\/pre>\n<p>\u56e0\u4e3areducer\u4f1a\u66f4\u65b0Store\u4e2d\u7684\u72b6\u6001\uff08\u6570\u636e\uff09\uff0c\u6240\u4ee5\u9700\u8981\u5f15\u5165reducer\uff0c\u5e76\u521b\u5efastore<\/p>\n<p>\u81f3\u6b64\uff0c\u6d41\u7a0b\u56fe\u7ed3\u675f\u3002\u4e0d\u8fc72\u30013\u30014\u90fd\u662fredux\u4e2d\u8d1f\u8d23\u63a5\u7ba1React\u72b6\u6001\u7684\u529f\u80fd\u30021\u662fReact\u8d1f\u8d23\u5c55\u793a\u7684\u7ec4\u4ef6\u3002\u4e24\u8005\u5e76\u6ca1\u5565\u5173\u7cfb\u3002\u65e2\u7136\u6709\u4e86\u5c55\u793a\u7ec4\u4ef6\uff0c\u63a5\u4e0b\u6765\u5c31\u8981\u6709\u5bb9\u5668\u7ec4\u4ef6\uff0c\u4e5f\u5c31\u662f\u80fd\u591f\u5c06React\u4e0eredux\u76f8\u5173\u8054\u7684\u4e00\u4e2a\u7ec4\u4ef6\u3002<\/p>\n<h3>\u5bb9\u5668\u7ec4\u4ef6<\/h3>\n<p>containers\/redux-app.js<\/p>\n<pre><code class=\"language-javascript\">import {connect} from &#039;react-redux&#039;\nimport {nameCreator, ageCreator, nameAsync} from &#039;..\/redux\/actions&#039;\nimport Person from &#039;..\/component\/person&#039;\n\nexport default connect(\n    state =&gt; ({\n        name: state.name,\n        age: state.age\n    }),\n    {nameCreator, ageCreator, nameAsync}\n)(Person)<\/code><\/pre>\n<p>\u8fd9\u91cc\u7528\u5230<code>react-redux<\/code>\u4e2d\u7684<code>connect<\/code>\uff0c\u53ef\u4ee5\u5c06React\u4e0eredux\u5173\u8054\u8d77\u6765\u3002Person\u5c31\u662f\u7b2c\u4e00\u6b65\u5199\u7684\u7ec4\u4ef6\u540d\uff0cstate\u4e2d\u5173\u8054\u4e86React\u4e2d\u7684\u5c5e\u6027\u3002<\/p>\n<h3>\u6dfb\u52a0store<\/h3>\n<p>src\/index.js<\/p>\n<pre><code class=\"language-javascript\">import React from &#039;react&#039;;\nimport ReactDOM from &#039;react-dom&#039;;\nimport &#039;.\/index.css&#039;;\nimport {Provider} from &#039;react-redux&#039;\nimport ReduxApp from &#039;.\/redux\/containers\/redux-app&#039;\nimport store from &#039;.\/redux\/redux\/store&#039;\n\nReactDOM.render((\n    \/\/\u4f7f\u7528Provider \u7ec4\u4ef6\u5c06APP\u4e3b\u7ec4\u4ef6\u5305\u88f9\u4f4f\uff0c\u8fd9\u6837\u5185\u90e8\u7ec4\u4ef6\u90fd\u6709Store\u79cd\u63d0\u4f9b\u7684\u5c5e\u6027\n    &lt;Provider store={store}&gt;\n        &lt;ReduxApp\/&gt;\n    &lt;\/Provider&gt;\n), document.getElementById(&#039;root&#039;));<\/code><\/pre>\n<h3>\u6587\u4ef6\u7ed3\u6784<\/h3>\n<ul>\n<li><code>redux<\/code>\uff1a\u96c6\u4e2d\u7ba1\u7406\u72b6\u6001\uff08\u6570\u636e\uff09<\/li>\n<li><code>component<\/code>\uff1a\u4e13\u6ce8\u4e8eReact \u5c55\u793a\u7ec4\u4ef6\u90e8\u5206<\/li>\n<li><code>containers<\/code>\uff1a\u96c6\u4e2d\u5904\u7406React\u4e0eredux\u4ea4\u4e92\u7684\u90e8\u5206<\/li>\n<\/ul>\n<p>\u6b64\u5916\uff0credux\u8fd8\u53ef\u4ee5\u5904\u7406\u4e00\u4e9b\u5f02\u6b65\u8bf7\u6c42\uff0c\u53ef\u4ee5\u505a\u5230data\u548cview\u7684\u7ba1\u7406\u5206\u79bb\uff0c\u589e\u5f3a\u4e86\u5de5\u7a0b\u7ed3\u6784\u7684\u53ef\u8bfb\u6027\u4e0e\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h2>\u6df1\u5165\u7406\u89e3Redux<\/h2>\n<h3>Action\u4e0eReducer\u7684\u8be6\u7ec6\u7406\u89e3<\/h3>\n<p>\u5148\u56de\u987e\u4e00\u4e0b\u4e0d\u4f7f\u7528Redux\u7684\u60c5\u51b5\u4e0b\uff0cReact\u7684Class\u8be5\u600e\u4e48\u5199<\/p>\n<pre><code class=\"language-javascript\">import React, {Component} from &#039;react&#039;;\n\nclass Test extends Component {\n  constructor(props){\n    super(props);\n    state = {\n      nickname: &#039;&#039;\n    }\n  }\n  handlerFunc = () =&gt; {\n    const inputName = this.refs.inputValue.value;\n    this.setState({\n      nickname: inputName\n    })\n  }\n  render() {\n    const {nickname} = this.state;   \n    return (\n      &lt;div&gt;\n        &lt;label&gt; {nickname} &lt;\/label&gt;&lt;br\/&gt;\n        &lt;input ref=&quot;inputValue&quot; \/&gt;&lt;br\/&gt;\n        &lt;button onClick={this.handlerFunc}&gt;confirm&lt;\/button&gt;&lt;br\/&gt;\n      &lt;\/div&gt;\n    );\n  }\n}<\/code><\/pre>\n<p>\u4e3b\u8981\u6d41\u7a0b\uff1a<code>constructor<\/code>\u91cc\u521d\u59cb\u5316\u72b6\u6001\uff0c\u7136\u540e\u6e32\u67d3\u7ec4\u4ef6\uff0c<code>onClick<\/code>\u91cc\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4e8b\u4ef6\u51fd\u6570\u4e2d<code>setState()<\/code>\u4e00\u4e0b\uff0c\u6b64\u65f6\u5f97\u5230\u65b0\u7684\u72b6\u6001\uff0c\u5e76\u4f7f\u865a\u62dfDOM\u91cd\u65b0\u6e32\u67d3\uff0c\u6700\u7ec8Label\u663e\u793a\u6211\u4eec\u8f93\u5165\u7684\u503c\u3002<\/p>\n<p>\u7136\u540e\u770b\u770b\u4f7f\u7528Redux\u540e\uff0c\u7ec4\u4ef6\u662f\u600e\u4e48\u5199\u7684\uff1a<\/p>\n<pre><code class=\"language-javascript\">import React, {Component} from &#039;react&#039;;\nimport PropTypes from &#039;prop-types&#039;\nclass Test extends Component {\n  static propTypes = {\n    nickname:PropTypes.string.isRequired,\n    addName:PropTypes.func.isRequired\n  }\n  handlerFunc = () =&gt; {\n    const inputName = this.refs.inputValue.value;\n    this.props.addName(inputName);\n  }\n  render() {\n    const {nickname} = this.props;   \n    return (\n      &lt;div&gt;\n        &lt;label&gt; {nickname} &lt;\/label&gt;&lt;br\/&gt;\n        &lt;input ref=&quot;inputValue&quot; \/&gt;&lt;br\/&gt;\n        &lt;button onClick={this.handlerFunc}&gt;confirm&lt;\/button&gt;\n      &lt;\/div&gt;\n    );\n  }\n}<\/code><\/pre>\n<p>\u89c2\u5bdf\u4e24\u79cd\u5199\u6cd5\uff0c\u53d1\u73b0\uff1a<br \/>\n\uff081\uff09\u521d\u59cb\u5316\u72b6\u6001\u6ca1\u6709\u4e86<br \/>\n\uff082\uff09<code>setState()<\/code>\u4e5f\u79fb\u9664\u4e86\uff0c\u7136\u540e\u901a\u8fc7\u5c5e\u6027\u7ed3\u6784\u8d4b\u503c<\/p>\n<p>\u603b\u4e4b\uff0c\u72b6\u6001\u7ba1\u7406\u65b9\u9762\u7684\u4e1c\u897f\u90fd\u88ab\u79fb\u9664\u3002\u56e0\u4e3a\u91c7\u7528Redux\u6846\u67b6\uff0c\u672c\u8eab\u5c31\u662f\u8981\u8ba9\u5176\u63a5\u7ba1\u72b6\u6001\u7684\uff0c\u6240\u4ee5\uff0credux\u6846\u67b6\u4e0b\u7684React\u4ee3\u7801\u5c31\u5f88\u597d\u7406\u89e3\u4e86\u3002\u5c31\u662freact\u7ec4\u4ef6\u91cc\u9762\u662f\u6ca1\u6709State\u76f8\u5173\u7684\u4e1c\u897f\uff08\u5f53\u7136\uff0c\u7279\u6b8a\u60c5\u51b5\u4e0b\u8fd8\u662f\u9700\u8981\u7528\u7684\u7ec4\u4ef6\u81ea\u5df1\u7684\u72b6\u6001\u7ba1\u7406\u7684\uff09\u3002redux\u6240\u8981\u505a\u7684\u5c31\u662f\uff0c\u4f20\u9012\u52a8\u4f5c\uff08\u6309\u94ae\u70b9\u51fb\uff09\u6216\u8005\u8bf7\u6c42\uff08\u8bf7\u6c42\u6570\u636e\uff09\uff0c\u5e76\u6839\u636e\u81ea\u8eab\u5c5e\u6027\u5411HTML\u4e2d\u586b\u5165\u6570\u636e\u3002\u8fd9\u91cc\u7528\u5230\u4e86<code>prop-types<\/code>\u8fd9\u4e2a\u5e93\u3002\u6765\u8fdb\u884c\u5c5e\u6027\u7684\u6821\u9a8c\u3002<\/p>\n<p>\u4e4b\u540e\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff0c<code>setState()<\/code>\u65b9\u6cd5\u5728Redux\u90a3\u8fb9\u88ab\u62c6\u5206\u6210\u4e24\u6b65\uff0c\u73b0\u5728Action\u4e2d\uff0c\u628a\u83b7\u53d6\u5230\u7684\u503c\uff0c\u62fc\u88c5\u6210\u4e00\u4e2aObject\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">import { NAME } from &quot;.\/action-type&quot;;\n\n\/\/\u5305\u542b\u6240\u6709\u7684action creator\nexport const addName = (name) =&gt; ({type:NAME, data:name})<\/code><\/pre>\n<p>\u8fd9\u91cc\u9762\u7684addName \u5c31\u662fReact\u4ee3\u7801\u91cc\u6309\u94ae\u89e6\u53d1\u7684\u65b9\u6cd5\u4e2d\u8981\u6267\u884c\u7684\u51fd\u6570\uff0cname \u5c31\u662f\u6211\u4eec\u6216\u5f97\u5230\u7684\u8f93\u5165\u6846\u91cc\u7684\u503c\u3002<\/p>\n<p>\u63a5\u7740redux\u5e2e\u6211\u4eec\u505a\u7684\u5c31\u662f\u7528Store\u4e2d\u63d0\u4f9b\u7684<code>dispatch<\/code>\u65b9\u6cd5\uff0c\u628a\u8fd9\u4e2a\u52a8\u4f5c\u4f20\u5230reducer\u90a3\u53bb\uff0c\u7136\u540e\u6765\u770b\u770bReducer\u505a\u4e86\u4ec0\u4e48\uff1a<\/p>\n<pre><code class=\"language-javascript\">import {NAME} from &#039;.\/action-type&#039;\nconst initialState = {\n  name: &#039;Joe.Ye&#039;\n}\nconst name = (state=initialState, action) =&gt; { \/\/\u5f62\u53c2\u9ed8\u8ba4\u503c\n  switch(action.type){\n    case NAME:\n      return action.data\n    default:\n      return state\n  }\n}<\/code><\/pre>\n<p>Reducer\u53ef\u4ee5\u7c7b\u522b\u7406\u89e3\u4e3a\uff1a\u65e2\u5145\u5f53\u4e86<code>setState()<\/code>\u7684\u89d2\u8272\uff0c\u4e5f\u8d1f\u8d23\u4e86\u521d\u59cb\u5316state\u7684\u4efb\u52a1<\/p>\n<p>\u6240\u4ee5\uff0creducer\u5728case\u5230NAME\u8fd9\u4e2a\u52a8\u4f5c\u4e4b\u540e\uff0c\u5c31\u628a\u4eceaction\u4f20\u6765\u7684\u503c\uff0c\u505a\u4e86\u4e00\u4e2a\u76f8\u5f53\u4e8esetState()\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-javascript\">this.setState({\n  name: action.data\n})<\/code><\/pre>\n<p>\u8981\u662f\u4e00\u5f00\u59cb\u6ca1\u6709\u89e6\u53d1\u4efb\u4f55\u52a8\u4f5c\uff0c\u5219Store\u4f1a\u5e2e\u6211\u4eec\u5728\u7a0b\u5e8f\u8fd0\u884c\u4e4b\u521d\u6267\u884c\u4e00\u904dReducer\uff0c\u4f7f\u7528\u521d\u59cb\u503cinitialState<\/p>\n<h3>\u5c55\u793a\u7ec4\u4ef6\u4e0e\u5bb9\u5668\u7ec4\u4ef6<\/h3>\n<p>redux\u5199\u7684\u4ee3\u7801\u5355\u62ce\u51fa\u6765\u4e0ereact\u4ee3\u7801\u662f\u6ca1\u5565\u8054\u7cfb\u7684\u3002\u5199\u5b8c\u4e0a\u9762\u7684\u4ee3\u7801\u540e\uff0c\u4e00\u8fd0\u884c\u5c31\u4f1a\u62a5\u9519\uff0c\u4e00\u5927\u5806undefined\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u8981\u4e00\u4e2aconnect\u6765\u8fde\u63a5Redux\u548cReact\u3002\u73b0\u5728\u6709\u8fd9\u4e48\u4e00\u4e2a\u5e93<code>react-redux<\/code>\uff0c\u4f7f\u7528\u91cc\u9762\u7684<code>connect<\/code>\u5373\u53ef\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">import React from &#039;react&#039;\nimport {connect} from &#039;react-redux&#039;\nimport {addName} from &#039;..\/redux\/actions&#039;\nimport Test from &#039;..\/component\/Test&#039;\n\nexport default connect(\n  state =&gt; ({\n    nickname: state.name\n  }),\n  {addName}\n)(Test)<\/code><\/pre>\n<p>\uff081\uff09\u5bb9\u5668\u7ec4\u4ef6<\/p>\n<p>\u8fd9\u6837\u5c31\u5199\u597d\u4e86\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u5b98\u65b9\u79f0\u4e4b\u4e3a\u5bb9\u5668\u7ec4\u4ef6\uff0c\u5c31\u662f\u5f15\u5165\u4e4b\u524d\u5199\u597d\u7684React\u7ec4\u4ef6Test\uff0c\u518d\u5f15\u5165\u4e4b\u524daction\u548creducer\u91cc\u7684\u52a8\u4f5c\u3001\u72b6\u6001\uff0c\u8fdb\u884c\u4e00\u4e2a\u5c01\u88c5\uff0c\u8fd9\u6837\u5c31\u628aTest\u91cc\u4e8b\u4ef6\u51fd\u6570\u4e0e\u5c5e\u6027\u5339\u914d\u5230\u4e86\u4e00\u8d77\u3002<\/p>\n<p>\u5176\u4e2dnickname\u5bf9\u5e94Test\u91cc\u9762\u7684nickname\u5c5e\u6027\uff0c<code>state.name<\/code>\u5c31\u662freducer\u91cc\u5b9a\u4e49\u7684\u90a3\u4e2aname\u3002\u5230\u65f6\u5019Test\u7ec4\u4ef6\u91cc\u66f4\u65b0nickname\u7684\u503c\uff0c\u5c31\u662f<code>state.name<\/code>\u7ed9\u8d4b\u4e0a\u53bb\u7684\u3002\u5c5e\u6027\u72b6\u6001\u5339\u914d\u5b8c\u4e4b\u540e\u8981\u5339\u914d\u52a8\u4f5c\uff0cTest\u4e2d\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u6267\u884c\u7684addName\uff0c\u5c31\u5339\u914daction\u91cc\u7684addName\u3002\u8fd9\u6837\uff0c\u70b9\u51fb\u6309\u94ae\u4e4b\u540e\uff0c\u6570\u636e\u5c31\u4f1a\u901a\u8fc7action\u4f20\u5230Reducer\u90a3\u53bb\u505a\u7c7b\u4f3c\u4e8esetState()\u8fd9\u6837\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\uff082\uff09\u5c55\u793a\u7ec4\u4ef6 <\/p>\n<p>\u5c31\u662fTest\u90a3\u4e2a\u7ec4\u4ef6\uff0c\u91cc\u9762\u4e00\u822c\u4e0d\u5305\u542b\u4efb\u4f55Redux\u76f8\u5173\u7684\u4e1c\u897f\uff0c\u7eaf\u51c0\u7684React\u7ec4\u4ef6\uff0c\u79f0\u4e4b\u4e3a\u5c55\u793a\u7ec4\u4ef6\u3002\u53ef\u4ee5\u7406\u89e3\u4e3a\u5bb9\u5668\u7ec4\u4ef6\uff08\u5c01\u88c5\u7ec4\u4ef6\uff09\u7684\u4e00\u90e8\u5206<\/p>\n<h3>\u5f02\u6b65<\/h3>\n<p>\u4e00\u4e2aweb\u5e94\u7528\uff0c\u5927\u591a\u6570\u90fd\u9700\u8981\u505a\u8bf7\u6c42\u64cd\u4f5c\u7684\u3002\u800c\u5728<code>React + Redux<\/code>\u8fd9\u6837\u7684\u4e00\u4e2a\u7ed3\u6784\u4e2d\uff0c\u5f02\u6b65\u8bf7\u6c42\u64cd\u4f5c\u6700\u5408\u9002\u7684\u4f4d\u7f6e\u5c31\u662f\u653e\u5230Redux\u4e2d\u7684action\u4e2d\u3002\u9996\u5148\uff0c\u5c55\u793a\u7ec4\u4ef6\uff0c\u5373Test\u7ec4\u4ef6\uff0c\u8fd9\u4e2a\u662fview\u5c42\u3002view\u5c42\u663e\u7136\u4e0d\u9002\u5408\u63ba\u6742\u5f02\u6b65\u8bf7\u6c42\u7684\u3002\u7136\u540ereducer\u5176\u5b9e\u662f\u5b9e\u73b0\u7684\u7c7b\u4f3c\u4e8e<code>setState()<\/code>\u7684\u4f5c\u7528\u3002\u6240\u4ee5\u5728Redux\u4e2d\u7684Action\u4e2d\u505a\u5f02\u6b65\u8bf7\u6c42\u662f\u6bd4\u8f83\u5408\u9002\u7684\u3002action\u5176\u5b9e\u4e5f\u662f\u4e2a\u4f20\u63a5\u6570\u636e\u7684\u8f7d\u4f53\u3002<\/p>\n<p>\u65e2\u7136\u9009\u62e9\u5728Redux\u4e2d\u8fdb\u884c\u5f02\u6b65\u8bf7\u6c42\uff0c\u5c31\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u5e93<code>react-thunk<\/code>\uff0c\u6765\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u5f02\u6b65\u8bf7\u6c42\u3002\u9996\u5148\u8fdb\u884c\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-javascript\">import {createStore, applyMiddleware} from &#039;redux&#039;\nimport {finalReducer} from &#039;.\/reducers&#039;\nimport thunk from &#039;redux-thunk&#039;\n\n\/\/\u751f\u6210store\u5bf9\u8c61\nconst store = createStore(finalReducer, applyMiddleware(thunk)); \/\/\u5185\u90e8\u4f1a\u7b2c\u4e00\u6b21\u8c03\u7528reducer\u51fd\u6570\uff0c\u5f97\u5230\u521d\u59cbstate \n\nexport default store<\/code><\/pre>\n<p>\u6b63\u5e38\u6765\u8bf4\uff0c\u4e00\u822c\u6211\u4eec\u662f\u89e6\u53d1\u4e00\u4e2a\u52a8\u4f5c\u8fdb\u884c\u8bf7\u6c42\uff0c\u5f97\u5230\u6570\u636e\u540e\uff0c\u518d\u53bbsetState\u3002\u5728Action\u4e2d\u4e5f\u662f\u4e00\u6837\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">export const nameAsync = (name) =&gt; {\n  return dispatch =&gt; {\n    setTimeout(() =&gt; {\n      dispatch(nameCreator(name))\n    }, 2000);\n  }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u7528setTimeout\u6a21\u62df\u8bf7\u6c42\u8fc7\u7a0b\u3002\u4e24\u79d2\u540e\uff0c\u6570\u636e\u8bf7\u6c42\u5230\u4e86\uff0c\u518d\u628a\u6570\u636e\u7ed9Reducer\u8fdb\u884c\u72b6\u6001\u66ff\u6362\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u624b\u52a8\u8c03\u7528dispatch\u8fd9\u4e2a\u51fd\u6570\uff0c\u5411Reducer\u53bb\u5206\u53d1\u52a8\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redux\u7b80\u4ecb Redux\u662fReact\u751f\u6001\u4e2d\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\u3002React\u63d0\u51fa\u5c06\u5c55\u793a\u7ec4\u4ef6\u4e0e\u5bb9\u5668\u7ec4\u4ef6\u5206\u79bb\u7684\u601d\u60f3\uff0c\u964d\u4f4e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[291],"class_list":["post-1062","post","type-post","status-publish","format-standard","hentry","category-react","tag-redux"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1062"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1062\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}