我正在尝试学习React Native,并希望使文本每秒变一次颜色。我有此代码,但是我的ios模拟器仅显示空白屏幕,根本没有文本。有人可以看看下面的代码,然后告诉我我做错了吗?
谢谢!
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<ChangeColor text= 'This text should be changing color.'/>
<ChangeColor text= 'Hopefully it works.'/>
</View>
);
}
}
class ChangeColor extends React.Component {
constructor(props) {
super(props);
this.state = {color: StyleSheet.skyBlue};
// Toggle the state every second
setInterval(() => {
this.setState(
{ color: StyleSheet.steelBlue}
);
}, 1000);
}
render() {
let display = this.state.color ? this.props.text : ' ';
return (
<Text>{display}</Text>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
skyBlue: {
color: 'skyblue',
},
steelBlue: {
color: 'steelblue',
},
});