react native 学习笔记之state

前端之家收集整理的这篇文章主要介绍了react native 学习笔记之state前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
state状态还是可以理解为Android,ios中一个类的成员变量,而props和state的区别是,props一经指定,就不能 修改,而state是可以修改的。一般来讲,你需要在constructor中初始化state,然后在需要修改调用setState方法

下面是一个闪烁文字的例子。

import React,{ Component } from 'react';
import { AppRegistry,Text,21)">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 : ' ';//这就是大家熟悉的问号表达式,当状态showText为真时, //设置为text。否者为空.而showText被上面setInterval所控制
 return ( <Text>{display}</Text> ); } } BlinkApp Component { render() { View> <Blink text='I love to blink' />  <'Yes blinking is so great' /> <'Why did they ever take this out of HTML' /> <'Look at me look at me look at me' /> </View> ); } } AppRegistry.registerComponent('BlinkApp',() => BlinkApp);

更多内容参看:http://reactnative.cn/docs/0.31/state.html

原文链接:https://www.f2er.com/react/306093.html

猜你在找的React相关文章