{"id":367,"date":"2015-12-20T22:58:08","date_gmt":"2015-12-20T14:58:08","guid":{"rendered":"http:\/\/weitaoc.com\/?p=367"},"modified":"2019-09-27T14:19:06","modified_gmt":"2019-09-27T06:19:06","slug":"react-native%e6%a8%a1%e5%9d%97%e7%9b%b4%e6%8e%a5%e7%9a%84%e7%9b%91%e5%90%ac%ef%bc%88%e9%9d%9e%e4%b8%8e%e5%8e%9f%e7%94%9f%ef%bc%89","status":"publish","type":"post","link":"http:\/\/weitaoc.com\/?p=367","title":{"rendered":"react-native\u6a21\u5757\u76f4\u63a5\u7684\u76d1\u542c\uff08\u975e\u4e0e\u539f\u751f\uff09"},"content":{"rendered":"<p>\u548c\u539f\u751f\u4e4b\u95f4\u7684\u901a\u4fe1\u524d\u9762\u8bf4\u4e86\uff0c\u8fd9\u91cc\u53ea\u8bf4react-native\u6a21\u5757\u95f4\u7684\u901a\u4fe1\uff0c\u9002\u7528\u60c5\u51b5\u4e0e\u53c2\u6570\u4f20\u9012\u4e0d\u4e00\u6837\uff0c\u8fd9\u4e2a\u6a21\u5757\u95f4\u53ef\u80fd\u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u8054\uff0c\u6bd4\u5982\u540e\u53f0\u76d1\u542c\u8fdb\u7a0b\u76d1\u542c\u5230\u66f4\u65b0\u7b49\u3002<br \/>\nreact-native\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2aEventEmitter\uff0c\u5c01\u88c5\u6210RCTDeviceEventEmitter\uff0c\u901a\u8fc7RCTDeviceEventEmitter\u5efa\u7acb\u76d1\u542c\u5e76\u56de\u8c03\u5904\u7406\u65b9\u6cd5\u3002<br \/>\n\u6838\u5fc3\u7684\u4e1c\u897f\u7275\u626f\u592a\u591a\uff0c\u8fd9\u91cc\u76f4\u63a5\u4e0ademo\u3002<\/p>\n<div class=\"codecolorer-container text geshi\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/>83<br \/>84<br \/>85<br \/>86<br \/>87<br \/>88<br \/>89<br \/>90<br \/>91<br \/>92<br \/>93<br \/>94<br \/>95<br \/>96<br \/>97<br \/><\/div><\/td><td><div class=\"text codecolorer\" style=\"white-space:nowrap\">\/**<br \/>\n&nbsp;* Sample React Native App<br \/>\n&nbsp;* https:\/\/github.com\/facebook\/react-native<br \/>\n&nbsp;*\/<br \/>\n'use strict';<br \/>\n<br \/>\nvar React = require('react-native');<br \/>\n<br \/>\nvar RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');<br \/>\n<br \/>\nvar {<br \/>\n&nbsp; StyleSheet,<br \/>\n&nbsp; Text,<br \/>\n&nbsp; View,<br \/>\n&nbsp; TextInput<br \/>\n} = React;<br \/>\n<br \/>\n<br \/>\nvar styles = StyleSheet.create({<br \/>\n&nbsp; container: {<br \/>\n&nbsp; &nbsp; flex: 1,<br \/>\n&nbsp; &nbsp; justifyContent: 'center',<br \/>\n&nbsp; &nbsp; alignItems: 'center',<br \/>\n&nbsp; &nbsp; backgroundColor: '#F5FCFF',<br \/>\n&nbsp; },<br \/>\n&nbsp; welcome: {<br \/>\n&nbsp; &nbsp; fontSize: 20,<br \/>\n&nbsp; &nbsp; textAlign: 'center',<br \/>\n&nbsp; &nbsp; margin: 10,<br \/>\n&nbsp; },<br \/>\n&nbsp; instructions: {<br \/>\n&nbsp; &nbsp; textAlign: 'center',<br \/>\n&nbsp; &nbsp; color: '#333333',<br \/>\n&nbsp; &nbsp; marginBottom: 5,<br \/>\n&nbsp; },<br \/>\n});<br \/>\n<br \/>\n<br \/>\nvar Input = React.createClass({<br \/>\n<br \/>\n&nbsp; &nbsp; handleUpdateChange(text) {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; RCTDeviceEventEmitter.emit('change',text);<br \/>\n&nbsp; &nbsp; },<br \/>\n<br \/>\n<br \/>\n&nbsp; render() {<br \/>\n<br \/>\n&nbsp; &nbsp; return (<br \/>\n&nbsp; &nbsp; &nbsp; &lt;View style={{justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF'}}&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;TextInput onChangeText={(text) =&gt; this.handleUpdateChange(text)} style={{ width : 200, height: 40, borderColor: 'gray', borderWidth: 1}} \/&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &lt;\/View&gt;<br \/>\n&nbsp; &nbsp; );<br \/>\n&nbsp; }<br \/>\n});<br \/>\n<br \/>\n<br \/>\n<br \/>\n<br \/>\nvar ShowText = React.createClass({<br \/>\n<br \/>\n&nbsp; &nbsp; getInitialState(){<br \/>\n&nbsp; &nbsp; &nbsp; return {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; text : ''<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; },<br \/>\n<br \/>\n&nbsp; &nbsp; componentDidMount(){<br \/>\n&nbsp; &nbsp; &nbsp; var me = this;<br \/>\n&nbsp; &nbsp; &nbsp; RCTDeviceEventEmitter.addListener('change',function(text){<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;me.setState({<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text : text<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})<br \/>\n&nbsp; &nbsp; &nbsp; })<br \/>\n&nbsp; &nbsp; },<br \/>\n<br \/>\n<br \/>\n&nbsp; &nbsp; render() {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; return (<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;View style={{justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF'}}&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Text&gt;{this.state.text}&lt;\/Text&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/View&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; );<br \/>\n&nbsp; &nbsp; }<br \/>\n});<br \/>\n<br \/>\nmodule.exports = React.createClass({<br \/>\n<br \/>\n<br \/>\n&nbsp; render() {<br \/>\n&nbsp; &nbsp; return (<br \/>\n&nbsp; &nbsp; &nbsp; &lt;View style={styles.container}&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;Input \/&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;ShowText \/&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &lt;\/View&gt;<br \/>\n&nbsp; &nbsp; );<br \/>\n&nbsp; }<br \/>\n});&lt;\/pre&gt;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><a href=\"http:\/\/weitaoc.com\/archives\/303\/fb273ef29cf057a236290c437f325ef1\" rel=\"attachment wp-att-304\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-304\" src=\"http:\/\/weitaoc.com\/wp-content\/uploads\/2015\/11\/fb273ef29cf057a236290c437f325ef1.png\" alt=\"fb273ef29cf057a236290c437f325ef1\" width=\"250\" height=\"250\" \/><\/a><\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"http:\/\/weitaoc.com\">\u8ff7\u8def\u7684\u8001\u9f20<\/a> &raquo; <a href=\"http:\/\/weitaoc.com\/?p=367\">react-native\u6a21\u5757\u76f4\u63a5\u7684\u76d1\u542c\uff08\u975e\u4e0e\u539f\u751f\uff09<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u548c\u539f\u751f\u4e4b\u95f4\u7684\u901a\u4fe1\u524d\u9762\u8bf4\u4e86\uff0c\u8fd9\u91cc\u53ea\u8bf4react-native\u6a21\u5757\u95f4\u7684\u901a\u4fe1\uff0c\u9002\u7528\u60c5\u51b5\u4e0e\u53c2\u6570\u4f20\u9012\u4e0d\u4e00\u6837\uff0c\u8fd9\u4e2a\u6a21\u5757\u95f4\u53ef\u80fd\u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u8054\uff0c\u6bd4\u5982\u540e\u53f0\u76d1\u542c\u8fdb\u7a0b\u76d1\u542c\u5230\u66f4\u65b0\u7b49\u3002 react-native\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2aEventEmitter\uff0c\u5c01\u88c5\u6210RCTDeviceEv [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[49],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-other","tag-react"],"views":9827,"_links":{"self":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367"}],"version-history":[{"count":2,"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":369,"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions\/369"}],"wp:attachment":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}