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

    我们使用两种数据来控制一个组件:Props+State。props在父组件中指定,一旦指定,在该组件的生命周期中不再改变。而对于需要改变的数据,我们通过state进行修改

    constructor(构造器):初始化组件state值,返回值会赋给this.state属性。--- EC6写法

    定时器

    setTimeout (fn, 1000) 表示延迟1000毫秒后执行 fn 方法

    setInterval (fn,1000)  表示每隔1000毫秒执行 fn 方法。

     requestAnimationFrame(fn) 在每帧刷新之后执行一次

    setTimeout(fn, 0) 尽可能快的执行 

    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);
  • 相关阅读:
    ARC081F Flip and Rectangles
    LCA
    Tarjan
    2020牛客暑期多校六
    状压DP
    操作系统
    JAVA期末复习
    D. Yet Another Yet Another Task (区间最值)
    构造
    Codeforces Round #641 (Div. 2)
  • 原文地址:https://www.cnblogs.com/madaha/p/5920161.html
Copyright © 2011-2022 走看看