{"id":482,"date":"2023-02-25T13:53:46","date_gmt":"2023-02-25T05:53:46","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=482"},"modified":"2023-04-29T20:52:59","modified_gmt":"2023-04-29T12:52:59","slug":"react-native-learning-reactjs-component-communication","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-reactjs-component-communication\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4bReactJS\u7ec4\u4ef6\u901a\u4fe1"},"content":{"rendered":"<h2>ReactJS\u7ec4\u4ef6\u901a\u4fe1<\/h2>\n<p>ReactJS\u7ec4\u4ef6\u5173\u7cfb\u662f\u5d4c\u5957\u7684\uff0c\u56e0\u4e3a\u4f7f\u7528DOM\u7ed3\u6784\uff0c\u7ec4\u7ec7\u7ed3\u6784\u6bd4\u8f83\u6e05\u6670\u3002<\/p>\n<p>\u56e0\u6b64ReactJS\u7ec4\u4ef6\u5305\u542b\u7236\u7ec4\u4ef6\u4e0e\u5b50\u7ec4\u4ef6<\/p>\n<p><!-- more --><\/p>\n<p>\uff081\uff09\u5b50\u7ec4\u4ef6\u5982\u4f55\u8c03\u7528\u7236\u7ec4\u4ef6<\/p>\n<p>this.props<\/p>\n<p>\uff082\uff09\u7236\u7ec4\u4ef6\u5982\u4f55\u8c03\u7528\u5b50\u7ec4\u4ef6<\/p>\n<p>\u9996\u5148\u4f7f\u7528\u5c5e\u6027ref\u4e3a\u5b50\u7ec4\u4ef6\u53d6\u4e2a\u540d\u5b57<\/p>\n<p>this.refs.\u540d\u5b57.getDOMNode()<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head lang=&quot;en&quot;&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;React\u7ec4\u4ef6\u901a\u4fe1&lt;\/title&gt;\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;react.js&quot;&gt;&lt;\/script&gt;\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;react-dom.js&quot;&gt;&lt;\/script&gt;\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;browser.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div id=&quot;example&quot;&gt;&lt;\/div&gt;\n&lt;script type=&quot;text\/babel&quot;&gt;\n    var Parent = React.createClass({\n        click:function(){\n            \/\/\u7236\u7ec4\u4ef6\u8c03\u7528\u5b50\u7ec4\u4ef6\n            this.refs.child.getDOMNode().style.color=&quot;red&quot;;\n        },\n\n        render:function(){\n            return (\n                &lt;div onClick={this.click} &gt;Parent is :\n                \/\/\u5b50\u7ec4\u4ef6\u8c03\u7528\u7236\u7ec4\u4ef6\n                &lt;Child name={this.props.name} ref=&quot;child&quot;&gt;&lt;\/Child&gt;\n                &lt;\/div&gt;\n            );\n        }\n    });\n\n    var Child = React.createClass({\n        render:function(){\n            return &lt;span&gt; {this.props.name} &lt;\/span&gt;;\n        }\n    });\n\n    ReactDOM.render(&lt;Parent name=&quot;React\u8bed\u6cd5\u57fa\u7840&quot; \/&gt;, document.getElementById(&#039;example&#039;));\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>JSX\u5b9e\u6218<\/h2>\n<p>React Native\u4e2d\u6ca1\u6709DOM\u7684\u6982\u5ff5\uff0c\u53ea\u6709\u7ec4\u4ef6\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u6211\u4eec\u5728ReactJS\u4e2d\u4f7f\u7528\u7684Html\u6807\u7b7e\u4ee5\u53ca\u5bf9DOM\u7684\u64cd\u4f5c\u662f\u4e0d\u8d77\u4f5c\u7528\u7684\uff0c\u4f46\u662f\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u3001JSX\u7684\u8bed\u6cd5\u3001\u4e8b\u4ef6\u7ed1\u5b9a\u3001\u81ea\u5b9a\u4e49\u5c5e\u6027\u7b49\uff0c\u5728React Native\u548cReactJS\u4e2d\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<h3>React Native\u7684ReactJs\u5199\u6cd5<\/h3>\n<pre><code class=\"language-html\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n&#039;use strict&#039;;\nimport React, {\n  AppRegistry,\n  Component,\n  StyleSheet,\n  Text,\n  View\n} from &#039;react-native&#039;;\n\nclass Box extends Component {\n  render(){\n  return (\n      &lt;View style={[BoxStyles.box,BoxStyles[this.props.width],BoxStyles[this.props.height]]}&gt;\n        &lt;View  style={[BoxStyles.top,BoxStyles.height50,BoxStyles[this.props.classBg]]}&gt;&lt;Text&gt;top&lt;\/Text&gt;&lt;\/View&gt;\n        &lt;View style={[BoxStyles[this.props.childName]]}&gt;\n          &lt;View style={[BoxStyles.left,BoxStyles[this.props.classBg]]}&gt;&lt;Text&gt;left&lt;\/Text&gt;&lt;\/View&gt;\n          {this.props.children}\n          &lt;View style={[BoxStyles.right,BoxStyles[this.props.classBg]]}&gt;&lt;Text&gt;right&lt;\/Text&gt;&lt;\/View&gt;\n        &lt;\/View&gt;\n        &lt;View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles[this.props.classBg]]}&gt;&lt;Text&gt;bottom&lt;\/Text&gt;&lt;\/View&gt;\n        &lt;View style={[BoxStyles.label]}&gt;&lt;Text&gt;{this.props.boxName}&lt;\/Text&gt;&lt;\/View&gt;\n      &lt;\/View&gt;\n  )}\n}\n\nclass MargginBox extends Component{\n  render(){\n  return (\n      &lt;View style={[BoxStyles.margginBox]}&gt;\n        &lt;Box  childName=&quot;borderBox&quot; height=&quot;height400&quot; width=&quot;width400&quot; boxName=&quot;margin&quot; classBg=&quot;bgred&quot;&gt;{this.props.children}&lt;\/Box&gt;\n      &lt;\/View&gt;\n  )}\n}\n\nclass BorderBox extends Component{\n  render(){\n  return (\n      &lt;Box childName=&quot;paddingBox&quot; height=&quot;height300&quot; width=&quot;width300&quot; boxName=&quot;border&quot; classBg=&quot;bggreen&quot; &gt;{this.props.children}&lt;\/Box&gt;\n  )}\n}\n\nclass PaddingBox extends Component{\n  render(){\n  return (\n      &lt;Box childName=&quot;elementBox&quot; height=&quot;height200&quot; width=&quot;width200&quot; boxName=&quot;padding&quot; classBg=&quot;bgyellow&quot; &gt;{this.props.children}&lt;\/Box&gt;\n  )}\n}\n\nclass ElementBox extends Component{\n  render(){\n  return (\n      &lt;View style={[BoxStyles.box,BoxStyles.height100]}&gt;\n        &lt;View style={[BoxStyles.measureBox]}&gt;\n          &lt;View style={[BoxStyles.right]}&gt;&lt;Text&gt;height&lt;\/Text&gt;&lt;\/View&gt;\n        &lt;\/View&gt;\n        &lt;View style={[BoxStyles.bottom,BoxStyles.height50]} &gt;&lt;Text&gt;width&lt;\/Text&gt;&lt;\/View&gt;\n        &lt;View style={[BoxStyles.label]}&gt;&lt;Text&gt;element&lt;\/Text&gt;&lt;\/View&gt;\n        &lt;View style={[BoxStyles.widthdashed]}&gt;&lt;\/View&gt;\n        &lt;View style={[BoxStyles.heightdashed]}&gt;&lt;\/View&gt;\n      &lt;\/View&gt;\n  )}\n}\n\nclass AppBlog extends Component {\n  render(){\n  return (\n      &lt;MargginBox&gt;\n        &lt;BorderBox&gt;\n          &lt;PaddingBox&gt;\n            &lt;ElementBox&gt;\n            &lt;\/ElementBox&gt;\n          &lt;\/PaddingBox&gt;\n        &lt;\/BorderBox&gt;\n      &lt;\/MargginBox&gt;\n  )}\n}\n\nconst BoxStyles = StyleSheet.create({\n  height50:{\n    height: 50,\n  },\n  height400:{\n    height: 400,\n  },\n  height300:{\n    height: 300,\n  },\n  height200:{\n    height: 200,\n  },\n  height100:{\n    height: 100,\n  },\n  width400:{\n    width: 400,\n  },\n  width300:{\n    width: 300,\n  },\n  width200:{\n    width: 200,\n  },\n  width100:{\n    width: 100,\n  },\n  bgred: {\n    backgroundColor:&#039;#6AC5AC&#039;,\n  },\n  bggreen: {\n    backgroundColor: &#039;#414142&#039;,\n  },\n  bgyellow: {\n    backgroundColor: &#039;#D64078&#039;,\n  },\n  box: {\n    flexDirection: &#039;column&#039;,\n    flex: 1,\n    position: &#039;relative&#039;,\n  },\n  label: {\n    top: 0,\n    left: 0,\n    paddingTop: 0,\n    paddingRight: 3,\n    paddingBottom: 3,\n    paddingLeft: 0,\n    position: &#039;absolute&#039;,\n    backgroundColor: &#039;#FDC72F&#039;,\n  },\n  top: {\n    justifyContent: &#039;center&#039;,\n    alignItems: &#039;center&#039;,\n  },\n  bottom: {\n    justifyContent: &#039;center&#039;,\n    alignItems: &#039;center&#039;,\n  },\n  right: {\n    width: 50,\n    justifyContent: &#039;space-around&#039;,\n    alignItems: &#039;center&#039;,\n  },\n  left: {\n    width: 50,\n    justifyContent: &#039;space-around&#039;,\n    alignItems: &#039;center&#039;,\n  },\n  heightdashed: {\n    bottom: 0,\n    top: 0,\n    right: 20,\n    borderLeftWidth: 1,\n    position: &#039;absolute&#039;,\n    borderLeftColor: &#039;#FDC72F&#039;,\n  },\n  widthdashed: {\n    bottom: 25,\n    left: 0,\n    right: 0,\n    borderTopWidth: 1,\n    position: &#039;absolute&#039;,\n    borderTopColor: &#039;#FDC72F&#039;,\n  },\n  yellow: {\n    color: &#039;#FDC72F&#039;,\n    fontWeight:&#039;900&#039;,\n  },\n  white: {\n    color: &#039;white&#039;,\n    fontWeight:&#039;900&#039;,\n  },\n  margginBox:{\n    position: &#039;absolute&#039;,\n    top: 100,\n    paddingLeft:7,\n    paddingRight:7,\n  },\n  borderBox:{\n    flex: 1,\n    justifyContent: &#039;space-between&#039;,\n    flexDirection: &#039;row&#039;,\n  },\n  paddingBox:{\n    flex: 1,\n    justifyContent: &#039;space-between&#039;,\n    flexDirection: &#039;row&#039;,\n  },\n  elementBox:{\n    flex: 1,\n    justifyContent: &#039;space-between&#039;,\n    flexDirection: &#039;row&#039;,\n  },\n  measureBox:{\n    flex: 1,\n    flexDirection: &#039;row&#039;,\n    justifyContent: &#039;flex-end&#039;,\n    alignItems:&#039;flex-end&#039;,\n  },\n  container: {\n    flex: 1,\n    justifyContent: &#039;center&#039;,\n    alignItems: &#039;center&#039;,\n    backgroundColor: &#039;#F5FCFF&#039;,\n  },\n  welcome: {\n    fontSize: 20,\n    textAlign: &#039;center&#039;,\n    margin: 10,\n  },\n  instructions: {\n    textAlign: &#039;center&#039;,\n    color: &#039;#333333&#039;,\n    marginBottom: 5,\n  },\n});\n\nAppRegistry.registerComponent(&#039;AppBlog&#039;, () =&gt; AppBlog);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ReactJS\u7ec4\u4ef6\u901a\u4fe1 ReactJS\u7ec4\u4ef6\u5173\u7cfb\u662f\u5d4c\u5957\u7684\uff0c\u56e0\u4e3a\u4f7f\u7528DOM\u7ed3\u6784\uff0c\u7ec4\u7ec7\u7ed3\u6784\u6bd4\u8f83\u6e05\u6670\u3002 \u56e0\u6b64React [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[162],"class_list":["post-482","post","type-post","status-publish","format-standard","hentry","category-react-native","tag-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}