通过state来更改数据
1.样式文件style.js
代码如下:
import {
StyleSheet,
} from 'react-native';
export let styles = StyleSheet.create({
blinkText: {
fontSize: 20
},
});
截图如下:
2.业务文件App.js
代码如下:
import React, {
Component
} from 'react';
import {
View,
Text,
AppRegistry
} from 'react-native';
// 加载样式文件
import {
styles
} from './static/style/style';
class Blink extends Component {
constructor(props) {
super(props);
this.state = {
isShowingText: true
};
setInterval(() => {
this.setState(previousState => {
return {
isShowingText: !previousState.isShowingText
};
});
}, 1000);
}
render() {
if (!this.state.isShowingText) {
return null;
};
return (
<Text style={styles.blinkText}>{this.props.text}</Text>
);
}
}
export default class BlinkApp extends Component {
render() {
return (
<View>
<Blink text='first blink' />
<Blink text='second blink' />
</View>
);
}
}
AppRegistry.registerComponent('BlinkApp', () => BlinkApp);
截图如下:
3.运行代码截图:
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。