{"id":514,"date":"2022-11-05T11:49:55","date_gmt":"2022-11-05T03:49:55","guid":{"rendered":"http:\/\/gjweb.top\/?p=514"},"modified":"2022-11-05T11:49:56","modified_gmt":"2022-11-05T03:49:56","slug":"react-redux-%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=514","title":{"rendered":"react-redux \u4f7f\u7528"},"content":{"rendered":"\n<p>\u4e0a\u9762\u4ecb\u7ecd Redux \u7684\u65f6\u5019\u88abup\u4e3b\u5f62\u5bb9\u4e3axiang, \u800c react-redux \u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u7b80\u5316\u4e86 redux \u7684\u64cd\u4f5c, \u4f18\u5316\u4e86\u7528\u6237\u4f53\u9a8c\uff0c\u4f46\u4e0d\u8fc7\u662fxiang\u4e0a\u6dfb\u82b1\uff0c\u672c\u8d28\u8fd8\u662fxiang\uff0c\u540e\u6765\u5b98\u65b9\u5bf9 react-redux \u7684\u529f\u80fd\u53c8\u505a\u4e86\u8fdb\u4e00\u6b65\u7684\u6539\u8fdb\uff0c\u63a8\u51fa\u7684 react Hook \u53ef\u4ee5\u8fdb\u5b8c\u5168\u66ff\u4ee3 react-redux \u7684\u529f\u80fd\uff0c\u8fd8\u662f\u63a8\u8350\u60f3\u7528\u51fd\u6570\u5f00\u53d1\u7684\u5c0f\u4f19\u4f34\u8bf7\u62e5\u62b1vue3<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"react-redux\u6838\u5fc3\u7ec4\u6210\">react-redux\u6838\u5fc3\u7ec4\u6210<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Provider<ul><li>Prov4der \u5305\u88f9\u5728\u8ddf\u7ec4\u4ef6\u6700\u5916\u5c42\uff0c\u4f7f\u6240\u6709\u7684\u5b50\u7ec4<\/li><li>\\4o;\/ntext \u4f20\u9012\u7ed9\u6240\u6709\u7ec4\u4ef6<\/li><\/ul><\/li><li>Connect<ul><li>Provider \u5185\u90e8\u7ec4\u4ef6\u5982\u679c\u60f3\u4f7f\u7528 state \u4e2d\u7684\u6570\u636e, \u5c31\u5fc5\u987b\u88abconnect\u52a0\u5f3a (\u9700\u8981\u67ef\u529b\u5316\u8c03\u7528\u7ec4\u4ef6)<\/li><li>connect \u672c\u8d28\u4e0a\u5c31\u662f\u7b80\u5316\u4e86\u83b7\u53d6 store \u4e2d\u7684state<\/li><li><strong>\u9700\u8981\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff08\u5c06\u8981\u4f20\u7ed9\u5b50\u7ec4\u4ef6\u4f20\u9012\u7684\u5c5e\u6027\uff0c\u5c06\u8981\u7ed9\u5b50\u7ec4\u4ef6\u4f20\u7684\u56de\u8c03\u51fd\u6570\uff09<\/strong><\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"react-redux-\u57fa\u672c\u4f7f\u7528\">react-redux \u57fa\u672c\u4f7f\u7528<\/h2>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install react-redux redux <\/code><\/pre>\n\n\n\n<p>\u521b\u5efa redux \u6587\u4ef6\u5939\u7528\u4e8e\u5b58\u653e\u72b6\u6001 redux\/reducer\/index \u6a21\u5757\u5316\u8bbe\u7f6e reducer<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const reducer = ((<br>    state = {<br> &nbsp; &nbsp; &nbsp; &nbsp;text:\"hello world\"<br> &nbsp;  },<br> &nbsp; &nbsp;action<br>)=&gt;{<br>    let newState &nbsp;= {...state}<br> &nbsp; &nbsp;let {type,text} = action<br> &nbsp; &nbsp;switch (type) {<br> &nbsp; &nbsp;    case \"setText\":<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newState.text = text<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return newState<br> &nbsp; &nbsp; &nbsp; &nbsp;default:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return state<br> &nbsp; &nbsp; } &nbsp; &nbsp;<br>})<br>export default reducer<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa redux \/ store.js \u7528\u4e8e\u6574\u5408 reducer<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {createStore,combineReducers} from \"redux\"<br>import textReducer from \".\/reducer\/index\"<br>const reducer = combineReducers({textReducer})<br>const store = createStore(reducer)<br>export default store<\/code><\/pre>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528 APP \u7ec4\u4ef6\u5f15\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {Provider} from \"react-redux\"<br>import store from \".\/redux\/store.js\"<br>import ChildA from \".\/childA\"<br>import ChildB from \".\/childB\"<br>const App = ()=&gt;{<br> &nbsp; &nbsp;return(<br> &nbsp;      &lt;Provider store={store}&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;ChildA\/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;ChildB\/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/Provider&gt;<br> &nbsp;  )<br>}<br>export default Index<\/code><\/pre>\n\n\n\n<p>\u5728 ChildA \u4e2d\u4fee\u6539 text \u7684\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>imoprt {connect} from \"react-redux\"<br>const ChildA = (props)=&gt;{<br> &nbsp; &nbsp;const handelClick = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;console.log(props)<br> &nbsp; &nbsp; &nbsp; &nbsp;props.setText()<br> &nbsp;  }<br> &nbsp; &nbsp;return (<br> &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;Click&lt;button\/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp;  )<br>}<br>const setText = {<br> &nbsp; &nbsp;setText\uff08\uff09{<br> &nbsp;      return {<br> &nbsp;          type:\"setText\",<br> &nbsp;          text:\"fuck react\"<br>        }<br>    }<br>}<br>export default connect(null,setText)(childA)<\/code><\/pre>\n\n\n\n<p>\u5728 childB \u4e2d\u62ff\u5230 text \u7684\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {connect} from \"react-redux\"<br>const ChildB = (props)=&gt;{<br> &nbsp; &nbsp;return (<br> &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp;      &lt;h2&gt;this is ChildB&lt;\/h2&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;h3&gt;this is react-redux {props.text}&lt;\/h3&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp;  )<br>}<br>export default connect((state)=&gt;{<br> &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp;text:state.textReducer.text<br> &nbsp;  }<br>})(ChildB)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u9762\u4ecb\u7ecd Redux \u7684\u65f6\u5019\u88abup\u4e3b\u5f62\u5bb9\u4e3axiang, \u800c react-redux \u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u7b80\u5316\u4e86 red [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,50],"tags":[],"class_list":["post-514","post","type-post","status-publish","format-standard","hentry","category-react","category-redux"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/514","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=514"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":515,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/514\/revisions\/515"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}