{"id":4596,"date":"2023-01-04T16:37:19","date_gmt":"2023-01-04T08:37:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4596"},"modified":"2023-01-04T16:37:19","modified_gmt":"2023-01-04T08:37:19","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8react%e7%b5%84%e4%bb%b6%e9%96%93%e5%82%b3%e9%81%9e%e8%b3%87%e6%96%99","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8react%e7%b5%84%e4%bb%b6%e9%96%93%e5%82%b3%e9%81%9e%e8%b3%87%e6%96%99\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u5728React\u7d44\u4ef6\u9593\u50b3\u905e\u8cc7\u6599"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, React \u7d44\u4ef6\u5144\u5f1f\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\"><\/p>\n<h1>React \u7d44\u4ef6\u5144\u5f1f\u4e4b\u9593\u50b3\u905e\u8cc7\u6599<\/h1>\n<p>React \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u54c1\u8cea\u7684\u7528\u6236\u754c\u9762\u3002React \u7684\u7d44\u4ef6\u7cfb\u7d71\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c07\u7d44\u4ef6\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u4ee5\u69cb\u5efa\u66f4\u8907\u96dc\u7684\u7528\u6236\u754c\u9762\u3002\u5728 React \u4e2d\uff0c\u7d44\u4ef6\u4e4b\u9593\u53ef\u4ee5\u901a\u904e props \u4f86\u50b3\u905e\u8cc7\u6599\uff0c\u4f46\u662f\u6709\u6642\u5019\uff0c\u6211\u5011\u9700\u8981\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u800c\u4e0d\u662f\u7236\u5b50\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 React \u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\u3002<\/p>\n<h2>React \u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\u7684\u65b9\u6cd5<\/h2>\n<p>\u5728 React \u4e2d\uff0c\u6709\u5169\u7a2e\u4e3b\u8981\u7684\u65b9\u6cd5\u53ef\u4ee5\u5728\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff1a<\/p>\n<ul>\n<li><strong>Context API\uff1a<\/strong>Context API \u662f React \u63d0\u4f9b\u7684\u4e00\u7a2e\u5168\u5c40\u8cc7\u6599\u5b58\u5132\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u8cc7\u6599\u3002<\/li>\n<li><strong>Redux\uff1a<\/strong>Redux \u662f\u4e00\u500b JavaScript \u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u8cc7\u6599\u3002<\/li>\n<\/ul>\n<h2>\u4f7f\u7528 Context API \u4f86\u50b3\u905e\u8cc7\u6599<\/h2>\n<p>Context API \u662f React \u63d0\u4f9b\u7684\u4e00\u7a2e\u5168\u5c40\u8cc7\u6599\u5b58\u5132\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u8cc7\u6599\u3002\u4f7f\u7528 Context API \u4f86\u50b3\u905e\u8cc7\u6599\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b Context \u5c0d\u8c61\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66 Provider \u7d44\u4ef6\uff1a\n<pre class=\"brush: javascript\">\n  const MyContext = React.createContext();\n  <MyContext.Provider value={\/* some value *\/}>\n    ...\n  <\/MyContext.Provider>\n  <\/pre>\n<\/li>\n<li>\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u9700\u8981\u4f7f\u7528\u8a72\u8cc7\u6599\u7684\u7d44\u4ef6\u4e2d\u4f7f\u7528 Consumer \u7d44\u4ef6\u4f86\u7372\u53d6\u8a72\u8cc7\u6599\uff1a\n<pre class=\"brush: javascript\">\n  <MyContext.Consumer>\n    {value => \/* render something based on the context value *\/}\n  <\/MyContext.Consumer>\n  <\/pre>\n<\/li>\n<\/ol>\n<h2>\u4f7f\u7528 Redux \u4f86\u50b3\u905e\u8cc7\u6599<\/h2>\n<p>Redux \u662f\u4e00\u500b JavaScript \u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u8cc7\u6599\u3002\u4f7f\u7528 Redux \u4f86\u50b3\u905e\u8cc7\u6599\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b store \u5c0d\u8c61\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66 Provider \u7d44\u4ef6\uff1a\n<pre class=\"brush: javascript\">\n  const store = createStore(reducer);\n  <Provider store={store}>\n    ...\n  <\/Provider>\n  <\/pre>\n<\/li>\n<li>\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u9700\u8981\u4f7f\u7528\u8a72\u8cc7\u6599\u7684\u7d44\u4ef6\u4e2d\u4f7f\u7528 connect() \u51fd\u6578\u4f86\u7372\u53d6\u8a72\u8cc7\u6599\uff1a\n<pre class=\"brush: javascript\">\n  const mapStateToProps = state => ({\n    \/* map state to props *\/\n  });\n  export default connect(mapStateToProps)(MyComponent);\n  <\/pre>\n<\/li>\n<\/ol>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 React \u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 Context API \u6216 Redux \u4f86\u5be6\u73fe\u9019\u4e00\u76ee\u6a19\u3002\u7136\u800c\uff0c\u6211\u5011\u9084\u9700\u8981\u6839\u64da\u9700\u6c42\u4f86\u6c7a\u5b9a\u4f7f\u7528\u54ea\u7a2e\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728React\u7d44\u4ef6\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528props\u548cstate\u4f86\u50b3\u905e\u8cc7\u6599\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528contextAPI\u4f86\u8655\u7406\u8907\u96dc\u7684\u8cc7\u6599\u6d41\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-4596","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1c8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4596"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4596\/revisions"}],"predecessor-version":[{"id":4597,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4596\/revisions\/4597"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}