简体   繁体   中英

React Native Change Color Every Second

I am trying to learn React Native and want to make text that changes color every second. I have this code, but my ios emulator just shows a blank white screen with no text at all. Can someone take a look at the code below and tell me what I did wrong?

Thank You!

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',
  },
});

Take a look at this snack: https://snack.expo.io/rkFe9tpfQ

StyleSheet.steelBlue

becomes

styles.steelBlue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM