这可能是一个简单的问题,但我对React-Native还很陌生,目前完全卡住了。
我如何从function1调用function2?
这是我尝试过的方法,但当按下第一个按钮时,它会呈现出一个错误,说:
未定义的函数(在“this.function2()”中评估)
import React, { Component } from 'react';
import {
AppRegistry,
View,
Image,
TouchableOpacity,
} from 'react-native';
export default class Example extends Component {
function1(){
console.log('function1() called');
...
this.function2();
};
function2() {
console.log('function2() called');
...
};
render() {
return (
<View>
<TouchableOpacity onPress={this.function1}>
<Image source={require('../../../assets/img/button.png')} />
</TouchableOpacity>
<TouchableOpacity onPress={this.function2}>
<Image source={require('../../../assets/img/button.png')} />
</TouchableOpacity>
</View>
);
};
}
AppRegistry.registerComponent('Example', () => Example);