zoukankan      html  css  js  c++  java
  • State(状态)

    propsstateprops是在父组件中指定,而且一经指定,在被指定的组件的生命周期中则不再改变。 对于需要改变的数据,我们需要使用state。般来说,你需要在constructor中初始化state(译注:这是ES6的写法,早期的很多ES5的例子使用的是getInitialState方法来初始化state,这一做法会逐渐被淘汰),然后在需要修改时调用setState方法。

    import React, { Component } from 'react';
    import { AppRegistry, Text, 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 : ' ';
        return (
          <Text>{display}</Text>
        );
      }
    }
    
    class BlinkApp extends Component {
      render() {
        return (
          <View>
            <Blink text='I love to blink' />
            <Blink text='Yes blinking is so great' />
            <Blink text='Why did they ever take this out of HTML' />
            <Blink text='Look at me look at me look at me' />
          </View>
        );
      }
    }
    AppRegistry.registerComponent('BlinkApp', () => BlinkApp);
  • 相关阅读:
    02.规划过程组表格-干系管理计划
    02.规划过程组表格-供方选择标准
    02.规划过程组表格-采购管理计划
    12动态规划运用实例
    11算法策略之动态规划
    10算法策略之贪婪法
    9算法策略之分治法
    8算法策略之枚举法
    7算法策略之递推法
    6数学模型和数学建模
  • 原文地址:https://www.cnblogs.com/dragonh/p/6210605.html
Copyright © 2011-2022 走看看