{"id":5743,"date":"2023-01-06T14:52:05","date_gmt":"2023-01-06T06:52:05","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5743"},"modified":"2023-01-06T14:52:05","modified_gmt":"2023-01-06T06:52:05","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%b5%84%e4%bb%b6%e9%80%9a%e4%bf%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%b5%84%e4%bb%b6%e9%80%9a%e4%bf%a1%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, \u7d44\u4ef6\u901a\u4fe1, \u7a0b\u5f0f\u7bc4\u4f8b\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u7ec4\u4ef6\u901a\u4fe1\uff1f<\/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\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002React \u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u5be6\u73fe\u7d44\u4ef6\u9593\u7684\u901a\u4fe1\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u7d44\u4ef6\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4ec0\u9ebc\u662f\u7d44\u4ef6\u901a\u4fe1\uff1f<\/h2>\n<p>\u7d44\u4ef6\u901a\u4fe1\u662f\u6307\u5728 React \u4e2d\uff0c\u5169\u500b\u6216\u591a\u500b\u7d44\u4ef6\u4e4b\u9593\u7684\u4ea4\u4e92\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\u548c\u51fd\u6578\uff0c\u4ee5\u4fbf\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u7d44\u4ef6\u3002<\/p>\n<h2>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\uff1f<\/h2>\n<p>\u5728 React \u4e2d\uff0c\u6709\u5169\u7a2e\u4e3b\u8981\u7684\u65b9\u6cd5\u53ef\u4ee5\u5be6\u73fe\u7d44\u4ef6\u9593\u7684\u901a\u4fe1\uff1aprops \u548c context\u3002<\/p>\n<h3>Props<\/h3>\n<p>Props \u662f React \u4e2d\u6700\u5e38\u7528\u7684\u7d44\u4ef6\u901a\u4fe1\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\u548c\u51fd\u6578\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5728\u4e00\u500b\u7d44\u4ef6\u4e2d\u50b3\u905e\u4e00\u500b\u51fd\u6578\u5230\u53e6\u4e00\u500b\u7d44\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 props \u4f86\u5be6\u73fe\uff1a<\/p>\n<pre class=\"brush: javascript\">\n\/\/ Parent Component\nclass Parent extends React.Component {\n  handleClick = () => {\n    console.log('Clicked!');\n  }\n\n  render() {\n    return (\n      <Child onClick={this.handleClick} \/>\n    );\n  }\n}\n\n\/\/ Child Component\nclass Child extends React.Component {\n  render() {\n    return (\n      <button onClick={this.props.onClick}>Click Me!<\/button>\n    );\n  }\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5728 Parent \u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u6578 handleClick\uff0c\u7136\u5f8c\u5c07\u5b83\u50b3\u905e\u7d66 Child \u7d44\u4ef6\uff0c\u4e26\u5c07\u5b83\u4f5c\u70ba props \u50b3\u905e\u7d66 Child \u7d44\u4ef6\u3002Child \u7d44\u4ef6\u53ef\u4ee5\u4f7f\u7528 props \u4f86\u8a2a\u554f handleClick \u51fd\u6578\uff0c\u4e26\u5728\u7528\u6236\u9ede\u64ca\u6309\u9215\u6642\u8abf\u7528\u5b83\u3002<\/p>\n<h3>Context<\/h3>\n<p>Context \u662f\u53e6\u4e00\u7a2e\u5728 React \u4e2d\u5be6\u73fe\u7d44\u4ef6\u9593\u901a\u4fe1\u7684\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\uff0c\u800c\u7121\u9700\u660e\u78ba\u5730\u50b3\u905e props\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5728\u4e00\u500b\u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u4e00\u500b\u5168\u5c40\u8b8a\u91cf\uff0c\u4e26\u5728\u53e6\u4e00\u500b\u7d44\u4ef6\u4e2d\u8a2a\u554f\u5b83\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 context \u4f86\u5be6\u73fe\uff1a<\/p>\n<pre class=\"brush: javascript\">\n\/\/ Context\nconst MyContext = React.createContext();\n\n\/\/ Parent Component\nclass Parent extends React.Component {\n  state = {\n    name: 'John'\n  };\n\n  render() {\n    return (\n      <MyContext.Provider value={this.state.name}>\n        <Child \/>\n      <\/MyContext.Provider>\n    );\n  }\n}\n\n\/\/ Child Component\nclass Child extends React.Component {\n  render() {\n    return (\n      <MyContext.Consumer>\n        {name => Hello, {name}!<\/p>}\n      <\/MyContext.Consumer>\n    );\n  }\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5728 Parent \u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u4e86\u4e00\u500b\u5168\u5c40\u8b8a\u91cf name\uff0c\u7136\u5f8c\u5c07\u5b83\u50b3\u905e\u7d66 Child \u7d44\u4ef6\uff0c\u4e26\u5c07\u5b83\u4f5c\u70ba context \u50b3\u905e\u7d66 Child \u7d44\u4ef6\u3002Child \u7d44\u4ef6\u53ef\u4ee5\u4f7f\u7528 context \u4f86\u8a2a\u554f name \u8b8a\u91cf\uff0c\u4e26\u5728\u6e32\u67d3\u6642\u4f7f\u7528\u5b83\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\u3002\u6211\u5011\u4ecb\u7d39\u4e86\u5169\u7a2e\u4e3b\u8981\u7684\u65b9\u6cd5\uff1aprops \u548c context\u3002Props \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\u548c\u51fd\u6578\uff0c\u800c context \u5247\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\u3002\u901a\u904e\u4f7f\u7528\u9019\u5169\u7a2e\u65b9\u6cd5\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u7d44\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReact\u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684\u958b\u6e90JavaScript\u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\u4f86\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528\u7d44\u4ef6\u901a\u4fe1\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-5743","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-1uD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5743","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=5743"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5743\/revisions"}],"predecessor-version":[{"id":5744,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5743\/revisions\/5744"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}