美文网首页
RN组件生命周期

RN组件生命周期

作者: 952625a28d0d | 来源:发表于2018-04-10 11:09 被阅读45次
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  Image,
  TouchableOpacity,
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {

  state = {
    count:30
  };

  render() {
    const count = this.state.count;
    return (
      <Text>
        {count}
      </Text>
    );
  }

  componentDidMount(){
    this.timer = setInterval(()=>{
      const count = this.state.count;
      if (count === 0) {
        return clearInterval(this.timer);
      };
      this.setState({
        count:count - 1
      });
    },1000);
  }

  componentWillUnmount(){
    clearInterval(this.timer);
  }

}
  • 插播:判断语句的三种写法
renderResult = ()=>{
    const count = this.state.count;

    // 常规写法
    if (count > 0) {
      return <Text>{count}</Text>
    }else{
      return <Text>时间到!</Text>
    }

    // 三目运算表达式写法
    return count > 0 ? <Text>{count}</Text> : <Text>时间到!</Text>

    // 运算符&&写法 不满足条件直接消失

    return count > 0 &&  <Text>{count}</Text>
  };
  • 总结
image.png

什么时候用Props?什么时候用state?

  • 初始值用 props 作为state的初始值 如果主控件修改了props的初始值,那么子控件则通过用来捕捉变化的props的专用方法修改
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  Image,
  TouchableOpacity,
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};

class CountDown extends Component<Props> {


  state = {
    count:this.props.time
  };

  // 因为主组件修改了props的time1的值是延迟执行的,但是这里并不知道修改了。所以我们需要把props的变动同步到state上
  componentWillReceiveProps(nextProps){
    this.setState({
      count:nextProps.time
    })
  }

  renderResult = ()=>{
    const count = this.state.count;

    // 常规写法
    if (count > 0) {
      return <Text>{count}</Text>
    }else{
      return <Text>时间到!</Text>
    }

    // 三目运算表达式写法
    // return count > 0 ? <Text>{count}</Text> : <Text>时间到!</Text>

    // 运算符&&写法 不满足条件直接消失

    // return count > 0 &&  <Text>{count}</Text>
  };

  render() {
    const count = this.state.count;
    return (
      <View>
      {this.renderResult()}
      </View>
    );
  }

  componentDidMount(){
    this.timer = setInterval(()=>{
      const count = this.state.count;
      if (count === 0) {
        return clearInterval(this.timer);
      };
      this.setState({
        count:count - 1
      });
    },1000);
  }

  componentWillUnmount(){
    clearInterval(this.timer);
  }
}

// 主组件
export default class App extends Component<Props> {

  // 设置主组件的初始值
  state = {
    time1:10,
    time2:20,
  }

  // 2秒钟之后修改time1初始值为30 视图将要展示的时候处理
  componentWillMount(){

    // 使用一次性的定时函数设置2秒的延迟执行 用来看清楚整个过程
    setTimeout(()=>{
      this.setState({
      time1:30,
      })
    },2000)
  }

  render() {
    return (
      <View>
        <CountDown time = {this.state.time1}/>
        <CountDown time = {this.state.time2}/>
      </View>
    );
  }

}
RNLife.gif

相关文章

  • 面试-ReactNative相关

    RN组件的生命周期 RN如何优化 Redux 和 MobX 选择 RN与原生通信 RN原理 ES6相关知识

  • React Native学习笔记之组件生命周期

    React Native学习笔记之组件生命周期 单一组件生命周期 首先看一下RN组件生命周期简略说明图 组件初始化...

  • 2018-06-11 RN组件的生命周期

    主题:组件的生命周期 RN组件的props和state 1.属性(props) 它是组件的不可变属性(组件自己不可...

  • ReactNative-组件的生命周期

    一、RN组件的生命周期 概要 组件的生命周期描述了其从生成到消亡所经历的一系列状态。深入理解组件的生命周期及相关回...

  • RN的生命周期

    RN的生命周期 getDefaultProps在组件创建之前,会先调用 getDefaultProps(),这是全...

  • React Native(RN)-组件生命周期

    生命周期简介 像 Android 开发一样,React Native(RN) 中的组件也有生命周期(Lifecyc...

  • React Native组件(二)View组件解析

    相关文章React Native探索系列React Native组件系列 前言 了解了RN的组件的生命周期后,我们...

  • react native面试题

    1、rn相比于原生,有哪些优势 2、rn组件的生命周期 3、rn的缺点有哪些 4、父传子,子传父实现原理 5、如何...

  • React Component(生命周期)

    RN 组件的生命周期如下图: 一、生命周期划分 第一阶段:组件第一次绘制,完成组件的加载和初始化。 第二阶段:组件...

  • ReactNative运行原理分析

    ReactNative运行原理分析 RN的生命周期 可以把组件生命周期大致分为三个阶段: 第一阶段:是组件第一次绘...

网友评论

      本文标题:RN组件生命周期

      本文链接:https://www.haomeiwen.com/subject/cklihftx.html