import React,{ Component } from 'react';
import { AppRegistry,Text,21)">View } from 'react-native';
class Blink extends Component {
constructor(props) {//哈哈,终于看到大家熟悉的构造函数了super(props);
this.state = { showtext: true };
// 每1000毫秒对showtext状态做一次取反操作
setInterval(() => {
this.setState({ showtext: !this.state.showtext });
},1000);
}
render() {
// 根据当前showtext的值决定是否显示text内容
let display = this.state.showtext ? this.props.text : ' ';//这就是大家熟悉的问号表达式,当状态showtext为真时, //设置为text。否者为空.而showtext被上面setInterval所控制
return ( <Text>{display}</Text> ); } } BlinkApp Component { render() { View> <Blink text='I love to blink' /> <'Yes blinking is so great' /> <'Why did they ever take this out of HTML' /> <'Look at me look at me look at me' /> </View> ); } } AppRegistry.registerComponent('BlinkApp',() => BlinkApp);
更多内容参看:http://reactnative.cn/docs/0.31/state.html