简体   繁体   English

undefined 不是 onPress 的 object(评估 this.props.navigation.navigate)

[英]undefined is not an object (evaluating this.props.navigation.navigate) for onPress

I'm new to react native and getting this error:我是本机反应的新手并收到此错误:

undefined is not an object (evaluating this.props.navigation.navigate) undefined 不是 object(评估 this.props.navigation.navigate)

This is a very common error, but the cause can be different as I'm unable to fix it by seeing other answers to similar questions.这是一个非常常见的错误,但原因可能不同,因为我无法通过查看类似问题的其他答案来修复它。

I have a screen that has this code:我有一个包含以下代码的屏幕:

function MyClientsScreen(props) {

    return (
        <Stack.Navigator>
            <Stack.Screen name='Clients' component={ClientSrc} options={{
                title: 'My Clients',
                headerRight: () => (
                    <TouchableOpacity onPress={() => props.navigation.navigate('AddClients')}>
                        <Image source={require('../assets/addClient.png')} style={styles.addBtnImg} />
                    </TouchableOpacity>
                ),
            }} />
            <Stack.Screen name='AddClients' component={AddClientSrc} options={{
                title: 'Add New Client',
                headerRight: () => (
                    <TouchableOpacity>
                        <Text style={styles.headerButtton}>Save</Text>
                    </TouchableOpacity>
                )
            }} />
        </Stack.Navigator>
    );
}

The headerRight button on the current screen should navigate to AddClients (which is inside the stack navigation) when pessed.当前屏幕上的headerRight按钮应导航到AddClients (位于堆栈导航内)。

navigation is not defined anywhere and I was able to do it when I had the button code inside a function that I made const ClientSrc = ({ navigation }) => {... but when I moved the button as a headerRight button, the onPress started throwing the error. navigation没有在任何地方定义,当我在 function 中有按钮代码时,我能够做到这一点,我做了const ClientSrc = ({ navigation }) => {...但是当我将按钮作为headerRight按钮移动时, onPress开始抛出错误。

In order to get a navigation prop that component should be part of navigation.为了获得导航道具,该组件应该是导航的一部分。

You can handle this differently like below你可以像下面这样不同地处理这个

<Stack.Screen name='Clients' component={ClientSrc} options={({ navigation }) => ({
                title: 'My Clients',
                headerRight: () => (
                    <TouchableOpacity onPress={() => navigation.navigate('AddClients')}>
                        <Image source={require('../assets/addClient.png')} style={styles.addBtnImg} />
                    </TouchableOpacity>
                ),
            })} />

The options passes navigation to the function that is given you can use that and get access to navigation.这些选项将导航传递给 function,您可以使用它并访问导航。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义不是对象(评估this.props.navigation.navigate) - undefined is not an object (evaluating this.props.navigation.navigate) 未定义不是对象(评估&#39;_this.props.navigation.navigate&#39;) - undefined is not an object (evaluating '_this.props.navigation.navigate') 类型错误:未定义不是对象(评估 this.props.navigation.navigate) - Type Error: Undefined is not an object (evaluating this.props.navigation.navigate) 类型错误:未定义不是对象(评估“_this.props.navigation.navigate”) - TypeError: undefined is not an object (evaluating '_this.props.navigation.navigate') 使用开关导航器:对象(评估&#39;_this.props.navigation.navigate&#39;)未定义 - using switch navigator: object (evaluating '_this.props.navigation.navigate') undefined 未定义不是组件上的对象(评估“ this.props.navigation.navigate”) - undefined is not an object (evaluating 'this.props.navigation.navigate') on a component 创建导航器时,未定义不是 object(评估 this.props.navigation.navigate) - Undefined is not an object (evaluating this.props.navigation.navigate) when creating navigator 错误:未定义不是 object(评估 this.props.navigation.navigate) - Error: undefined is not an object (evaluating this.props.navigation.navigate) TypeError: undefined is not an object (evalating '_this.props.navigation.navigate') reactNavigation 5 - TypeError: undefined is not an object (evaluating '_this.props.navigation.navigate') reactNavigation 5 【React-native】undefined不是对象(评估&#39;_this.props.navigation.navigate&#39;) - 【React-native】undefined is not an object (evaluating '_this.props.navigation.navigate')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM