【发布时间】:2017-06-13 14:32:41
【问题描述】:
我是 React Native 的新手,我尝试按照教程学习,但是无法在屏幕之间进行导航。我以前在一个屏幕上可以正常工作,但是在添加导航时出现错误。
关注这个:https://facebook.github.io/react-native/releases/next/docs/navigation.html
给我这样的代码:
import React from 'react';
import {
StackNavigator,
} from 'react-navigation';
export default class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to Jane's profile"
onPress={() =>
navigate('Profile', { name: 'Jane' })
}
/>
);
}
}
class ProfileScreen extends React.Component {
static navigationOptions = {
title: 'Profile',
};
render() {
return (
<Button title="do nothing"/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Profile: { screen: ProfileScreen },
});
尝试运行此程序(通过 expo 应用程序)会导致错误
undefined 不是对象(评估 'this.props.navigation.navigate')
那么我的导航是否正确?如何解决这个错误?
【问题讨论】:
-
你在哪里渲染
<App/> -
你能在你调用
AppRegistry.registerComponent('MyAwesomeApp', () => App);的地方显示你的index.ios.js文件吗 -
您的 HomeScreen 组件中有构造函数吗?也许您无法访问道具。阅读:facebook.github.io/react/docs/react-component.html#constructor
标签: javascript react-native navigation