【问题标题】:React Native Change Color Every SecondReact Native 每秒改变颜色
【发布时间】:2018-12-15 12:58:12
【问题描述】:

我正在尝试学习 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',
  },
});

【问题讨论】:

    标签: javascript react-native mobile frontend


    【解决方案1】:

    看看这个小吃:https://snack.expo.io/rkFe9tpfQ

    StyleSheet.steelBlue
    

    变成

    styles.steelBlue
    

    【讨论】:

    • 如果解决了您的问题,请标记为解决方案。
    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多