{"id":298,"date":"2021-09-22T20:54:57","date_gmt":"2021-09-22T12:54:57","guid":{"rendered":"http:\/\/gjweb.top\/?p=298"},"modified":"2021-09-22T20:54:58","modified_gmt":"2021-09-22T12:54:58","slug":"%e7%bb%84%e4%bb%b6%e9%97%b4%e4%bc%a0%e5%80%bc%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=298","title":{"rendered":"\u7ec4\u4ef6\u95f4\u4f20\u503c\u8be6\u89e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7236\u5b50\u7ec4\u4ef6\u4f20\u503c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>\/*\u7236\u7ec4\u4ef6*\/<br>class Father extends React.Component{<br> &nbsp; &nbsp;constructor(props) {<br> &nbsp; &nbsp; &nbsp; &nbsp;super(props)<br> &nbsp; &nbsp; &nbsp; &nbsp;this.state = {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count:0<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;handleAdd = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;this.setState({<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count:this.state.count + 1<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  }<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return(<br> &nbsp; &nbsp; &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      \u7236\u5b50\u4f20\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;button onClick={this.handleAdd}&gt;\u52a0\u4e00&lt;\/button&gt;<br>             &nbsp; &lt;Child count={this.state.count}\/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<br>}<br>\/*\u5b50\u7ec4\u4ef6*\/<br>const Child = (props)=&gt;{<br> &nbsp; &nbsp;return(<br> &nbsp;      &lt;div&gt;\u5b50\u7ec4\u4ef6\u63a5\u6536 {props.count}&lt;\/div&gt;<br> &nbsp;  )<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b50\u7236\u7ec4\u4ef6\u4f20\u503c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>\/*\u7236\u7ec4\u4ef6*\/<br>class Father extends React.Component{<br> &nbsp; &nbsp;constructor(){<br> &nbsp; &nbsp; &nbsp; &nbsp;super()<br> &nbsp; &nbsp; &nbsp; &nbsp;this.state = {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getChildCount:0<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;getCount = (data)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;this.setState({<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getChildCount:data<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  }<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return(<br> &nbsp; &nbsp; &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      \u5b50\u7ec4\u4ef6\u4f20\u53c2 {this.state.getChildcount}<br> &nbsp;              &lt;Child getCount={this.getCount}\/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<br>}<br>\/*\u5b50\u7ec4\u4ef6*\/<br>class Child extends React.Component{<br> &nbsp; &nbsp;constructor(props){<br> &nbsp; &nbsp; &nbsp; &nbsp;super(props)<br> &nbsp; &nbsp; &nbsp; &nbsp;this.state = {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count:0<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;handelAdd = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;this.setState((state)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let newValue = state.count + 1 <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.props.getCount(newValue)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count:newValue<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  }<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return(<br> &nbsp; &nbsp; &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      \u5b50\u7ec4\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;button onClick={this.handelAdd}&gt;\u52a0\u4e00&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5144\u5f1f\u7ec4\u4ef6\u4f20<\/h2>\n\n\n\n<p>\u5144\u5f1f\u7ec4\u4ef6\u4f20\u503c\uff0c\u5b8c\u6210\u601d\u8def\u5728\u4e8e\u628a\u6240\u6709\u7684\u72b6\u6001\u653e\u5728\u7236\u7ec4\u4ef6\u4e0a\uff0c\u4e24\u4e2a\u5144\u5f1f\u7ec4\u4ef6\u901a\u8fc7\u7236\u5b50\u3001\u5b50\u7236\u4f20\u503c\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5144\u5f1f\u7ec4\u4ef6\u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>class Father extends React.Component {<br> &nbsp; &nbsp;constructor(props){<br> &nbsp; &nbsp; &nbsp; &nbsp;super(propss)<br> &nbsp; &nbsp; &nbsp; &nbsp;this.state = {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count:0<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;getCount = (data)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;this.setState((state)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count:state.count + data<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  }<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      \u5144\u5f1f\u7ec4\u4ef6\u4f20\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;Child count={this.state.count}\/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;Child2 getCount={this.getCount}\/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<br>\u200b<br>class Child extends React.Component {<br> &nbsp; &nbsp;constructor(props){<br> &nbsp; &nbsp; &nbsp; &nbsp;super(props)<br> &nbsp;  }<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;div&gt;\u5b50\u7ec4\u4ef6\u4e00&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;div&gt;\u8ba1\u6570\u5668\uff1a{this.props.count}&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<br>\u200b<br>class Child2 extends React.Component {<br> &nbsp; &nbsp;constructor(props){<br> &nbsp; &nbsp; &nbsp; &nbsp;super(props)<br> &nbsp;  }<br> &nbsp; &nbsp;handelAdd = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;this.props.getCount(1)<br> &nbsp;  }<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;div&gt;\u5b50\u7ec4\u4ef6\u4e8c&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;button onClick={this.handelAdd}&gt;\u52a0\u4e00&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7236\u5b50\u7ec4\u4ef6\u4f20\u503c \u5b50\u7236\u7ec4\u4ef6\u4f20\u503c \u5144\u5f1f\u7ec4\u4ef6\u4f20 \u5144\u5f1f\u7ec4\u4ef6\u4f20\u503c\uff0c\u5b8c\u6210\u601d\u8def\u5728\u4e8e\u628a\u6240\u6709\u7684\u72b6\u6001\u653e\u5728\u7236\u7ec4\u4ef6\u4e0a\uff0c\u4e24\u4e2a\u5144\u5f1f\u7ec4\u4ef6\u901a\u8fc7\u7236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-298","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/298","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=298"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}