React Native学习总结第三天

2018-01-19  本文已影响31人  Zz7777777

1.0 组件的生命周期

9FC9FFAF-4FA5-40AA-A02E-8D69C450E9FB.png 2018-01-19 10_00_49.gif

2.0 组件间通信(重点,难点)

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

//编写定时器组件
class CountDown extends Component {
  state ={
    count : 30,
  }
   render(){
     const {count}=this.state;
     return (
        <Text>{count}</Text>
     )
   }
  add = (time) =>{
    this.setState({
      count:this.state.count+time,
    })
  }

  componentDidMount(){
    this.timer=setInterval(()=>{
      const { count } =this.state;
      if (count == 0){
        return clearInterval(this.timer);
      }
      this.setState({
        count :count - 1,
      })
    },1000)
  }
  componentWillUnmount(){
    clearInterval(this.timer);
  }
}
class App extends Component {
  //es6的函数写法 addTime是方法名 ()参数名   =>{}执行的方法体
  addTime = () =>{
    this.a.add(10);

  }
  render() {
    return (
      <View style={styles.container}>
        <TouchableOpacity onPress ={this.addTime}>
          延迟10秒
        </TouchableOpacity>
        <Text style={styles.welcome}>
          {/*
            <CountDown  ref={q => this.countDown = q}
            如何理解这句代码:CountDown是当前的组件类 q表示CountDown组件类的实例 q是实例的名字a 可以理解oc的 Person *p =[Person new];
            => 表示作用于的意思 我们都知道要想界面能够进行刷新 只能对当前的属性进行setState操作 所以刷新界面 只能对当前的属性进行操作,属性的变化
            变化就会引起界面的变化 所以用this.a 对q的植进行接收
          */}
          <CountDown  ref={q =>this.a = q}/>
        </Text>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
})

AppRegistry.registerComponent('App', () => App)

2018-01-19 13_20_00.gif
    import React, { Component } from 'react'
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
} from 'react-native'

//编写定时器组件
class CountDown extends Component {
  state ={
    count : 5,
  }
   render(){
     const {count}=this.state;
     return (
        <Text>{count}</Text>
     )
   }
timeup =() => {
  alert('时间到!');
  this.props.timeupParent && this.props.timeupParent(123);
}

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

  timeupParent = (params) => {
    alert('我知道了'+params);
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
            {/*this.timeupParent与this.timeupParent()的区别
            this.timeupParent()表示函数的直接调用
            this.timeupParent 表示传的参数
            */}
          <CountDown  timeupParent={this.timeupParent} />
        </Text>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
})

AppRegistry.registerComponent('App', () => App)

2018-01-19 13_52_54.gif
    import React, { Component } from 'react'
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
} from 'react-native'

//编写定时器组件
class CountDown extends Component {
  state ={
    count : 2,
  }
   render(){
     const {count}=this.state;
     return (
        <Text>{count}</Text>
     )
   }
timeup =() => {
  //alert('时间到!');
  //子控件调用父控件的函数或者属性 都是需要带入this.props.属性名或者函数名
  this.props.a && this.props.a('子组件的参数' +12312312);
}

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

  timeupParent = (param1,param2) => {
   alert('我知道了' +param1);
   alert('我知道了' +param2);
  }

  state = {
    arr:['张三'],
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          {
            this.state.arr.map(i =>{
              {/*如何理解:<CountDown key={i} timeupParent={(child) => this.timeupParent('父组件的参数' +i,child)}/>
                this.timeupParent('父组件的参数' +i,child)表示调用函数
              */}
              return <CountDown key={i} a={(child) => this.timeupParent('父组件的参数' +i,child)}/>
            })
          }
        </Text>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
})

AppRegistry.registerComponent('App', () => App)
上一篇下一篇

猜你喜欢

热点阅读