{"id":481,"date":"2023-02-25T13:52:53","date_gmt":"2023-02-25T05:52:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=481"},"modified":"2023-04-29T20:53:08","modified_gmt":"2023-04-29T12:53:08","slug":"react-native-learning-jsx-and-reactjs","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-jsx-and-reactjs\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4bJSX\u4e0eReactJS"},"content":{"rendered":"<h2>JSX<\/h2>\n<p>React\u662f\u7531ReactJS\u4e0eReact Native\u7ec4\u6210\uff0c\u5176\u4e2dReactJS\u662fFacebook\u5f00\u6e90\u7684\u4e00\u4e2a\u524d\u7aef\u6846\u67b6\uff0cReact Native\u662fReactJS\u601d\u60f3\u5728Native\u4e0a\u7684\u4f53\u73b0\uff01<\/p>\n<p>JSX\u5e76\u4e0d\u662f\u4e00\u95e8\u65b0\u7684\u8bed\u8a00\uff0c\u4ec5\u4ec5\u662f\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728JavaScript\u4e2d\u4e66\u5199HTML\u8bed\u6cd5\uff0c\u6700\u540e\u6bcf\u4e2aHTML\u6807\u7b7e\u90fd\u8f6c\u5316\u4e3aJavaScript\u4ee3\u7801\u6765\u8fd0\u884c\u3002<\/p>\n<p><!-- more --><\/p>\n<h3>\u73af\u5883<\/h3>\n<p>ReactJS\u73af\u5883\uff0c\u5982\uff1a<\/p>\n<pre><code class=\"language-html\">&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;<\/code><\/pre>\n<h3>\u8f7d\u5165\u65b9\u5f0f<\/h3>\n<p>\u5185\u8054\/\u5916\u8054\uff0c\u5982<\/p>\n<pre><code class=\"language-javascript\">var HelloMessage = React.createClass({\n    render:function(){\n        return &lt;h1&gt;Hello {this.props.name}! &lt;br \/&gt;www.appblog.cn&lt;\/h1&gt;;\n    }\n});<\/code><\/pre>\n<h3>\u6807\u7b7e<\/h3>\n<ul>\n<li>HTML\u6807\u7b7e<\/li>\n<li>ReactJS\u521b\u5efa\u7684\u7ec4\u4ef6\u7c7b\u6807\u7b7e\uff08\u9996\u5b57\u6bcd\u4e00\u5b9a\u8981\u5927\u5199\uff09<\/li>\n<\/ul>\n<h3>\u8f6c\u6362\uff08\u89e3\u6790\u5668\uff09<\/h3>\n<pre><code class=\"language-javascript\">&lt;h3&gt;\u8f93\u5165&lt;\/h3&gt;\n\/\/\u8f6c\u6362\u6210\nReact.createElement(&quot;h3&quot;, null, &quot;\u8f93\u5165&quot;);  \/\/\u8fd4\u56de\u4e00\u4e2aReactElement\u5bf9\u8c61<\/code><\/pre>\n<h3>\u6267\u884cJavaScript\u8868\u8fbe\u5f0f<\/h3>\n<pre><code class=\"language-javascript\">var msg = &quot;www.appblog.cn&quot;;\n&lt;h1&gt;{msg}&lt;\/h1&gt;\n\/\/\u8f6c\u6362\u6210\nReact.createElement(&quot;h1&quot;, null, msg);<\/code><\/pre>\n<h3>\u6ce8\u91ca<\/h3>\n<ul>\n<li>\u5355\u884c\uff1a\/\/<\/li>\n<li>\u591a\u884c\uff1a\/*\u6ce8\u91ca\u6587\u672c*\/<\/li>\n<\/ul>\n<h3>\u5c5e\u6027<\/h3>\n<pre><code class=\"language-javascript\">var msg = &lt;h1 width=&quot;10px&quot;&gt;www.appblog.cn&lt;\/h1&gt;\n\/\/\u8f6c\u6362\u6210\nReact.createElement(&quot;h1&quot;, {width:&quot;10px&quot;}, &quot;www.appblog.cn&quot;)<\/code><\/pre>\n<h3>\u5ef6\u5c55\u5c5e\u6027<\/h3>\n<p>\u4f7f\u7528ES6\u7684\u8bed\u6cd5<\/p>\n<pre><code class=\"language-javascript\">var props = {};\nprops.foo = &quot;1&quot;;\nprops.bar = &quot;1&quot;;\n&lt;h1 {...props} foo=&quot;2&quot; &gt;www.appblog.cn&lt;\/h1&gt;\n\/\/\u8f6c\u6362\u6210\nReact.createElement(&quot;h1&quot;, React.__spread({}, props, {foo:&quot;2&quot;}), &quot;www.appblog.cn&quot;);<\/code><\/pre>\n<h3>\u81ea\u5b9a\u4e49\u5c5e\u6027<\/h3>\n<p>HTML5\u7ed9\u51fa\u7684\u65b9\u6848\uff0c\u51e1\u662f\u4ee5data-\u5f00\u5934\u7684\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u53ef\u6e32\u67d3\u5230\u9875\u9762<\/p>\n<h3>\u663e\u793aHTML<\/h3>\n<p>\u663e\u793a\u4e00\u6bb5HMTL\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662fhtml\u8282\u70b9<\/p>\n<p>\u501f\u52a9\u4e00\u4e2a\u5c5e\u6027 _html<\/p>\n<pre><code class=\"language-html\">&lt;div&gt;{{_html:&#039;&lt;h1&gt;www.appblog.cn&lt;\/h1&gt;&#039;}}&lt;\/div&gt;<\/code><\/pre>\n<h3>\u6837\u5f0f\u7684\u4f7f\u7528<\/h3>\n<p>style\u5c5e\u6027\uff1a\u5916\u5c42{}\u6309\u7167JSX\u8bed\u6cd5\uff0c\u5185\u5c42{}\u662fJavaScript\u5bf9\u8c61<\/p>\n<pre><code class=\"language-javascript\">var HelloMessage = React.createClass({\n    render:function(){\n        return &lt;h1 style={{color:&#039;#ff0000&#039;,fontSize:&#039;24px&#039;}} &gt;Hello {this.props.name}! &lt;br \/&gt;www.appblog.cn&lt;\/h1&gt;;\n    }\n});<\/code><\/pre>\n<h3>\u6837\u5f0f\u7684\u4f7f\u7528\u4e8b\u4ef6\u7ed1\u5b9a<\/h3>\n<p>\u6ce8\u610f\uff1aonClick \u8c03\u7528bind\u65b9\u6cd5\uff08\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8bbe\u5b9a\u7684\u4f5c\u7528\u57df\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8981\u4f20\u9012\u7684\u53c2\u6570\uff09<\/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\u4e8b\u4ef6\u7ed1\u5b9a&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\n    function testClick(msg){\n        alert(msg);\n    }\n\n    var app=&lt;button onClick={testClick.bind(this, &#039;hello&#039;)} style={{\n        backgroundColor:&#039;#ff0000&#039;,\n        fontSize:&#039;28px&#039;,\n        width:&#039;200px&#039;,\n        height:&#039;50px&#039;\n    }} &gt;\u70b9\u51fb\u6211\u5427&lt;\/button&gt;\n\n    React.render(app, document.getElementById(&#039;example&#039;));\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>ReactJS<\/h2>\n<p>ReactJS\u6838\u5fc3\u601d\u60f3\uff1a\u7ec4\u4ef6\u5316<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u7ec4\u4ef6\u7ef4\u62a4\u81ea\u5df1\u7684\u72b6\u6001\u548cUI\uff0c\u5e76\u4e14\u80fd\u591f\u81ea\u52a8\u91cd\u65b0\u6e32\u67d3  <\/li>\n<li>\u591a\u4e2a\u7ec4\u4ef6\u7ec4\u6210\u4e86\u4e00\u4e2aReactJS\u5e94\u7528<\/li>\n<\/ul>\n<h3>\u9876\u5c42API<\/h3>\n<p>React\u662f\u5168\u5c40\u5bf9\u8c61<\/p>\n<ul>\n<li>React.createClass\uff1a\u521b\u5efa\u7ec4\u4ef6\u7c7b\u7684\u65b9\u6cd5<\/li>\n<li>React.render\uff1a\u6e32\u67d3\uff0c\u5c06\u6307\u5b9a\u7ec4\u4ef6\u6e32\u67d3\u5230\u6307\u5b9aDOM\u8282\u70b9<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\uff1aReact.render \u88ab ReactDOM.render \u66ff\u4ee3<\/p>\n<\/blockquote>\n<h3>\u7ec4\u4ef6API<\/h3>\n<ul>\n<li>render\uff1a\u7ec4\u4ef6\u7ea7API\uff0c\u8fd4\u56de\u7ec4\u4ef6\u7684\u5185\u90e8\u7ed3\u6784<\/li>\n<li>\u7ec4\u4ef6\u751f\u547d\u5468\u671fAPI<\/li>\n<\/ul>\n<h2>ReactJS\u7ec4\u4ef6\u751f\u547d\u5468\u671f<\/h2>\n<h3>\u521b\u5efa\u9636\u6bb5<\/h3>\n<p><strong>getDefaultProps<\/strong>\uff1a\u5904\u7406props\u7684\u9ed8\u8ba4\u503c\uff0c\u5728React.createClass\u8c03\u7528<\/p>\n<h3>\u5b9e\u4f8b\u5316\u9636\u6bb5<\/h3>\n<p>React.render \u542f\u52a8\u4e4b\u540e\u4f9d\u6b21\u8c03\u7528\uff1a<\/p>\n<p><strong>getInitialState<\/strong>\u3001<strong>componentWillMount<\/strong>\u3001<strong>render<\/strong>\u3001<strong>componentDidMount<\/strong><\/p>\n<p>state\uff1a\u7ec4\u4ef6\u7684\u5c5e\u6027\uff0c\u4e3b\u8981\u662f\u7528\u6765\u5b58\u50a8\u7ec4\u4ef6\u81ea\u8eab\u9700\u8981\u7684\u6570\u636e\uff0c\u6bcf\u6b21\u6570\u636e\u7684\u66f4\u65b0\u90fd\u662f\u901a\u8fc7\u4fee\u6539state\u5c5e\u6027\u7684\u503c\uff0cReactJS\u5185\u90e8\u4f1a\u76d1\u542cstate\u5c5e\u6027\u7684\u53d8\u5316\uff0c\u4e00\u65e6\u53d1\u751f\u53d8\u5316\uff0c\u5219\u4e3b\u52a8\u89e6\u53d1\u7ec4\u4ef6\u7684render\u65b9\u6cd5\u6765\u66f4\u65b0\u865a\u62dfDOM\u7ed3\u6784<\/p>\n<p>\u865a\u62dfDOM\uff1a\u5c06\u771f\u5b9e\u7684DOM\u7ed3\u6784\u5728\u5185\u5b58\u4e2d\u6620\u5c04\u6210\u4e00\u4e2aJSON\u6570\u636e\u7ed3\u6784<\/p>\n<h3>\u66f4\u65b0\u9636\u6bb5<\/h3>\n<p>\u4e3b\u8981\u53d1\u751f\u5728\u7528\u6237\u64cd\u4f5c\u4e4b\u540e\u6216\u7236\u7ec4\u4ef6\u6709\u66f4\u65b0\u7684\u65f6\u5019\uff0c\u6b64\u65f6\u4f1a\u6839\u636e\u7528\u6237\u7684\u64cd\u4f5c\u884c\u4e3a\u8fdb\u884c\u76f8\u5e94\u7684\u9875\u9762\u7ed3\u6784\u7684\u8c03\u6574\u3002\u66f4\u65b0\u884c\u4e3a\u4f9d\u6b21\u8c03\u7528\uff1a<\/p>\n<p><strong>componentWillReceiveProps<\/strong>\u3001<strong>shouldComponentUpdate<\/strong>\u3001<strong>componentWillUpdate<\/strong>\u3001<strong>render<\/strong>\u3001<strong>componentDidUpdate<\/strong><\/p>\n<h3>\u9500\u6bc1\u9636\u6bb5<\/h3>\n<p>\u9500\u6bc1\u65f6\u88ab\u8c03\u7528\uff0c\u901a\u5e38\u505a\u4e00\u4e9b\u53d6\u6d88\u4e8b\u4ef6\u7ed1\u5b9a\u3001\u79fb\u9664\u865a\u62dfDOM\u4e2d\u5bf9\u5e94\u7684\u7ec4\u4ef6\u6570\u636e\u7ed3\u6784\u3001\u9500\u6bc1\u4e00\u4e9b\u65e0\u6548\u7684\u5b9a\u65f6\u5668\u7b49\u5de5\u4f5c\u3002\u8c03\u7528\uff1a<\/p>\n<p><strong>componentWillUnmount<\/strong><\/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\u7684\u751f\u547d\u5468\u671f&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 HelloMessage=React.createClass({\n        \/\/1\u3001\u521b\u5efa\u9636\u6bb5\n        getDefaultProps:function(){\n            \/\/\u5728\u521b\u5efa\u7c7b\u7684\u65f6\u5019\u88ab\u8c03\u7528\uff0cthis.props\u5904\u7406\u8be5\u7ec4\u4ef6\u7684\u9ed8\u8ba4\u5c5e\u6027\n            console.log(&quot;getDefaultProps&quot;);\n            return {};\n        },\n\n        \/\/2\u3001\u5b9e\u4f8b\u5316\u9636\u6bb5\n        getInitialState:function(){\n            \/\/\u521d\u59cb\u5316\u7ec4\u4ef6\u7684state\u503c\uff0c\u5176\u8fd4\u56de\u503c\u4f1a\u8d4b\u503c\u7ed9\u7ec4\u4ef6\u7684this.state\u5c5e\u6027\n            \/\/\u83b7\u53d6this.state\u7684\u9ed8\u8ba4\u503c\n            console.log(&quot;getInitialState&quot;);\n            return {};\n        },\n\n        componentWillMount:function(){\n            \/\/\u5728render\u4e4b\u524d\u8c03\u7528\u6b64\u65b9\u6cd5\n            \/\/\u4e1a\u52a1\u903b\u8f91\u7684\u5904\u7406\u90fd\u5e94\u8be5\u653e\u5728\u8fd9\u91cc\uff0c\u5982\u5bf9state\u7684\u64cd\u4f5c\u7b49\n            console.log(&quot;componentWillMount&quot;);\n        },\n\n        render:function(){\n            \/\/\u6839\u636estate\u503c\uff0c\u6e32\u67d3\u5e76\u8fd4\u56de\u4e00\u4e2a\u865a\u62dfDOM\n            console.log(&quot;render&quot;);\n            return &lt;h1 style={{color:&#039;#ff0000&#039;,fontSize:&#039;24px&#039;}} &gt;Hello {this.props.name}!&lt;br \/&gt;www.appblog.cn&lt;\/h1&gt;;\n        },\n\n        componentDidMount:function(){\n            \/\/\u8be5\u65b9\u4fbf\u53d1\u751f\u5728render\u65b9\u6cd5\u4e4b\u540e\n            \/\/\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0cReactJS\u4f1a\u4f7f\u7528render\u65b9\u6cd5\u8fd4\u56de\u7684\u865a\u62dfDOM\u5bf9\u8c61\u6765\u521b\u5efa\u771f\u5b9e\u7684DOM\u7ed3\u6784\n            \/\/\u7ec4\u4ef6\u5185\u90e8\u53ef\u4ee5\u901a\u8fc7this.getDOMNode()\u6765\u83b7\u53d6\u5f53\u524d\u7ec4\u4ef6\u7684\u8282\u70b9\n            console.log(&quot;componentDidMount&quot;);\n        },\n\n        \/\/3\u3001\u66f4\u65b0\u9636\u6bb5\uff0c\u4e3b\u8981\u53d1\u751f\u5728\u7528\u6237\u64cd\u4f5c\u4e4b\u540e\u6216\u7236\u7ec4\u4ef6\u6709\u66f4\u65b0\u7684\u65f6\u5019\uff0c\u6b64\u65f6\u4f1a\u6839\u636e\u7528\u6237\u7684\u64cd\u4f5c\u884c\u4e3a\u8fdb\u884c\u76f8\u5e94\u7684\u9875\u9762\u7ed3\u6784\u7684\u8c03\u6574\n        componentWillReceiveProps:function(){\n            \/\/\u8be5\u65b9\u6cd5\u53d1\u751f\u5728this.props\u88ab\u4fee\u6539\u6216\u7236\u7ec4\u4ef6\u8c03\u7528setProps()\u65b9\u6cd5\u4e4b\u540e\n            \/\/\u8c03\u7528this.setState\u65b9\u6cd5\u6765\u5b8c\u6210\u5bf9state\u7684\u4fee\u6539\n            console.log(&quot;componentWillRecieveProps&quot;);\n        },\n\n        shouldComponentUpdate:function(){\n            \/\/\u7528\u6765\u62e6\u622a\u65b0\u7684props\u6216state\uff0c\u6839\u636e\u903b\u8f91\u6765\u5224\u65ad\n            \/\/\u662f\u5426\u9700\u8981\u66f4\u65b0\n            console.log(&quot;shouldComponentUpdate&quot;);\n            return true;\n        },\n\n        componentWillUpdate:function(){\n            \/\/shouldComponentUpdate\u8fd4\u56detrue\u7684\u65f6\u5019\u6267\u884c\n            \/\/\u7ec4\u4ef6\u5c06\u66f4\u65b0\n            console.log(&quot;componentWillUpdate&quot;);\n        },\n\n        componentDidUpdate:function(){\n            \/\/\u7ec4\u4ef6\u66f4\u65b0\u5b8c\u6bd5\uff0c\u6211\u4eec\u5e38\u5728\u8fd9\u91cc\u505a\u4e00\u4e9bDOM\u64cd\u4f5c\n            console.log(&quot;componentDidUpdate&quot;);\n        },\n\n        \/\/4\u3001\u9500\u6bc1\u9636\u6bb5\n        componentWillUnmount:function(){\n            \/\/\u9500\u6bc1\u65f6\u88ab\u8c03\u7528\uff0c\u901a\u5e38\u505a\u4e00\u4e9b\u53d6\u6d88\u4e8b\u4ef6\u7ed1\u5b9a\u3001\u79fb\u9664\u865a\u62dfDOM\u4e2d\u5bf9\u5e94\u7684\u7ec4\u4ef6\u6570\u636e\u7ed3\u6784\u3001\u9500\u6bc1\u4e00\u4e9b\u65e0\u6548\u7684\u5b9a\u65f6\u5668\u7b49\u5de5\u4f5c\n            console.log(&quot;componentWillUnmount&quot;);\n        }\n    });\n\n    ReactDOM.render(&lt;HelloMessage name=&quot;React \u8bed\u6cd5\u57fa\u7840&quot; \/&gt;, document.getElementById(&#039;example&#039;));\n\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JSX React\u662f\u7531ReactJS\u4e0eReact Native\u7ec4\u6210\uff0c\u5176\u4e2dReactJS\u662fFacebook\u5f00\u6e90\u7684 [&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-481","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\/481","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=481"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}