React Native之內(nèi)部方法常用幾種寫法和調(diào)用規(guī)則

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>
        );
      }
        //記得這里調(diào)用的時候不需要加上()
        showMsg(){
        alert("showMsg(){}");  
        }
        
        //記得末尾加上分號,調(diào)用的時候也要加上()
        showMessage = () => {
            alert("showMessage = () => {}");
        };
    }
     
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },
      welcome: {
        fontSize: 10,
        textAlign: 'center',
        margin: 30,
      },
    });

 

 
2 分別點(diǎn)擊上面代碼的2個按鈕效果如下

 
 

 











作者:chen.yu
深信服三年半工作經(jīng)驗,目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進(jìn)階人工智能(鏈接)