react-native 的生命周期,以及通信关系,转载
2017-03-20 11:05
393 查看
import React, {Component} from 'react'; import { AppRegistry, StyleSheet, Text, View } from 'react-native'; var Son = React.createClass({ getDefaultProps(){ console.log('getDefaultProps') }, getInitialState(){ return { times: 0 } }, timesPlus(){ let times = this.state.times times++ this.setState({ times: times }) }, timesReset(){ this.props.timesReset() }, render() { return ( <View style={styles.container}> <Text style={styles.welcome} onPress={this.timesPlus}> 儿子说有本事揍我 </Text> <Text style={styles.instructions}> 你居然揍了{this.state.times}次 </Text> <Text style={styles.instructions} onPress={this.timesReset}> 反揍{this.state.times}次 </Text> </View> ); } }) var proMyapp = React.createClass({ getDefaultProps(){ console.log('father', 'getDefaultProps') }, getInitialState(){ return { hit: false, times: 0 } }, timesReset(){ this.setState({ times: 0 }) }, willHit(){ this.setState({ hit: !this.state.hit }) }, timesPlus(){ let times = this.state.times times += 3 this.setState({ times: times }) }, render() { return ( <View style={styles.container}> { this.state.hit ? <Son times={this.state.times} timesReset={this.timesReset}/> : null } <Text style={styles.welcome} onPress={this.timesReset}> 放你一马 </Text> <Text style={styles.instructions} onPress={this.willHit}> 揍不揍 </Text> <Text style={styles.instructions}> 就{this.state.times}次 </Text> <Text style={styles.instructions} onPress={this.timesPlus}> 就3次 </Text> </View> ); } }) var styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, }); AppRegistry.registerComponent('proMyapp', () => proMyapp);
不知道为什么这里换乘es5 的写法会出现爆红,可能是因为rn版本的问题不一样
相关文章推荐
- [ReactNative] 03--ReactNative的生命周期 & render的Diff算法 & 组件之间的通信
- react native组件通信以及
- react native利用webvView嵌入h5页面以及RN与webView的通信
- React Native 中 component 生命周期
- 【React Native开发】React Native控件之ViewPagerAndroid讲解以及美团首页顶部效果实例(17)
- React Native生命周期
- (React-Native 学习之三) Yarn 和 npm 相关命令 以及区别
- Android React Native组件的生命周期及回调函数
- React Native 中组件的生命周期
- react-native生命周期
- React Native 中组件的生命周期
- react native Component 生命周期
- 详解react-native-fs插件的使用以及遇到的坑
- react native FlatList使用详解以及上拉刷新下拉加载带可运行demo
- React Native生命周期
- Unity系统自带函数生命周期以及相互关系
- UML类图符号 各种关系说明以及举例(转载)
- React Native 中组件的生命周期
- React-Native之通信机制
- react_native 项目实战 (4) 自定义分类 使用 CheckBox 以及 数据存储asyncStorage