[英]React-native:cannot read property 'navigate' of undefined
I'm getting below error when im trying to navigate to different screen from home screen. 当我尝试从主屏幕导航到其他屏幕时,出现以下错误。 i defined initial loading screen in routes.
我在路线中定义了初始加载屏幕。 From initial screen trying to navigate to different screen then getting below error.
从初始屏幕尝试导航到其他屏幕,然后出现以下错误。
this.props is returning just {}. this.props仅返回{}。 Not really sure why.
不太确定为什么。
login.component.js login.component.js
import React, {Component} from 'react';
import {NavigationActions} from 'react-navigation';
import {
Platform,
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
Touchable,
Image,
Button
} from 'react-native';
import { handleFbLogin } from '../../config/authConfig/';
class Login extends Component {
render () {
console.log("this.props")
console.log(this.props)
return (
<View style={{flex: 1,backgroundColor: '#37afa6'}}>
<Button
title="Go to Details"
onPress={() => this.props.navigation.navigate('setupProfile')}
/>
</View>
);
}
}
export default Login;
routes/index.js 路线/index.js
import {StackNavigator} from 'react-navigation';
import Login from '../pages/Login.page';
import SetupProfile from '../pages/setupProfile.page';
const Config = {
navigation: {
login: {
screen: Login
},
setupProfile: {
screen: SetupProfile,
}
}
}
export default Config;
App.container.js App.container.js
import React, {Component} from 'react';
import Login from './pages/Login.page';
import {connect} from 'react-redux';
import Router from './routes';
import {
Platform,
StyleSheet,
Text,
View,
TextInput
} from 'react-native';
class App extends Component {
render () {
return (
<Router />
);
}
}
export default App;
index.js(startup point): index.js(启动点):
import { AppRegistry } from 'react-native';
import { StackNavigator } from 'react-navigation';
import app from './app/index';
import Config from './app/routes/index';
export const AppNavigator = StackNavigator(Config.navigation,{initialRouteName : 'login'});
AppRegistry.registerComponent('BuddApp', () => AppNavigator);
export default app;
i'm able to load initalscreen after these changes but when trying to navigate still getting the same error. 这些更改后,我能够加载initalscreen,但是在尝试导航时仍然遇到相同的错误。
EDIT : 编辑:
I got it. 我知道了。 You forgot the constructor in your login screen, that's why your props are empty :
您在登录屏幕上忘记了构造函数,这就是为什么道具为空的原因:
constructor(props) {
super(props);
}
PREVIOUS ANSWER : 上一个答案:
Without the full code, i can't see what the exact problem is, but i will give you an exemple of use of react-navigation : 没有完整的代码,我看不到确切的问题是什么,但是我会给你一个使用react-navigation的例子:
In your index.js (start point of your app) : 在index.js(应用的起点)中:
import { AppRegistry } from 'react-native';
import { StackNavigator } from 'react-navigation';
export const AppNavigator = StackNavigator(Config.navigation);
AppRegistry.registerComponent('appName', () => AppNavigator);
In the navigation config : 在导航配置中:
const Config = {
navigation: {
login: {
screen: Login
},
setupProfile: {
screen: SetupProfile,
}
}
}
The first object found in the navigation config is the start of your app. 在导航配置中找到的第一个对象是您的应用程序的开始。
Now you can use your navigation function in the "login" page with navigate : 现在,您可以在浏览器的“登录”页面中使用导航功能:
this.props.navigation.navigate('setupProfile')
If the AppRegistry is filled everything should run fine. 如果AppRegistry已填满,则一切运行正常。
I hope it help ! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.