[英]undefined is not an object (evaluating '_this.props.navigator.push') react native , when i am trying to navigate from one page to another
Here is the sample code , i am trying react-native-navigation 这是示例代码,我正在尝试react-native-navigation
i used react-native init navigate to start the project and afterwards i installed
我使用react-native init导航启动项目,然后安装
yarn add react-native-navigation@latest
纱线添加react-native-navigation @ latest
the code run perfect for first screen , but as i am making a call to showNextScreen function it throws the error 该代码对于第一个屏幕运行完美,但是由于我正在调用showNextScreen函数,因此会引发错误
undefined is not an object (evaluating 'this.props.navigator.push') react native 未定义不是对象(评估“ this.props.navigator.push”)是本机反应
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import {Navigation} from 'react-native-navigation';
import firstPage from './app/components/firstPage';
export default class navigate extends Component {
//function to move to next screen
showNextScreen = () =>{
this.props.navigator.push({
screen: 'firstPage',
title: 'firstScreen'
})
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<Text onPress={this.showNextScreen.bind(this)}
style={styles.instructions}>
Double tap R on your keyboard to reload,{'\n'}
Shake or press menu button for dev menu
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('navigate', () => navigate);
Using React Native Navigation, you have to bootstrap your app differently than how it's done originally. 使用React Native Navigation,您必须以与原始方式不同的方式引导应用程序。 Native Navigation doesn't use the registerComponent setup since it's a native navigator.
本机导航不使用registerComponent设置,因为它是本机导航器。
The steps to fully setting up are listed in their docs https://wix.github.io/react-native-navigation/#/usage 完全设置步骤在其文档https://wix.github.io/react-native-navigation/#/usage中列出
As you said that your code runs perfect for the first screen, I'm pretty confident you've followed the configuration instructions given at the vendor's website, as your application wouldn't even run if you didn't. 正如您说的那样,您的代码可以完美地在第一个屏幕上运行,我非常有信心您遵循了供应商网站上给出的配置说明,因为如果不这样做,您的应用程序甚至将无法运行。 Now you've configured a startSingleScreenApp or a startTabBasedApp (doesn't really matter which) and you've set your screen (or tabs).
现在,您已经配置了startSingleScreenApp或startTabBasedApp(实际上无关紧要),并且已经设置了屏幕(或标签)。
navigator.push is passed as props to those screens you defined and you can execute it from there. navigator.push作为props传递给您定义的屏幕,您可以从那里执行它。 However, this.props.navigator.push isn't available applicationwide but only to the defined screens.
但是,this.props.navigator.push在应用程序范围内不可用,而仅在定义的屏幕上可用。 What to do then?: pass the method as props to the component children as needed.
接下来该怎么做?:根据需要将方法作为道具传递给子组件。
Example: 例:
navegarADetalleInstitucion = (institucion) => {
console.log('[Navigating to institution]', institucion);
this.props.navigator.push({
screen: 'serviciosdominicanos.Servicios',
title: 'Listado de Servicios',
passProps: {
institucion
}
});
}
renderInstituciones = () => {
return this.state.instituciones.map(inst => <Institucion key={inst} institucion={inst} navegacion={() => this.navegarADetalleInstitucion(inst)} />)
}
If navegarADetalleInstitucion was defined on the child component and I tried calling it from there, I'd get the exact same error you're receiving. 如果在子组件上定义了navegarADetalleInstitucion,而我尝试从那里调用它,则将收到与您收到的错误完全相同的错误。 But as I'm sending it as a prop called navegacion, when the child component executes navegacion, it is going to work just fine.
但是,当我将其作为名为navegacion的道具发送时,当子组件执行navegacion时,它将可以正常工作。
I know this question is rather old, but I hope it helps other developers that encounter this error in the future. 我知道这个问题已经很老了,但是我希望它可以帮助将来遇到此错误的其他开发人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.