微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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个按钮效果如下

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐