意料之外的令牌,意料之中;在 React Native 中调用函数时

Unexpected token, expected; When calling a function in react native

我试图在按下按钮时调用函数。 添加函数后,它出现意外令牌错误。

我按照之前所有类似问题的说明进行操作,但这并没有解决我的问题。请帮忙。

_handlePress: function() {
  console.log("Butto GO!")
}

export default class fun extends Component {
 render() {
  return (
    <View style={styles.container}>
       <TouchableHighlight onPress={() => this._handlePress()}>
            <Image style={{width: 50, height: 50}} source={require('./go.png')} />
        </TouchableHighlight>
    </View>
  );
 }
}

另外,调用的函数是否应该在默认值之前定义class?

将函数放在 render 后面,在 class 之后,我不知道你是否这样做 test : function() 它在那里工作所以试试我的例子并提供反馈

export default class fun extends Component {

  _handlePress() {
    console.log("Butto GO!")
  }

  render() {
    return (
     <View style={styles.container}>
         <TouchableHighlight onPress={() => this._handlePress()}>
            <Image style={{width: 50, height: 50}} source={require('./go.png')} />
         </TouchableHighlight>
      </View>
    );
   }
  }

或者如果您想在导出 class 后面做,您可以使用 _handlePress() 而不是 this._handlePress(),它应该可以工作!

示例:

'use strict';
import React, {Component} from 'react';
import {View,Text,TouchableOpacity,Alert,Dimensions} from 'react-native';

const windows = Dimensions.get('window');

export default class Feed extends Component {

  _test(){
    Alert.alert("Test");
    console.log("It worked!");
  }

  render() {
    return (
       <View style={{flex: 1}}>
          <TouchableOpacity onPress={() => this._test()}>
             <Image source={require('../image/2.jpg')} style={{height: windows.height, width: windows.width, }} />
          </TouchableOpacity> 
       </View>
    );
  }
}