{"id":431,"date":"2022-06-28T14:33:06","date_gmt":"2022-06-28T06:33:06","guid":{"rendered":"http:\/\/gjweb.top\/?p=431"},"modified":"2022-11-05T11:51:35","modified_gmt":"2022-11-05T03:51:35","slug":"redux%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=431","title":{"rendered":"Redux\u4f7f\u7528"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"redux-\u548c-react-\u7684\u5173\u7cfb\">Redux \u548c React \u7684\u5173\u7cfb<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">redux \u7684\u529f\u80fd\u548c vuex \u76f8\u540c,\u90fd\u662f\u4f5c\u4e3a\u4e2d\u592e\u72b6\u6001\u7ba1\u7406\u4ed3\u5e93, \u4f46\u7528\u6237\u4f53\u9a8c\u4e0a\u548c vuex \u771f\u7684\u662f\u5927\u76f8\u5f84\u5ead, \u5176\u539f\u56e0\u5728\u4e8e vuex \u9ad8\u5ea6\u96c6\u6210\u5728 vue \u4e2d,\u53ef\u4ee5\u8bf4\u662f\u4e3avuex \u91cf\u8eab\u6253\u9020\u7684\u72b6\u6001\u7ba1\u7406\u7cfb\u7edf, \u800c redux \u4f5c\u4e3a\u4e00\u4e2a\u5e93,\u53ef\u4ee5\u652f\u6301\u5728 React \\ Angular \\ jquery \u8fd8\u6709\u539f\u751fJS\u4e2d, \u5e76\u4e0d\u662f\u4e3a react \u79c1\u4eba\u5b9a\u5236\u7684, \u4e60\u60ef\u4e86vuex\u7684\u5c0f\u4f19\u4f34 \u5728\u770b redux\u7684\u6d41\u7a0b\u5982\u540c\u7fd4\u4e00\u6837, \u518d\u6b21\u63d0\u5021\u60f3\u4f53\u9a8c\u51fd\u6570\u5f0f\u5f00\u53d1\u7684\u5c0f\u4f19\u4f34\u8bf7\u62e5\u62b1 vue3<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"redux\u7684\u7ec4\u6210\">Redux\u7684\u7ec4\u6210<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Action<ul><li>\u628a\u6570\u636e\u4ece\u5e94\u7528\u4f20\u5230store\u7684\u8f7d\u4f53\uff0c\u662f store \u7684\u552f\u4e00\u6765\u6e90\uff0c\u53ef\u4ee5\u901a\u8fc7 store.dispatch() \u5c06 action\u4f20\u9012\u7ed9 store<\/li><li>\u7279\u70b9<ul><li>\u672c\u8d28\u662f\u4e00\u4e2a\u5bf9\u8c61<\/li><\/ul><\/li><\/ul><\/li><li>\u5fc5\u987b\u5305\u542b\u4e00\u4e2a type \u5c5e\u6027\u6765\u8868\u793a\u8981\u6267\u884c\u7684\u52a8\u4f5c<ul><li>type \u4e00\u822c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32<\/li><\/ul><\/li><li>\u53ea\u662f\u63cf\u8ff0\u4e86\u4e8b\u60c5\u5c06\u8981\u53d1\u751f,\u5e76\u6ca1\u6709\u53bb\u66f4\u65b0 state<\/li><li>Reducer<ul><li>\u672c\u8d28\u662f\u4e00\u4e2a\u51fd\u6570,\u7528\u6765\u54cd\u5e94 action, \u7ecf\u8fc7\u5904\u7406\u540e,\u628a state \u53d1\u9001\u7ed9 Store<\/li><li>\u6ce8\u610f<ul><li>\u5fc5\u987b\u6709 return \u8fd4\u56de\u503c, store\u624d\u80fd\u63a5\u6536\u5230\u6570\u636e<\/li><li>\u63a5\u6536\u4e24\u4e2a\u53c2\u6570, \u7b2c\u4e00\u4e2a\u662f\u521d\u59cb\u5316\u7684 state (\u9700\u8981\u7ed9\u9ed8\u8ba4\u503c), \u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f action<\/li><\/ul><\/li><\/ul><\/li><li>Store<ul><li>\u628a action \u548c reducer \u8054\u7cfb\u5230\u4e00\u8d77\u7684\u5bf9\u8c61<\/li><li>\u804c\u8d23<ul><li>\u7ef4\u6301\u5e94\u7528\u7684 state<\/li><li>getState() \u83b7\u53d6 state<\/li><li>dispatch() \u53d1\u9001 action<\/li><li>subscribe() \u6ce8\u518c\u76d1\u542c<\/li><li>subscribe() \u8fd4\u56de\u503c\u6765\u6ce8\u9500\u76d1\u542c<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"redux\u57fa\u672c\u7528\u6cd5\">redux\u57fa\u672c\u7528\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install redux<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 src \u76ee\u5f55\u521b\u5efa action \/ index.js \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const sendAction = ()=&gt;{<br> &nbsp;return {<br> &nbsp; &nbsp;type:\"send_action\",<br> &nbsp; &nbsp;value:\"\u6211\u662f\u4e00\u4e2aaction\"<br>  }<br>}<br>module.exports = {<br> &nbsp;sendAction<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 src \u76ee\u5f55\u521b\u5efa reducer \/ index.js \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const initState = { value: \"\u9ed8\u8ba4\u503c\" };<br>const reducer = (state = initState, action) =&gt; {<br> &nbsp;console.log('reducer',state,action)<br> &nbsp;switch (action.type) {<br> &nbsp; &nbsp;case \"send_action\":<br> &nbsp; &nbsp; &nbsp;return Object.assign({}, state, action);<br> &nbsp; &nbsp;default:<br> &nbsp; &nbsp; &nbsp;return state;<br>  }<br>};<br>module.exports = {<br> &nbsp;reducer,<br>};<br>\u200b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 src \u76ee\u5f55\u521b\u5efa store \/ index.js \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {createStore} from \"redux\";<br>import {reducer} from \"..\/reducer\/index\"<br>const store = createStore(reducer)<br>export default store<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa home \u7ec4\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\";<br>import store from \"..\/store\/index\"<br>import {sendAction} from \"..\/action\/index\"<br>export default class Home extends React.Component {<br> &nbsp;handleClick = ()=&gt;{<br> &nbsp; &nbsp;const action = sendAction()<br> &nbsp; &nbsp;store.dispatch(action)<br>  }<br>componentDidMount(){<br> &nbsp;store.subscribe(()=&gt;{<br> &nbsp; &nbsp;console.log(\"subscribe\",store.getState())<br> &nbsp; &nbsp;this.setState({})<br>  })<br>}<br> &nbsp;render() {<br> &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;h1&gt;this is home&lt;\/h1&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={()=&gt;this.handleClick()}&gt;\u53d1\u9001\u4e00\u4e2aaction&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;{store.getState().value}&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp;  );<br>  }<br>}<br>\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"redux-\u4f7f\u7528-redux-thunk-\u4e2d\u95f4\u4ef6\">redux \u4f7f\u7528 redux-thunk \u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u662f\u8ba9 dispatch \u652f\u6301\u51fd\u6570\u7684\u5f62\u5f0f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add redux-thunk -S<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6a21\u62df\u4e00\u4e2a \u51fd\u6570\u6587\u4ef6 fun.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const xxxfun = ()=&gt;{<br> &nbsp;return (dispatch)=&gt;{<br> &nbsp; &nbsp;new Promise((resolve)=&gt;{<br> &nbsp; &nbsp; &nbsp;resolve(\"\u4e0d\u77e5\u9053\u8fd9\u4e2a\u73a9\u610f\u80fd\u5e72\u70b9\u5565\")<br> &nbsp;  }).then(response=&gt;{<br> &nbsp; &nbsp; &nbsp;dispatch({<br> &nbsp; &nbsp; &nbsp; &nbsp;type:'setText',<br> &nbsp; &nbsp; &nbsp; &nbsp;content:response<br> &nbsp; &nbsp;  })<br> &nbsp;  })<br>  }<br>}<br>export default xxxfun<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u58f0\u660e\u4e00\u4e2a reducer \u6587\u4ef6\u5939 reducer \/ test.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const reducer = ((<br> &nbsp;state={<br> &nbsp; &nbsp;text:'xxx'<br>  },<br> &nbsp;action<br>)=&gt;{<br> &nbsp;let newState = {...state};<br> &nbsp;let {type,content} = action<br> &nbsp;switch (type) {<br> &nbsp; &nbsp;case \"setTest\":<br> &nbsp; &nbsp; &nbsp;newState.text = content<br> &nbsp; &nbsp; &nbsp;return newState<br>\u200b<br> &nbsp; &nbsp;default:<br> &nbsp; &nbsp; &nbsp;return state<br>  }<br>})<br>export default reducer<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 store.js \u4e2d\u6574\u5408<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {createStore,combineReducers, applyMiddleware} from \"redux\"<br>import reducerText from \".\/test.js\"<br>import reduxThunk from \"redux-thunk\"<br>const reducer = combineReducers({reducerText})<br>const store = createStore(reducer,applyMiddleware(reduxThunk))<br>export default store<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import store from \".\/redux\/store\"<br>import xxxFun from \".\/redux\/test.js\"<br>const ChildA = ()=&gt;{<br> &nbsp;const handelClick = ()=&gt;{<br> &nbsp; &nbsp;store.dispatch(xxxFun())<br>  }<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;h2&gt;this is childA&lt;\/h2&gt;<br> &nbsp; &nbsp; &nbsp;&lt;button onClick={handelClick}&gt;handelClick&lt;\/button&gt;<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  )<br>}<br>export default ChildA<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"redux-\u4f7f\u7528-redux-promise-\u4e2d\u95f4\u4ef6\">redux \u4f7f\u7528 redux-promise \u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u65b9\u5f0f\u548c redux-thunk \u4e00\u6837, \u552f\u4e00\u7684\u533a\u522b\u662f dispatch \u53ef\u4ee5\u63a5\u6536 promise \u5bf9\u8c61<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add redux-promise -S<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 store \u6587\u4ef6\u4e2d\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {createStore,combineReducers, applyMiddleware} from \"redux\"<br>import reducerText from \".\/reducer\/test\"<br>import reduxThunk from \"redux-thunk\"<br>const reducer = combineReducers({reducerText})<br>const store = createStore(reducer,applyMiddleware(reduxPromise))<br>export default store<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"redux-\u6301\u4e45\u5316\">redux \u6301\u4e45\u5316<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add redux-persist -S<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u67e5\u770b\u5b98\u65b9\u6587\u6863 CRM \u5b66\u4e60\u6cd5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redux \u548c React \u7684\u5173\u7cfb redux \u7684\u529f\u80fd\u548c vuex \u76f8\u540c,\u90fd\u662f\u4f5c\u4e3a\u4e2d\u592e\u72b6\u6001\u7ba1\u7406\u4ed3\u5e93, \u4f46\u7528\u6237\u4f53 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-431","post","type-post","status-publish","format-standard","hentry","category-redux"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=431"}],"version-history":[{"count":2,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":517,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions\/517"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}