[英]undefined is not an object (evaluating 'this.props.navigation')
I'm trying to create a React Native app with some basic routing. 我正在尝试使用一些基本的路由创建一个React Native应用程序。
This is my code so far: 到目前为止,这是我的代码:
App.js: App.js:
import React from 'react'
import { StackNavigator } from 'react-navigation'
import MainScreen from './classes/MainScreen'
const AppNavigator = StackNavigator(
{
Index: {
screen: MainScreen,
},
},
{
initialRouteName: 'Index',
headerMode: 'none'
}
);
export default () => <AppNavigator />
MainScreen.js: MainScreen.js:
import React, { Component } from 'react'
import { StyleSheet, Text, View, Button, TouchableOpacity, Image } from 'react-native'
import HomeButton from './HomeButton'
export default class MainScreen extends Component {
static navigatorOptions = {
title: 'MyApp'
}
constructor(props) {
super(props)
}
render() {
return (
<View style={styles.container}>
<Image source={require('../img/logo.png')} style={{marginBottom: 20}} />
<HomeButton text='Try me out' classType='first' />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}
})
HomeButton.js: HomeButton.js:
import React, { Component } from 'react'
import { StyleSheet, Text, View, Button, TouchableOpacity } from 'react-native'
import { StackNavigator } from 'react-navigation'
export default class HomeButton extends Component {
constructor(props) {
super(props)
}
render() {
return (
<TouchableOpacity
onPress={() => navigate('Home')}
style={[baseStyle.buttons, styles[this.props.classType].style]}
>
<Text style={baseStyle.buttonsText}>{this.props.text.toUpperCase()}</Text>
</TouchableOpacity>
)
}
}
var Dimensions = require('Dimensions')
var windowWidth = Dimensions.get('window').width;
const baseStyle = StyleSheet.create({
buttons: {
backgroundColor: '#ccc',
borderRadius: 2,
width: windowWidth * 0.8,
height: 50,
shadowOffset: {width: 0, height: 2 },
shadowOpacity: 0.26,
shadowRadius: 5,
shadowColor: '#000000',
marginTop: 5,
marginBottom: 5
},
buttonsText: {
fontSize: 20,
lineHeight: 50,
textAlign: 'center',
color: '#fff'
}
})
const styles = {
first: StyleSheet.create({
style: { backgroundColor: '#4caf50' }
})
}
Everything works fine, but when pressing the button I get 一切正常,但是当我按下按钮时
Can't find variable: navigate
找不到变量:导航
I've read that I have to declare it like this: 我读过我必须这样声明:
const { navigate } = this.props.navigation
So I edited HomeButton.js
and added that line at the beginning of the render
function: 因此,我编辑了
HomeButton.js
并在render
函数的开头添加了这一行:
render() {
const { navigate } = this.props.navigation
return (
<TouchableOpacity
onPress={() => navigate('Home')}
style={[baseStyle.buttons, styles[this.props.classType].style]}
>
<Text style={baseStyle.buttonsText}>{this.props.text.toUpperCase()}</Text>
</TouchableOpacity>
)
}
Now I get: 现在我得到:
TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate')
TypeError:undefined不是对象(评估'this.props.navigation.navigate')
It seems that the navigation
object is not coming into the properties, but I don't understand where should I get it from. 似乎
navigation
对象没有进入属性,但是我不知道应该从哪里获取它。
What am I missing here? 我在这里想念什么?
React-navigation
pass navigation prop to the screen components defined in the stack navigator. React-navigation
将导航道具传递给在堆栈导航器中定义的屏幕组件。
So in your case, MainScreen
can access this.props.navigation
but HomeButton
can't. 因此,在您的情况下,
MainScreen
可以访问this.props.navigation
但HomeButton
不能。
It should work if you pass navigation prop from MainScreen
to HomeButton
: 如果您将导航道具从
MainScreen
传递到HomeButton
它应该可以工作:
<HomeButton text='Try me out' classType='first' navigation={this.props.navigation}/>
Edit: You have to define the Home
screen in your stack navigator in order to navigate to it, your onPress={() => navigate('Home')}
won't work until then. 编辑:您必须在堆栈导航器中定义
Home
屏幕才能导航到它,您的onPress={() => navigate('Home')}
直到那时才起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.