undefined is not an object (evaluating 'this.props.navigation.navigate') react native 错误
undefined is not an object (evaluating 'this.props.navigation.navigate') error in react native
我阅读了与此错误相关的一些解决方案,但没有使用它。由于不同的原因,他们中的大多数人都遇到了同样的错误。我是 React-Native 的初学者。所以请帮帮我!
App.js
import loginScreen from './components/Login';
import React from 'react'
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import homeScreen from "./HomeScreen";
import LoadingScreen from "./components/LoadingScreen";
import SignUpScreen from "./components/SignUpScreen";
import StarterScreen from "./components/starter";
import ShoppingCartIconScreen from './components/ShoppingCartIcon';
import SummeryScreen from './components/summery'
const MainNavigator=createStackNavigator(
{
signup:{screen:SignUpScreen},
login:{screen:loginScreen},
Loading: {screen: LoadingScreen },
Summery:{screen:SummeryScreen}, //exporting the Summery component
Starter:{screen:StarterScreen,
navigationOptions:{
title:'Starters',
headerRight:<ShoppingCartIconScreen/>,//using as a icon on the navigation header.
headerStyle:{
backgroundColor:'#694fad'
},
headerTitleStyle:{
color:'white'
}
}},
Home: { screen: homeScreen,
navigationOptions:{
headerTitle:'Home',
headerRight:<ShoppingCartIconScreen/>,
headerStyle:{
backgroundColor:'#694fad',
},
headerTitleStyle:{
color:'white'
}
}}
},
{
initialRouteName:"Home" //set home as a default screen
}
);
const App=createAppContainer(MainNavigator);
export default App; //exporting App.js with stack navigator
ShoppingCartIcon.js
import React from 'react';
import {View,Text,StyleSheet,Platform} from 'react-native';
import Icon from '@expo/vector-icons/Ionicons'
//creating a constant
const ShoppingCartIcon = (props) => (
<View style={[{ padding: 5 }, Platform.OS == 'android' ? styles.iconContainer : null]}>
<View style={{
position: 'absolute', height: 20, width: 20, borderRadius: 15, backgroundColor: '#e3e3e3', right: 6, bottom: 29, alignItems: 'center', justifyContent: 'center', zIndex: 2500,
}}>
<Text style={{ color: 'black', fontWeight: 'bold' }}>0</Text>
</View>
<View style={{top:3}}>
<Icon onPress={()=>props.navigation.navigate("Summery")} name="ios-cart" size={35} color='yellow' /> //navigate to summery screen on icon press
</View>
</View>
)
export default ShoppingCartIcon; //exporting shoppingcarticon
Summery.js
//只是一个虚拟文件
从 'react' 导入 React
从 'react-native'
导入 {View,Text,StyleSheet}
//nothing special here
export default class summery extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>summery page</Text>
</View>
)
}
}
this.props.navigation
仅在导航器中直接分配为屏幕的组件中可用,例如createStackNavigator
.
生成的堆栈导航器
如果您想访问这些组件之外的导航,请直接传递 navigation
道具(我不推荐),或者按照本教程在没有 navigation
道具的情况下导航:https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
好吧,我认为你的错误是因为你没有将 ShoppingCartIcon 添加为 StackNavigator 的一部分,因此,你无法从 props 查询导航器属性。你可以做的是通过 ShoppingCartIcon 的道具传递 App 的导航器属性,我的意思是你应该写这样的东西
// App.js
import LoginScreen from './components/Login';
import React from 'react'
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import HomeScreen from "./HomeScreen";
import LoadingScreen from "./components/LoadingScreen";
import SignUpScreen from "./components/SignUpScreen";
import StarterScreen from "./components/starter";
import ShoppingCartIconScreen from './components/ShoppingCartIcon';
import SummeryScreen from './components/summery'
const MainNavigator=createStackNavigator(
{
Signup:SignUpScreen,
Login:LoginScreen, //Name of components have to start with Uppercase Letter
Loading: LoadingScreen,
Summery: SummeryScreen, //exporting the Summery component
Starter:StarterScreen,
Home: HomeScreen,
},
{
initialRouteName:"Home" //set home as a default screen
}
);
const AppContainer=createAppContainer(MainNavigator);
class App extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<AppContainer/>
)
}
}
export default App; //exporting App.js with stack navigator
//ShoppingCartIcon.js
import React from 'react';
import {View,Text,StyleSheet,Platform} from 'react-native';
import Icon from '@expo/vector-icons/Ionicons'
//creating a constant
const ShoppingCartIcon = (navigation) => (
<View style={[{ padding: 5 }, Platform.OS == 'android' ? styles.iconContainer : null]}>
<View style={{
position: 'absolute', height: 20, width: 20, borderRadius: 15, backgroundColor: '#e3e3e3', right: 6, bottom: 29, alignItems: 'center', justifyContent: 'center', zIndex: 2500,
}}>
<Text style={{ color: 'black', fontWeight: 'bold' }}>0</Text>
</View>
<View style={{top:3}}>
<Icon onPress={()=> navigation.navigate("Summery")} name="ios-cart" size={35} color='yellow' /> //navigate to summery screen on icon press
</View>
</View>
)
export default ShoppingCartIcon; //exporting shoppingcarticon
//Home.js or Starter.js
class Name extends React.Component{
//Add this
static navigationOptions = ({ navigation }) => { //Configuración de pantalla
return {
title:'Name',
headerRight:(<ShoppingCartIconScreen navigation = {navigation}/>),//using as a icon on the navigation header.
headerStyle:{
backgroundColor:'#694fad'
},
headerTitleStyle:{
color:'white'
}
};
};
}
在这种情况下,为每个屏幕定义导航选项是一种更好的方法。就像我在上面的代码中描述的那样。
此外,我建议使用这种方法来定义反应组件:
class App extends React.Component{
constructor(props){
super(props)
}
//Some functions ...
render(){ return(//.... what you want)}
希望对您有所帮助:)
我阅读了与此错误相关的一些解决方案,但没有使用它。由于不同的原因,他们中的大多数人都遇到了同样的错误。我是 React-Native 的初学者。所以请帮帮我!
App.js
import loginScreen from './components/Login';
import React from 'react'
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import homeScreen from "./HomeScreen";
import LoadingScreen from "./components/LoadingScreen";
import SignUpScreen from "./components/SignUpScreen";
import StarterScreen from "./components/starter";
import ShoppingCartIconScreen from './components/ShoppingCartIcon';
import SummeryScreen from './components/summery'
const MainNavigator=createStackNavigator(
{
signup:{screen:SignUpScreen},
login:{screen:loginScreen},
Loading: {screen: LoadingScreen },
Summery:{screen:SummeryScreen}, //exporting the Summery component
Starter:{screen:StarterScreen,
navigationOptions:{
title:'Starters',
headerRight:<ShoppingCartIconScreen/>,//using as a icon on the navigation header.
headerStyle:{
backgroundColor:'#694fad'
},
headerTitleStyle:{
color:'white'
}
}},
Home: { screen: homeScreen,
navigationOptions:{
headerTitle:'Home',
headerRight:<ShoppingCartIconScreen/>,
headerStyle:{
backgroundColor:'#694fad',
},
headerTitleStyle:{
color:'white'
}
}}
},
{
initialRouteName:"Home" //set home as a default screen
}
);
const App=createAppContainer(MainNavigator);
export default App; //exporting App.js with stack navigator
ShoppingCartIcon.js
import React from 'react';
import {View,Text,StyleSheet,Platform} from 'react-native';
import Icon from '@expo/vector-icons/Ionicons'
//creating a constant
const ShoppingCartIcon = (props) => (
<View style={[{ padding: 5 }, Platform.OS == 'android' ? styles.iconContainer : null]}>
<View style={{
position: 'absolute', height: 20, width: 20, borderRadius: 15, backgroundColor: '#e3e3e3', right: 6, bottom: 29, alignItems: 'center', justifyContent: 'center', zIndex: 2500,
}}>
<Text style={{ color: 'black', fontWeight: 'bold' }}>0</Text>
</View>
<View style={{top:3}}>
<Icon onPress={()=>props.navigation.navigate("Summery")} name="ios-cart" size={35} color='yellow' /> //navigate to summery screen on icon press
</View>
</View>
)
export default ShoppingCartIcon; //exporting shoppingcarticon
Summery.js //只是一个虚拟文件 从 'react' 导入 React 从 'react-native'
导入 {View,Text,StyleSheet}//nothing special here
export default class summery extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>summery page</Text>
</View>
)
}
}
this.props.navigation
仅在导航器中直接分配为屏幕的组件中可用,例如createStackNavigator
.
如果您想访问这些组件之外的导航,请直接传递 navigation
道具(我不推荐),或者按照本教程在没有 navigation
道具的情况下导航:https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
好吧,我认为你的错误是因为你没有将 ShoppingCartIcon 添加为 StackNavigator 的一部分,因此,你无法从 props 查询导航器属性。你可以做的是通过 ShoppingCartIcon 的道具传递 App 的导航器属性,我的意思是你应该写这样的东西
// App.js
import LoginScreen from './components/Login';
import React from 'react'
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import HomeScreen from "./HomeScreen";
import LoadingScreen from "./components/LoadingScreen";
import SignUpScreen from "./components/SignUpScreen";
import StarterScreen from "./components/starter";
import ShoppingCartIconScreen from './components/ShoppingCartIcon';
import SummeryScreen from './components/summery'
const MainNavigator=createStackNavigator(
{
Signup:SignUpScreen,
Login:LoginScreen, //Name of components have to start with Uppercase Letter
Loading: LoadingScreen,
Summery: SummeryScreen, //exporting the Summery component
Starter:StarterScreen,
Home: HomeScreen,
},
{
initialRouteName:"Home" //set home as a default screen
}
);
const AppContainer=createAppContainer(MainNavigator);
class App extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<AppContainer/>
)
}
}
export default App; //exporting App.js with stack navigator
//ShoppingCartIcon.js
import React from 'react';
import {View,Text,StyleSheet,Platform} from 'react-native';
import Icon from '@expo/vector-icons/Ionicons'
//creating a constant
const ShoppingCartIcon = (navigation) => (
<View style={[{ padding: 5 }, Platform.OS == 'android' ? styles.iconContainer : null]}>
<View style={{
position: 'absolute', height: 20, width: 20, borderRadius: 15, backgroundColor: '#e3e3e3', right: 6, bottom: 29, alignItems: 'center', justifyContent: 'center', zIndex: 2500,
}}>
<Text style={{ color: 'black', fontWeight: 'bold' }}>0</Text>
</View>
<View style={{top:3}}>
<Icon onPress={()=> navigation.navigate("Summery")} name="ios-cart" size={35} color='yellow' /> //navigate to summery screen on icon press
</View>
</View>
)
export default ShoppingCartIcon; //exporting shoppingcarticon
//Home.js or Starter.js
class Name extends React.Component{
//Add this
static navigationOptions = ({ navigation }) => { //Configuración de pantalla
return {
title:'Name',
headerRight:(<ShoppingCartIconScreen navigation = {navigation}/>),//using as a icon on the navigation header.
headerStyle:{
backgroundColor:'#694fad'
},
headerTitleStyle:{
color:'white'
}
};
};
}
在这种情况下,为每个屏幕定义导航选项是一种更好的方法。就像我在上面的代码中描述的那样。 此外,我建议使用这种方法来定义反应组件:
class App extends React.Component{
constructor(props){
super(props)
}
//Some functions ...
render(){ return(//.... what you want)}
希望对您有所帮助:)