這可能是一個簡單的問題,但我是React-Native的新手,現在我完全陷入困境。如何從React-Native的另一個函數調用函數?
如何從函數1調用函數2?
這是我嘗試過,但按下第一個按鈕時,它呈現一個錯誤,指出:
功能未定義不是(評估「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);
直接調用function2()怎麼樣? – user10089632
@ user10089632因爲您有時可能想要在function1()中執行代碼,但有時也只能在function2()中執行代碼。它減少了冗餘。請參閱帖子中的編輯代碼。 – Misklahr