{"id":4576,"date":"2023-01-04T16:25:10","date_gmt":"2023-01-04T08:25:10","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4576"},"modified":"2023-01-04T16:25:10","modified_gmt":"2023-01-04T08:25:10","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react-js%e7%9a%84%e7%b5%84%e4%bb%b6%e5%b1%ac%e6%80%a7%e5%82%b3%e9%81%9epassingprops","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%9a%84%e7%b5%84%e4%bb%b6%e5%b1%ac%e6%80%a7%e5%82%b3%e9%81%9epassingprops\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u5229\u7528React.js\u7684\u7d44\u4ef6\u5c6c\u6027\u50b3\u905e(PassingProps)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, React \u7d44\u4ef6\u5c6c\u6027\u50b3\u905e, Props\"><\/p>\n<h1>React \u7d44\u4ef6\u5c6c\u6027\u50b3\u905e(passing props)<\/h1>\n<p>React \u7d44\u4ef6\u5c6c\u6027\u50b3\u905e\uff08passing props\uff09\u662f React \u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u52a0\u7684\u53ef\u64f4\u5145\u3002<\/p>\n<p>\u5728 React \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 props \u4f86\u50b3\u905e\u8cc7\u6599\uff0cprops \u662f\u4e00\u500b\u7269\u4ef6\uff0c\u5b83\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u4f60\u60f3\u8981\u50b3\u905e\u7684\u8cc7\u6599\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;MyComponent name=\"John\" age={30} \/&gt;\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u50b3\u905e\u4e86\u5169\u500b props\uff0cname \u548c age\uff0cname \u662f\u4e00\u500b\u5b57\u4e32\uff0c\u800c age \u662f\u4e00\u500b\u6578\u5b57\u3002<\/p>\n<p>\u5728\u7d44\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 this.props \u4f86\u5b58\u53d6 props\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">class MyComponent extends React.Component {\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;h1&gt;Hello, {this.props.name}!&lt;\/h1&gt;\n        &lt;p&gt;You are {this.props.age} years old.&lt;\/p&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 this.props.name \u4f86\u53d6\u5f97 name \u7684\u503c\uff0c\u4ee5\u53ca this.props.age \u4f86\u53d6\u5f97 age \u7684\u503c\u3002<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 props \u4f86\u50b3\u905e\u51fd\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;MyComponent onClick={this.handleClick} \/&gt;\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u50b3\u905e\u4e86\u4e00\u500b\u51fd\u5f0f\uff0c\u53eb\u505a handleClick\uff0c\u5728\u7d44\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 this.props.onClick \u4f86\u53d6\u5f97\u51fd\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">class MyComponent extends React.Component {\n  handleClick() {\n    \/\/ do something\n  }\n\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;button onClick={this.props.onClick}&gt;Click Me!&lt;\/button&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 this.props.onClick \u4f86\u53d6\u5f97\u50b3\u905e\u7684\u51fd\u5f0f\uff0c\u4e26\u4e14\u5c07\u5b83\u8a2d\u5b9a\u70ba\u6309\u9215\u7684 onClick \u4e8b\u4ef6\u8655\u7406\u51fd\u5f0f\u3002<\/p>\n<p>React \u7d44\u4ef6\u5c6c\u6027\u50b3\u905e\uff08passing props\uff09\u662f React \u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u52a0\u7684\u53ef\u64f4\u5145\u3002\u900f\u904e props\uff0c\u4f60\u53ef\u4ee5\u50b3\u905e\u4efb\u4f55\u4f60\u60f3\u8981\u50b3\u905e\u7684\u8cc7\u6599\uff0c\u4f8b\u5982\u5b57\u4e32\u3001\u6578\u5b57\u3001\u7269\u4ef6\u3001\u51fd\u5f0f\u7b49\u7b49\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u7d44\u4ef6\u4e2d\u4f7f\u7528 this.props \u4f86\u53d6\u5f97\u50b3\u905e\u7684\u8cc7\u6599\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 React \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 props \u4f86\u50b3\u905e\u8cc7\u6599\uff0cprops \u662f\u4e00\u500b\u7269\u4ef6\uff0c\u5b83\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u4f60\u60f3\u8981\u50b3\u905e\u7684\u8cc7\u6599\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;MyComponent name=\"John\" age={30} \/&gt;\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u50b3\u905e\u4e86\u5169\u500b props\uff0cname \u548c age\uff0cname \u662f\u4e00\u500b\u5b57\u4e32\uff0c\u800c age \u662f\u4e00\u500b\u6578\u5b57\u3002<\/p>\n<p>\u5728\u7d44\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <span style=\"color: #f00; font-weight: bold;\">this.props<\/span> \u4f86\u5b58\u53d6 props\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">class MyComponent extends React.Component {\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;h1&gt;Hello, {this.props.name}!&lt;\/h1&gt;\n        &lt;p&gt;You are {this.props.age} years old.&lt;\/p&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 this.props.name \u4f86\u53d6\u5f97 name \u7684\u503c\uff0c\u4ee5\u53ca this.props.age \u4f86\u53d6\u5f97 age \u7684\u503c\u3002<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 props \u4f86\u50b3\u905e\u51fd\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;MyComponent onClick={this.handleClick} \/&gt;\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u50b3\u905e\u4e86\u4e00\u500b\u51fd\u5f0f\uff0c\u53eb\u505a handleClick\uff0c\u5728\u7d44\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 this.props.onClick \u4f86\u53d6\u5f97\u51fd\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-js line-numbers\">class MyComponent extends React.Component {\n  handleClick() {\n    \/\/ do something\n  }\n\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;button onClick={this.props.onClick}&gt;Click Me!&lt;\/button&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 this.props.onClick \u4f86\u53d6\u5f97\u50b3\u905e\u7684\u51fd\u5f0f\uff0c\u4e26\u4e14\u5c07\u5b83\u8a2d\u5b9a\u70ba\u6309\u9215\u7684 onClick \u4e8b\u4ef6\u8655\u7406\u51fd\u5f0f\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cReact \u7d44\u4ef6\u5c6c\u6027\u50b3\u905e\uff08passing props\uff09\u662f React \u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u52a0\u7684\u53ef\u64f4\u5145\u3002\u900f\u904e props\uff0c\u4f60\u53ef\u4ee5\u50b3\u905e\u4efb\u4f55\u4f60\u60f3\u8981\u50b3\u905e\u7684\u8cc7\u6599\uff0c\u4f8b\u5982\u5b57\u4e32\u3001\u6578\u5b57\u3001\u7269\u4ef6\u3001\u51fd\u5f0f\u7b49\u7b49\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u7d44\u4ef6\u4e2d\u4f7f\u7528 this.props \u4f86\u53d6\u5f97\u50b3\u905e\u7684\u8cc7\u6599\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReact.js\u7684\u7d44\u4ef6\u5c6c\u6027\u50b3\u905e(PassingProps)\u662f\u4e00\u7a2e\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728React.js\u4e2d\u5c07\u8cc7\u6599\u5f9e\u4e00\u500b\u7d44\u4ef6\u50b3\u905e\u5230\u53e6\u4e00\u500b\u7d44\u4ef6\u7684\u65b9\u6cd5\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5229\u7528React.js\u7684\u7d44\u4ef6\u5c6c\u6027\u50b3\u905e(PassingProps)\u4f86\u5efa\u7acb\u66f4\u6709\u6548\u7387\u7684\u61c9\u7528\u7a0b\u5f0f\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-4576","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-1bO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4576","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=4576"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4576\/revisions"}],"predecessor-version":[{"id":4577,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4576\/revisions\/4577"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}