{"id":4594,"date":"2023-01-04T16:36:09","date_gmt":"2023-01-04T08:36:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4594"},"modified":"2023-01-04T16:36:09","modified_gmt":"2023-01-04T08:36:09","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react-js%e7%b5%84%e4%bb%b6%e8%b3%87%e6%96%99%e5%82%b3%e9%81%9e","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react-js%e7%b5%84%e4%bb%b6%e8%b3%87%e6%96%99%e5%82%b3%e9%81%9e\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u5229\u7528React.js\u7d44\u4ef6\u8cc7\u6599\u50b3\u905e"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, React \u7d44\u4ef6\u8cc7\u6599\u50b3\u905e\"><\/p>\n<h1>React \u7d44\u4ef6\u8cc7\u6599\u50b3\u905e<\/h1>\n<p>React \u662f\u4e00\u500b\u7531 Facebook \u6240\u958b\u767c\u7684 JavaScript \u51fd\u5f0f\u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5efa\u7acb\u4e92\u52d5\u5f0f\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u3002React \u7684\u7d44\u4ef6\u8cc7\u6599\u50b3\u905e\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u8cc7\u6599\u5f9e\u7236\u7d44\u4ef6\u50b3\u905e\u5230\u5b50\u7d44\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u5b50\u7d44\u4ef6\u56de\u50b3\u8cc7\u6599\u7d66\u7236\u7d44\u4ef6\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 React \u4e2d\uff0c\u7236\u7d44\u4ef6\u53ef\u4ee5\u4f7f\u7528 <code>props<\/code> \u4f86\u50b3\u905e\u8cc7\u6599\u7d66\u5b50\u7d44\u4ef6\uff0c\u800c\u5b50\u7d44\u4ef6\u53ef\u4ee5\u4f7f\u7528 <code>state<\/code> \u4f86\u56de\u50b3\u8cc7\u6599\u7d66\u7236\u7d44\u4ef6\u3002<\/p>\n<h2>\u7236\u7d44\u4ef6\u50b3\u905e\u8cc7\u6599\u7d66\u5b50\u7d44\u4ef6<\/h2>\n<p>\u7236\u7d44\u4ef6\u53ef\u4ee5\u4f7f\u7528 <code>props<\/code> \u4f86\u50b3\u905e\u8cc7\u6599\u7d66\u5b50\u7d44\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass Parent extends React.Component {\n  render() {\n    return (\n      <Child name=\"John\" age=\"20\" \/>\n    );\n  }\n}\n\nclass Child extends React.Component {\n  render() {\n    return (\n      <div>\n        My name is {this.props.name}\n        My age is {this.props.age}\n      <\/div>\n    );\n  }\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u7236\u7d44\u4ef6 <code>Parent<\/code> \u5c07 <code>name<\/code> \u548c <code>age<\/code> \u7684\u8cc7\u6599\u50b3\u905e\u7d66\u5b50\u7d44\u4ef6 <code>Child<\/code>\uff0c\u5b50\u7d44\u4ef6 <code>Child<\/code> \u5c31\u53ef\u4ee5\u4f7f\u7528 <code>this.props.name<\/code> \u548c <code>this.props.age<\/code> \u4f86\u53d6\u5f97\u7236\u7d44\u4ef6\u50b3\u905e\u7684\u8cc7\u6599\u3002<\/p>\n<h2>\u5b50\u7d44\u4ef6\u56de\u50b3\u8cc7\u6599\u7d66\u7236\u7d44\u4ef6<\/h2>\n<p>\u5b50\u7d44\u4ef6\u53ef\u4ee5\u4f7f\u7528 <code>state<\/code> \u4f86\u56de\u50b3\u8cc7\u6599\u7d66\u7236\u7d44\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass Parent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = {\n      name: '',\n      age: 0\n    };\n  }\n\n  handleChildData = (name, age) => {\n    this.setState({\n      name: name,\n      age: age\n    });\n  }\n\n  render() {\n    return (\n      <Child onChildData={this.handleChildData} \/>\n    );\n  }\n}\n\nclass Child extends React.Component {\n  handleClick = () => {\n    this.props.onChildData('John', 20);\n  }\n\n  render() {\n    return (\n      <button onClick={this.handleClick}>Click me!<\/button>\n    );\n  }\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u7236\u7d44\u4ef6 <code>Parent<\/code> \u5c07 <code>handleChildData<\/code> \u7684\u51fd\u5f0f\u50b3\u905e\u7d66\u5b50\u7d44\u4ef6 <code>Child<\/code>\uff0c\u5b50\u7d44\u4ef6 <code>Child<\/code> \u5c31\u53ef\u4ee5\u4f7f\u7528 <code>this.props.onChildData<\/code> \u4f86\u547c\u53eb\u7236\u7d44\u4ef6\u50b3\u905e\u7684\u51fd\u5f0f\uff0c\u4e26\u4e14\u5c07\u8cc7\u6599\u56de\u50b3\u7d66\u7236\u7d44\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5229\u7528React.js\u7d44\u4ef6\u8cc7\u6599\u50b3\u905e\uff0c\u4e86\u89e3\u5982\u4f55\u5728React.js\u4e2d\u4f7f\u7528props\u548cstate\u4f86\u50b3\u905e\u8cc7\u6599\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528context\u4f86\u66f4\u6709\u6548\u5730\u7ba1\u7406\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-4594","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-1c6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4594","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=4594"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4594\/revisions"}],"predecessor-version":[{"id":4595,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4594\/revisions\/4595"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}