React Native内部方法常用几种写法和调用规则

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011068702/article/details/82903678

1 简单部分代码

export default class App extends Component<Props> {
  render() {
    return (
        <View style={styles.container}>
	    <View style={styles.welcome}>
                <Button onPress={this.showMsg}title='prees me showMsg'/> 
		<Button onPress={() => {this.showMessage()}}title='prees me showMessage'/> 
            </View>
        </View>
    );
  }
    //记得这里调用的时候不需要加上()
    showMsg(){
	alert("showMsg(){}");  
    }
    
    //记得末尾加上分号,调用的时候也要加上()
    showMessage = () => {
        alert("showMessage = () => {}");
    };
}

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

2 分别点击上面代码的2个按钮效果如下

猜你喜欢

转载自blog.csdn.net/u011068702/article/details/82903678