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);
  • 相关阅读:
    bzoj3110
    idea 设置系列 各种乱码
    vim 系列
    idea 神键
    简单工厂,工厂方法,抽象工厂
    log4 按包进行日志输出
    maven依赖本地宝
    kafka 理论学习
    kafka windows环境搭建 测试
    linux 查找文件的命令
  • 原文地址:https://www.cnblogs.com/madaha/p/5920161.html
Copyright © 2011-2022 走看看