react timer

最简单的timer demo

import React, {Component} from 'react';
import {View} from 'react-native';

export default class DemoTimer extends Component {
    componentDidMount() {
        //设置500 ms后执行,打印log
        this.timer = setTimeout(() => {
            console.log('a simple timer');
        }, 500);
    }
    componentWillUnmount() {
        //在unmount回调清除定时器
        this.timer && clearTimeout(this.timer);
    }

    render() {
        return (
            
                {this.props.children}
            
        );
    }
};

index.android.js

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

import DemoTimer from './src/timer';

class DemoMain extends Component {

  render() {
    return (
      
        
          Demo Timer
        
      

    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

// 注册应用(registerComponent)后才能正确渲染 注意:只把应用作为一个整体注册一次,而不是每个组件/模块都注册
AppRegistry.registerComponent('DemoProject', () => DemoMain);

你可能感兴趣的:(react timer)