我是本机反应的新手。我正在使用react-native-navigation(通过wix)进行导航。我为抽屉边栏创建了“ Drawerscreen”组件,在组件类下创建了一个名为“ navigateToScr”的函数。在此处,“ navigateToScr”函数会在初始化组件期间自动调用,而无需从任何地方调用,请任何人都可以解释一下为什么会发生这种情况?
import React, {Component} from 'react';
import {Text, View, StyleSheet, TouchableOpacity, ScrollView, SafeAreaView } from 'react-native'
export default class Drawerscreen extends Component {
constructor (props){
super(props);
}
navigateToScr = () =>{
console.log('in');
}
render() {
return (
<View style={styles.container}>
<ScrollView>
<SafeAreaView style={styles.container} forceInset={{ top: 'always', horizontal: 'never' }}>
<TouchableOpacity onPress={this.navigateToScr()} style={styles.menulinkcon}>
<Text style={styles.menulink}>Test 1</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.navigateToScr()} style={styles.menulinkcon}>
<Text style={styles.menulink}>Test 2</Text>
</TouchableOpacity>
</SafeAreaView>
</ScrollView>
</View>
)
}
}
const styles = StyleSheet.create({ ... });
``````````````````````````````````````````````````````````````````````
````````````````````````````````````````````````````````````````
Navigation.startSingleScreenApp({
screen: {
screen: "screen.MainScreen",
title: 'Main Screen',
},
drawer: {
left: {
screen: 'screen.Drawerscreen',
fixedWidth: 500
}
}
});
````````````````````````````````````````````````````````````````
答案 0 :(得分:0)
通过在this.navigateToScr之后放置打开和关闭括号,您实际上是在调用它并将onPress设置为其返回值。如果只想在按下该功能时运行该功能,请改用
forEach
或
for
后者通常在需要将值传递给方法时使用。