React native Undefined不是本机StackNavigator的对象
我一直试图让一个简单的反应本地StackNavigation示例应用程序工作,但我一直得到一个React native Undefined不是本机StackNavigator的对象,react-native,react-native-android,react-native-navigation,stack-navigator,React Native,React Native Android,React Native Navigation,Stack Navigator,我一直试图让一个简单的反应本地StackNavigation示例应用程序工作,但我一直得到一个 TypeError:undefined不是对象(计算'this.props.navigation.navigate') 我不希望应用程序在这个阶段导航到任何地方,只需使用一个应用程序栏和一些任意文本进行部署 import React, { Component } from 'react'; import {AppRegistry, Text} from 'react-native'; import {
TypeError:undefined不是对象(计算'this.props.navigation.navigate')
我不希望应用程序在这个阶段导航到任何地方,只需使用一个应用程序栏和一些任意文本进行部署
import React, { Component } from 'react';
import {AppRegistry, Text} from 'react-native';
import {StackNavigator} from 'react-navigation';
export default class App extends React.Component {
static navigationOptions = {
title: 'Home',
};
render() {
const { navigate } = this.props.navigation;
return (
<Text> Hello World </Text>
);
}
}
const appScreens = StackNavigator({
Home: {screen: App},
})
AppRegistry.registerComponent('IntervalTimer', () => appScreens);
import React,{Component}来自'React';
从“react native”导入{AppRegistry,Text};
从“react navigation”导入{StackNavigator};
导出默认类App扩展React.Component{
静态导航选项={
标题:"家",,
};
render(){
const{navigate}=this.props.navigation;
返回(
你好,世界
);
}
}
const appScreens=StackNavigator({
主页:{screen:App},
})
AppRegistry.registerComponent('IntervalTimer',()=>appScreens);
错误是报告const{navigate}=this.props.navigation代码>声明。删除这一行确实允许应用程序进行部署,但正如我所期望的那样,没有标题
StackNavigator
是使用NPM安装的,正在导入应用程序
这里也有类似的问题,我尝试过他们的建议。感谢您提供的任何帮助 您可以在StackNavigator的选项中添加initialRouteName。试试这个
import React, { Component } from 'react';
import {AppRegistry, Text} from 'react-native';
import {StackNavigator} from 'react-navigation';
class App extends React.Component {
static navigationOptions = {
title: 'Home',
};
render() {
const { navigate } = this.props.navigation;
return (
<Text> Hello World </Text>
);
}
}
export const appScreens = StackNavigator({
Home: { screen: App }
},{
initialRouteName: Home
})
AppRegistry.registerComponent('IntervalTimer', () => appScreens);
import React,{Component}来自'React';
从“react native”导入{AppRegistry,Text};
从“react navigation”导入{StackNavigator};
类应用程序扩展了React.Component{
静态导航选项={
标题:"家",,
};
render(){
const{navigate}=this.props.navigation;
返回(
你好,世界
);
}
}
导出常量appScreens=StackNavigator({
主页:{屏幕:应用程序}
},{
initialRouteName:主页
})
AppRegistry.registerComponent('IntervalTimer',()=>appScreens);
如果这只是道具有可能未定义,您可以检查未定义
const { navigate } = this.props.navigation || {};
假设在某个点上渲染中定义了导航
,则上述内容应该可以安全使用。您可以尝试记录它,看看它是否总是未定义的,或者它是否在某个点被定义
console.log(navigate)
输出可能是
undefined
undefined
//defined