{"id":1066,"date":"2023-03-12T10:22:47","date_gmt":"2023-03-12T02:22:47","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1066"},"modified":"2023-04-29T11:46:23","modified_gmt":"2023-04-29T03:46:23","slug":"react-redux-manage-status","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/react-redux-manage-status\/","title":{"rendered":"React Redux\u7ba1\u7406\u72b6\u6001"},"content":{"rendered":"<p>state\u662freact\u7279\u6709\u7684\u4e00\u4e2a\u5b58\u50a8\u6570\u636e\u7684\u5730\u65b9\uff0c\u901a\u8fc7\u5404\u79cd\u64cd\u4f5c\u53ef\u4ee5\u6539\u53d8state\u7684\u503c\uff0c\u7136\u540e\u66f4\u65b0\u5230\u9875\u9762\u663e\u793a\u4e0a\u3002<br \/>\n\u4f46\u662f\uff0c\u5f53\u5de5\u7a0b\u53d8\u5f97\u975e\u5e38\u5e9e\u5927\u4e14\u590d\u6742\u7684\u65f6\u5019\uff0c\u5404\u79cd\u5404\u6837\u7684state\u548c\u66f4\u65b0state\u7684\u65b9\u6cd5\u5c31\u4f1a\u4f7f\u7a0b\u5e8f\u53d8\u5f97\u5f88\u4e71\uff0c\u7ba1\u7406\u524d\u7aef\u8fd9\u4e9b\u6570\u636e\uff0c\u9700\u8981\u8fdb\u884c\u201c\u6709\u6761\u7406\u7684\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u201d\u3002\u800c\u8fdb\u884c\u8fd9\u4e2a\u64cd\u4f5c\u7684\uff0c\u5c31\u662f<code>Redux<\/code>\u3002<\/p>\n<p><!-- more --><\/p>\n<p>Redux\u63d0\u4f9b\u4e00\u4e9bapi\u6765\u7ba1\u7406\u6570\u636e\uff0cRedux\u5f88\u9738\u9053\u5730\u544a\u8bc9\u6211\u4eec\uff1a<strong>\u6570\u636e\u53ea\u80fd\u5b58\u5728\u6211\u8fd9\uff0c\u5e76\u4e14\u53ea\u80fd\u901a\u8fc7\u6211\u7684\u65b9\u5f0f\u6765\u4fee\u6539<\/strong>\uff01<\/p>\n<p>Redux\u5305\u62ec\u4e09\u90e8\u5206\uff1a<code>store<\/code>\uff0c<code>action<\/code>\uff0c<code>reducer<\/code><\/p>\n<p><strong>store<\/strong>\uff1a\u4e00\u4e2a\u89c4\u8303\u7684state\uff0cRedux\u5c06\u6574\u4e2a\u5e94\u7528\u7684state\u50a8\u5b58\u5728\u552f\u4e00\u7684store\u4e2d<br \/>\n<strong>action<\/strong>\uff1a\u4e00\u4e2a\u6709\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528<code>dispatch(action)<\/code>\u6765\u89e6\u53d1\uff0c\u5e76\u4e14\u8fd9\u662f\u6539\u53d8state\u7684\u552f\u4e00\u65b9\u5f0f<br \/>\n<strong>reducer<\/strong>\uff1a\u5177\u4f53\u901a\u8fc7action\u66f4\u65b0state\u7684\u51fd\u6570\uff0c\u57fa\u672c\u7ed3\u6784\u662f<code>reducer(state, action) =&gt; newstate<\/code><\/p>\n<h3>\u6dfb\u52a0redux\u4f9d\u8d56<\/h3>\n<pre><code>yarn add redux<\/code><\/pre>\n<h3>\u521b\u5efastore<\/h3>\n<p>\u5728app.js\u4e2d\uff0c\u5f15\u5165redux\u5305\u4e2d\u7684createStore()\u65b9\u6cd5<\/p>\n<pre><code class=\"language-javascript\">import { createStore } from &#039;redux&#039;;<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u901a\u8fc7<code>createStore(reducers[, initialState])<\/code>\u7684\u65b9\u5f0f\u6765\u521b\u5efastore\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u6839\u636ereducer\u751f\u6210store\uff0c\u5e76\u4e14\u53ea\u80fd\u901a\u8fc7\u6b64reducer\u6765\u6539\u53d8store\u4e2d\u7684\u72b6\u6001\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684\u9ed8\u8ba4\u521d\u59cb\u503c\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u4e00\u4e2a\u5e94\u7528\u4e2d\u6211\u4eec\u53ea\u6709\u4e00\u4e2astore\uff0c\u5b58\u50a8\u4e86\u5168\u90e8\u6570\u636e\u72b6\u6001\uff0c\u4f46\u662f\u4f1a\u6709\u5f88\u591areducer\uff0c\u901a\u8fc7\u8fd9\u4e9breducer\u5408\u8d77\u6765\u521b\u5efastore\u9700\u8981\u7528\u5230<code>combineReducers<\/code>\u65b9\u6cd5\u3002<\/p>\n<p>\u4f46\u662f\u73b0\u5728\u53ea\u505a\u4e00\u4e2a\u7b80\u5355\u64cd\u4f5c\uff0c\u6240\u4ee5\u7528\u4e00\u4e2areducer\u505a\u4f8b\u5b50\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/\u521d\u59cb\u72b6\u6001\nconst initialState = {\n    data: [{\n        &quot;key&quot;: &quot;1&quot;,\n        &quot;city&quot;: &quot;\u676d\u5dde&quot;,\n        &quot;province&quot;: &quot;\u6d59\u6c5f&quot;\n    },{\n        &quot;key&quot;: &quot;2&quot;,\n        &quot;city&quot;: &quot;\u5e7f\u5dde&quot;,\n        &quot;province&quot;: &quot;\u5e7f\u4e1c&quot;\n    }]\n}\n\n\/\/\u521b\u5efareducer\u65b9\u6cd5\uff0c\u5148\u539f\u5c01\u4e0d\u52a8\u8fd4\u56destate\nconst myReducer = (state=initialState, action) =&gt; {\n    return state;\n}\n\/\/\u521b\u5efastore\u5b58\u50a8\u533a\uff0c\u5b83\u53ea\u80fd\u901a\u8fc7reducer\u4f5c\u4e3a\u53c2\u6570\u6765\u6784\u9020\nconst store = createStore(myReducer);<\/code><\/pre>\n<p>\u8fd9\u6837store\u5c31\u521b\u597d\u4e86\uff0c\u53ef\u4ee5\u628astore\u6253\u5370\u5230\u63a7\u5236\u53f0\u770b\u4e00\u4e0b\u3002<code>getState()<\/code>\u662fstore\u7684\u4e00\u4e2a\u6700\u5e38\u89c1api\uff0c\u7528\u4e86\u83b7\u53d6state\u7684\u503c\u3002<\/p>\n<pre><code class=\"language-javascript\">console.log(&quot;initial state: &quot;, store.getState());<\/code><\/pre>\n<p>Very well\uff0c\u63a5\u4e0b\u6765\u5199\u4e00\u4e2a\u66f4\u65b0state\u7684action\u3002<\/p>\n<h3>\u521b\u5efaaction<\/h3>\n<p>Action \u662f\u4e00\u4e2a\u5e26\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u5176\u5c5e\u6027\u7528type\u6765\u5b9a\u4e49\uff0ctype\u662f\u5fc5\u586b\u9879\uff0c\u5176\u4ed6\u7684\u8fd8\u53ef\u4ee5\u6709\u9644\u5e26\u6570\u636e\uff0c\u4e00\u822c\u5199\u5728\u7528payload\u91cc\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/\u521b\u5efa\u63cf\u8ff0\u6027\u5bf9\u8c61action\nconst myAction = {\n    type: &#039;ADD_DATA&#039;,\n    payload: {\n        &quot;key&quot;: &quot;3&quot;,\n        &quot;city&quot;: &quot;\u6df1\u5733&quot;,\n        &quot;province&quot;: &quot;\u5e7f\u4e1c&quot;\n    }\n};<\/code><\/pre>\n<p>\u4e0a\u9762\u5df2\u7ecf\u8bf4\u8fc7\uff0c\u4fee\u6539state\u7684\u552f\u4e00\u65b9\u6cd5\u5c31\u662f<code>dispatch(action)<\/code>\uff0c\u90a3\u4e48\u73b0\u5728\u6211\u4eec\u5c31\u628a\u8fd9\u4e2aaction\u89e6\u53d1\u4e00\u4e0b\u3002<\/p>\n<pre><code class=\"language-javascript\">store.dispatch(myAction);<\/code><\/pre>\n<p>\u5982\u679c\u662f\u70b9\u51fb\u4e00\u4e2a\u6309\u94ae\u89e6\u53d1\u7684\u8bdd\uff0c\u628adispatch\u653e\u5728onClick\u7ed1\u5b9a\u7684\u51fd\u6570\u91cc\u5c31\u884c\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/\u70b9\u51fb\u6309\u94ae\u89e6\u53d1\nconst changeData = () =&gt; {\n    store.dispatch(myAction);\n}\n\nclass App extends Component {\n    render() {\n        return ( \n            &lt;Button type=&quot;primary&quot; onClick={changeData}&gt;\u4fee\u6539\u6570\u636e&lt;\/Button&gt;\n            \u2026\u2026\n        );\n    }\n}<\/code><\/pre>\n<h3>\u521b\u5efareducer<\/h3>\n<p>Store\u89e6\u53d1\u4e86\u4e00\u4e2aaction\uff0c\u76f8\u5f53\u4e8estore\u5ba3\u79f0\uff1a\u201c\u6211\u8981\u6539\u53d8\u81ea\u5df1\u7684state\u201d\uff0c\u6b64\u65f6\uff0c\u9700\u8981reducer\u6765\u6267\u884c\u8fd9\u4e2a\u8fc7\u7a0b\u3002<br \/>\nReducer\u7684\u8f93\u5165\u53c2\u6570\u662f\u5f53\u524d\u7684state\u548c\u6536\u5230\u7684action\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684state\u3002\u6ce8\u610f\u5728reducer\u4e2d\uff0c\u4e0d\u80fd\u968f\u610f\u66f4\u6539\u53c2\u6570\u6216\u8005\u52a0\u5165\u968f\u673a\u6027\u7684\u5404\u79cd\u64cd\u4f5c\uff0c\u4e5f\u5c31\u662freducer\u8f93\u5165\u76f8\u540c\u7684\u8bdd\uff0c\u8f93\u51fa\u4e5f\u5fc5\u5b9a\u76f8\u540c\u3002<\/p>\n<p>\u4fee\u6539\u521a\u521a\u7684reducer\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/\u521b\u5efareducer\u65b9\u6cd5\nconst myReducer = (state=initialState, action) =&gt; {\n    switch (action.type) {\n        case &#039;ADD_DATA&#039;:\n            return {\n                ...state,\n                data:state.data.concat(action.payload)\n            }\n        default:\n            return state;\n    }\n}<\/code><\/pre>\n<p>&#8230;state\u662fES6\u4e2d\u7684\u4e09\u70b9\u8fd0\u7b97\u7b26\uff0c\u4f5c\u7528\u662f\u628a\u6570\u7ec4\u6253\u5f00\u8fdb\u884c\u64cd\u4f5c\u3002<br \/>\n\u53ef\u4ee5\u770b\u5230\uff0c\u5f53action\u662f<code>ADD_DATA<\/code>\u7684\u65f6\u5019\uff0c\u6211\u4eec\u628apayload\u91cc\u9762\u7684\u5185\u5bb9\u52a0\u5728\u539f\u6709\u7684data\u4e4b\u540e\uff0c\u4e5f\u5c31\u662f\u65b0\u52a0\u4e00\u884c\u6570\u636e\u3002<\/p>\n<p>\u8fd9\u65f6\uff0c\u5728\u63a7\u5236\u53f0\u8f93\u51fastore\u7684\u503c\uff0c\u53ef\u4ee5\u770b\u5230\u5df2\u7ecf\u6709\u6240\u6539\u53d8\u3002<\/p>\n<h3>\u901a\u8fc7\u76d1\u542c\u66f4\u65b0\u89c6\u56fe<\/h3>\n<p>\u7136\u800c\uff0c\u73b0\u5728\u89c6\u56fe\u4e0a\u5374\u6ca1\u6709\u5237\u65b0\uff0c\u53ea\u662fstore\u4e2d\u7684\u503c\u53d1\u751f\u6539\u53d8\u3002\u8fd9\u662f\u56e0\u4e3astore\u8ddfview\u5c42\u5e76\u6ca1\u6709\u8fde\u8d77\u6765\uff0c\u5982\u679c\u4e0d\u624b\u52a8\u91cd\u65b0render\uff0c\u9875\u9762\u662f\u4e0d\u4f1a\u53d8\u5316\u7684\uff0c\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u76d1\u542c\u51fd\u6570\uff0c\u76d1\u542cstore\u4e2d\u503c\u7684\u53d8\u5316\uff0c\u5f53\u53d1\u751f\u53d8\u5316\u65f6\u91cd\u65b0\u6e32\u67d3view\u3002<\/p>\n<p>\u8fd9\u4e2a\u76d1\u542c\u7531<code>store.subscribe<\/code>\u5b9e\u73b0\u3002\uff08\u5982\u679c\u4f7f\u7528\u4e86<code>react-redux<\/code>\u7684\u8bdd\uff0c\u5b83\u91cc\u9762\u7684<code>connect<\/code>\u8ba9\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u53bb<code>subscribe<\/code>\u5168\u5c40state\u7684\u53d8\u5316\uff0c\u5b83\u4f1a\u5728\u5185\u90e8\u81ea\u52a8\u76d1\u542c\u5e76\u66f4\u65b0\u3002\uff09<\/p>\n<pre><code class=\"language-javascript\">class App extends Component {\n    listener() {\n      let newState = store.getState();\n      this.setState(newState);   \n    }\n\n    \/\/\u4fdd\u6301\u76d1\u542c\n    componentDidMount () {\n        store.subscribe(this.listener.bind(this));\n    }\n}<\/code><\/pre>\n<h3>Redux\u6d41\u7a0b\u603b\u7ed3<\/h3>\n<p>\u8fd9\u6837\uff0c\u6574\u4e2aRedux\u6d41\u7a0b\u5b9e\u73b0\u5b8c\u6bd5\uff1a<\/p>\n<ul>\n<li>\u521b\u5efastore\u5b58\u50a8\u6570\u636e\uff0c\u5e76\u628a\u521d\u59cb\u503c\u7ed1\u5b9a\u5230view\u5c42<\/li>\n<li>\u70b9\u51fb\u6309\u94ae\uff0c\u89e6\u53d1<code>store.dispatch(myAction)<\/code><\/li>\n<li>store\u6536\u5230action\u540e\uff0c\u8c03\u7528<code>myReducer = (state=initialState, action)<\/code>\u6539\u53d8store\u4e2d\u7684state<\/li>\n<li>store\u66f4\u65b0\u540e\uff0c\u901a\u8fc7<code>store.subscribe<\/code>\u76d1\u542c\u51fd\u6570\u66f4\u65b0view\u5c42<\/li>\n<\/ul>\n<p>\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">import React from &#039;react&#039;;\nimport { Button, Icon, Table, Row, Col } from &#039;antd&#039;;\nimport { createStore } from &#039;redux&#039;;\n\n\/\/\u521d\u59cb\u72b6\u6001\nconst initialState = {\n    data: [{\n        &quot;key&quot;: &quot;1&quot;,\n        &quot;city&quot;: &quot;\u676d\u5dde&quot;,\n        &quot;province&quot;: &quot;\u6d59\u6c5f&quot;\n    },{\n        &quot;key&quot;: &quot;2&quot;,\n        &quot;city&quot;: &quot;\u5e7f\u5dde&quot;,\n        &quot;province&quot;: &quot;\u5e7f\u4e1c&quot;\n    }]\n}\n\n\/\/ \/\/\u521b\u5efareducer\u65b9\u6cd5\uff0c\u5148\u539f\u5c01\u4e0d\u52a8\u8fd4\u56destate\n\/\/ const myReducer = (state=initialState, action) =&gt; {\n\/\/     return state;\n\/\/ }\n\/\/\u521b\u5efareducer\u65b9\u6cd5\nconst myReducer = (state=initialState, action) =&gt; {\n    switch (action.type) {\n        case &#039;ADD_DATA&#039;:\n            return {\n                ...state,\n                data:state.data.concat(action.payload)\n            }\n        default:\n            return state;\n    }\n}\n\/\/\u521b\u5efastore\u5b58\u50a8\u533a\uff0c\u5b83\u53ea\u80fd\u901a\u8fc7reducer\u4f5c\u4e3a\u53c2\u6570\u6765\u6784\u9020\nconst store = createStore(myReducer);\n\n\/\/\u521b\u5efa\u63cf\u8ff0\u6027\u5bf9\u8c61action\nconst myAction = {\n    type: &#039;ADD_DATA&#039;,\n    payload: {\n        &quot;key&quot;: &quot;3&quot;,\n        &quot;city&quot;: &quot;\u6df1\u5733&quot;,\n        &quot;province&quot;: &quot;\u5e7f\u4e1c&quot;\n    }\n};\n\nclass ReduxTest extends React.Component {\n    constructor(props) {\n        super(props);\n        this.state = {\n            columns: [{\n                title: &#039;\u57ce\u5e02&#039;,\n                dataIndex: &#039;city&#039;,\n            }, {\n                title: &#039;\u7701\u4efd&#039;,\n                dataIndex: &#039;province&#039;,\n            }],\n        }\n    }\n\n    listener() {\n        let newState = store.getState();\n        this.setState(newState);\n    }\n\n    \/\/\u4fdd\u6301\u76d1\u542c\n    componentDidMount () {\n        store.subscribe(this.listener.bind(this));\n    }\n\n    readData() {\n        console.log(&quot;initial state: &quot;, store.getState());;\n    }\n\n    addData() {\n        store.dispatch(myAction);\n    }\n\n    render() {\n        var data = store.getState().data;\n        var columns = this.state.columns;\n        return (\n            &lt;div className=&quot;App&quot;&gt;\n                &lt;Row type=&quot;flex&quot; justify=&quot;center&quot;&gt;\n                    &lt;Button onClick={this.readData}&gt;\u8bfb\u53d6\u6570\u636e&lt;\/Button&gt;\n                    &lt;Button type=&quot;primary&quot; onClick={this.addData.bind(this)}&gt;\u6dfb\u52a0\u6570\u636e&lt;\/Button&gt;\n                &lt;\/Row&gt;\n                &lt;div&gt;\n                    &lt;Table columns={columns} dataSource={data}\/&gt;\n                &lt;\/div&gt;\n            &lt;\/div&gt;\n        );\n    }\n}\n\nexport default ReduxTest;<\/code><\/pre>\n<h3>\u91cd\u8981\u8865\u5145\u8bf4\u660e<\/h3>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u91cc\uff0c\u6211\u4eec\u53ea\u7528\u4e86redux\uff0c\u4e8b\u5b9e\u4e0a\u5728React\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5e38\u7528<code>react-redux<\/code>\u6765\u8fdb\u884c\u64cd\u4f5c\u3002<code>react-redux<\/code>\u63d0\u4f9b\u7684\u7ed3\u6784\u53ef\u4ee5\u8ba9\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u53bb\u5199<code>dispatch<\/code>\uff0c<code>subscribe<\/code>\u8fd9\u4e9b\u51fd\u6570\uff0c\u5b83\u5728\u5185\u90e8\u901a\u8fc7\u81ea\u8eab\u7684\u7ed3\u6784\u5e2e\u6211\u4eec\u5b8c\u6210\u4e86\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<p>\u800c<code>Dva<\/code>\u5df2\u7ecf\u5c01\u88c5\u597d\u4e86<code>react-router<\/code>\uff0c<code>react-redux<\/code>\uff0c<code>redux-saga<\/code>\u7b49\u4e2d\u95f4\u4ef6\uff0c\u5e76\u7ed9\u51fa\u4e86\u6e05\u6670\u5730\u5de5\u7a0b\u76ee\u5f55\u7ed3\u6784\u3002\u56e0\u800c\u5728\u4e86\u89e3react\u57fa\u7840\u77e5\u8bc6\u540e\uff0c\u7528Dva\u6765\u6784\u5efa\u5de5\u7a0b\u66f4\u52a0\u5feb\u6377\u7b80\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>state\u662freact\u7279\u6709\u7684\u4e00\u4e2a\u5b58\u50a8\u6570\u636e\u7684\u5730\u65b9\uff0c\u901a\u8fc7\u5404\u79cd\u64cd\u4f5c\u53ef\u4ee5\u6539\u53d8state\u7684\u503c\uff0c\u7136\u540e\u66f4\u65b0\u5230\u9875\u9762\u663e\u793a\u4e0a\u3002 \u4f46 [&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-1066","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\/1066","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=1066"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}