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);
  • 相关阅读:
    centos 7 配置ip
    Linux下安装jmeter
    eclipse 高效快捷键大全
    eclipse中不能找到dubbo.xsd报错”cvc-complex-type.2.4.c“的 两种解决方法
    大型网站系统架构演化之路(转)
    程序员技术练级攻略
    JSP和servlet之间的传值(总结的很全面)
    正则表达式笔记
    cenos 安装nginx并添加到service
    mac os重装php
  • 原文地址:https://www.cnblogs.com/madaha/p/5920161.html
Copyright © 2011-2022 走看看