首页 文章

不能在React本机默认项目中将类作为函数调用

提问于
浏览
0

我刚安装了反应本机默认项目并在默认类中添加了一个函数,但它没有被执行

import React, {Component} from 'react';
    import {Platform, StyleSheet, Text, View, Alert,TouchableOpacity} from 'react-native';

    const instructions = Platform.select({
      ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
      android:
        'Double tap R on your keyboard to reload,\n' +
        'Shake or press menu button for dev menu',
    });

    type Props = {};
    export default class App extends Component {
    //the function i added      
    clickMe() {
        Alert("started");
      }
      render() {
        return (
          <View>
            <Text >Welcome to  Native!</Text>
            <Text >To get started, edit App.js</Text>
            <TouchableOpacity onPress={() => this.clickMe()}>
            <Text>{instructions}</Text>
            </TouchableOpacity>
          </View>
        );
      }
    }

1 回答

  • 0

    React-Native中Alert的正确方法是:

    Alert.alert(
      'Alert Title',
      'My Alert Msg',
      [        
        {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
        {text: 'OK', onPress: () => console.log('OK Pressed')},
      ],
      { cancelable: false }
    )
    

    谢谢..!

    ~ Praz

相关问题