{"id":436,"date":"2022-06-28T21:45:17","date_gmt":"2022-06-28T13:45:17","guid":{"rendered":"http:\/\/gjweb.top\/?p=436"},"modified":"2022-06-28T21:45:18","modified_gmt":"2022-06-28T13:45:18","slug":"reacthooks-%e4%bb%a3%e6%9b%bf-redux","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=436","title":{"rendered":"reactHooks \u4ee3\u66ff Redux"},"content":{"rendered":"\n<p>\u5b98\u65b9\u9000\u51fa\u7684 reactHooks \u53ef\u4ee5\u5b8c\u7f8e\u7684\u5b9e\u73b0 redux \u7684\u529f\u80fd, \u4f7f\u7528\u65b9\u6cd5\u66f4\u52a0\u4fbf\u6377, \u4eff\u4f5b\u53c8\u770b\u5230\u4e86React\u7684\u672a\u6765<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4f7f\u7528\u6b65\u9aa4\">\u4f7f\u7528\u6b65\u9aa4<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5c06\u6570\u636e\u53ca\u91cd\u5728\u4e00\u4e2astore\u5bf9\u8c61\u4e0a<\/li><li>\u5c06\u6240\u6709\u64cd\u4f5c\u96c6\u4e2d\u5728reducer\u4e2d<\/li><li>\u521b\u5efa\u4e00\u4e2aContext<\/li><li>\u521b\u5efa\u5bf9\u6570\u636e\u7684\u8bfb\u5199API<\/li><li>\u5c06\u7b2c\u56db\u90e8\u7684\u5185\u5bb9\u653e\u5230\u7b2c\u4e09\u6b65\u7684Context\u4e2d<\/li><li>\u7528 Context.Provider \u5c06 Context \u63d0\u4f9b\u7ed9\u6240\u6709\u7ec4\u4ef6<\/li><li>\u5404\u7ec4\u4ef6\u7528 useContext \u83b7\u53d6\u8bfb\u5199API<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4e0a\u4ee3\u7801\">\u4e0a\u4ee3\u7801<\/h2>\n\n\n\n<p>\u65b0\u5efa Context \/ index.js \u6587\u4ef6\u6a21\u5757\u5316\u6b65\u9aa4\u4e09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {createContext} from \"react\"<br>const Context = createContext(null)<br>export default Context<\/code><\/pre>\n\n\n\n<p>\u5728APP.js \u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useReducer } from \"react\";<br>import Context from \".\/Context\/index\"<br>import Coma from \".\/pages\/Coma\"<br>import Comb from \".\/pages\/Comb\"<br>\u200b<br>const initState = {<br> &nbsp;user:\"null\",<br> &nbsp;books:\"\"<br>}<br>\u200b<br>const obj = {<br> &nbsp;user: (state, action) =&gt; {<br> &nbsp; &nbsp;return { ...state, user: action.user };<br>  },<br> &nbsp;books:(state, action) =&gt; {<br> &nbsp; &nbsp;return { ...state, books: action.books };<br>  },<br>}<br>\u200b<br>const reducer = (state,action)=&gt;{<br> &nbsp;let fn = obj&#91;action.type];<br> &nbsp;if(fn){<br> &nbsp; &nbsp;return fn(state,action)<br>  }else{<br> &nbsp; &nbsp;console.error(\"wrong type\");<br>  }<br>}<br>\u200b<br>function App() {<br> &nbsp;const &#91;state,dispatch] = useReducer(reducer,initState)<br>\u200b<br> &nbsp;const handelAddUser = ()=&gt;{<br> &nbsp; &nbsp;dispatch({type:\"user\",user:\"yoyo\"})<br>  }<br> &nbsp;const handelAddBooks = ()=&gt;{<br> &nbsp; &nbsp;dispatch({type:\"books\",books:\"\u4e1c\u65b9\u7504\u9009\"})<br>  }<br>\u200b<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;Context.Provider value={{state,dispatch}}&gt;<br> &nbsp; &nbsp; &nbsp;&lt;Coma&gt;&lt;\/Coma&gt;<br> &nbsp; &nbsp; &nbsp;&lt;Comb&gt;&lt;\/Comb&gt;<br> &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={handelAddUser}&gt;\u6dfb\u52a0\u4f5c\u8005&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={handelAddBooks}&gt;\u6dfb\u52a0\u4e66\u7c4d&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp;&lt;\/Context.Provider&gt;<br>  );<br>}<br>\u200b<br>export default App;<br>\u200b<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa pages \/ Coma.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ eslint-disable-next-line<br>import React, { useContext } from \"react\"<br>import Context from \"..\/Context\/index\"<br>function Coma(){<br> &nbsp;\/\/ eslint-disable-next-line<br> &nbsp;const {state,dispatch} = useContext(Context)<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;h1&gt;this is Coma &lt;\/h1&gt;<br> &nbsp; &nbsp; &nbsp;&lt;div&gt;name: {state.user}&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;hr \/&gt;<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  )<br>}<br>export default Coma<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa pages \/ Comb.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useContext } from \"react\"<br>import Context from \"..\/Context\/index\"<br>function Comb(){<br> &nbsp;\/\/ eslint-disable-next-line<br> &nbsp;const {state,dispatch} = useContext(Context)<br>\u200b<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;h1&gt;this is Comb &lt;\/h1&gt;<br> &nbsp; &nbsp; &nbsp;&lt;div&gt;\u4e66\u7c4d\uff1a{state.books}&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;br \/&gt;<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  )<br>}<br>\u200b<br>export default Comb<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u9000\u51fa\u7684 reactHooks \u53ef\u4ee5\u5b8c\u7f8e\u7684\u5b9e\u73b0 redux \u7684\u529f\u80fd, \u4f7f\u7528\u65b9\u6cd5\u66f4\u52a0\u4fbf\u6377, \u4eff\u4f5b\u53c8\u770b\u5230\u4e86Rea [&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-436","post","type-post","status-publish","format-standard","hentry","category-redux"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/436","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=436"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/436\/revisions"}],"predecessor-version":[{"id":437,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/436\/revisions\/437"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}