React Native 生命周期

2019-01-15  本文已影响0人  努力生活的西鱼
ES6 React Native生命周期图
componentWillReceiveProps

组件接收到新的props,会被调用
props发生变化,使用componentWillReceiveProps来处理(比如将变动同步给state)

// props改变时调用
// 第一个参数:代表即将传入的新的props
componentWillReceiveProps(nextProps: Readonly<P>, nextContext: any): void {
    this.setState({
        isVisible: nextProps.show, // show为具体的props
    });
}

生命周期函数

render() {

}

自己定义的函数

renderResult = () => {

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

class App extends Component {
  state = {
    likes: 0
  };
  onPress = () => {
    const {likes} = this.state;
    this.setState({
      likes: likes + 1
    });
  };
  
  render() {
      return(
        <View>
          <TouchableOpacity onPress={this.onPress}>
            <Image 
              style = {styles.image}
              source = {{
                uri: 'http://img.hb.aicdn.com/f16a90f723f26bc08104ca5623a509a695a4c00c1352-zYJ85p_fw658'
              }}
              />
          </TouchableOpacity>
          <Text>{this.state.likes}</Text>
        </View>
      )
  }
  
}
上一篇 下一篇

猜你喜欢

热点阅读