为什么自定义功能会在屏幕渲染期间在react-native-navigation抽屉组件中自动执行

时间:2019-04-13 21:00:17

标签: react-native react-native-navigation

我是本机反应的新手。我正在使用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
        }
    }
});
````````````````````````````````````````````````````````````````

1 个答案:

答案 0 :(得分:0)

通过在this.navigateToScr之后放置打开和关闭括号,您实际上是在调用它并将onPress设置为其返回值。如果只想在按下该功能时运行该功能,请改用

forEach

for

后者通常在需要将值传递给方法时使用。