React Native 导航:undefined 不是一个对象
React Native navigation: undefined is not an object
我尝试根据 this 教程使用 react-navigation 在 React Native 中实现导航,但在启动应用程序时遇到以下错误:
undefined 不是对象(正在计算 'this.props.navigation.navigate')
渲染 index.android.js:17:12
我的index.android.js:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button
} from 'react-native';
import {
StackNavigator,
} from 'react-navigation';
export default class Abc extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Button
title="Go to List"
onPress={() =>
navigate('Liste')
}
/>
</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,
},
});
export class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to List"
onPress={() =>
navigate('Profile')
}
/>
);
}
}
export class Liste extends React.Component {
static navigationOptions = {
title: 'Liste',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Liste"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
你有什么建议吗?谢谢!
您没有 Profile
屏幕。
如果您想从 HomeScreen
转到 Liste
屏幕,请使用:
onPress={() => navigate('Liste')}
如果您想转到 Profile
屏幕,则需要制作 Profile Screen
并将其添加到 StackNavigator
,示例:
export class Profile extends React.Component {
static navigationOptions = {
title: 'Profile',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Profile"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
Profile: {screen: Profile},
});
我相信您想做的是在 AppRegistry
中注册 App
组件而不是 Abc
组件。
您 运行 进入此 undefined is not an object (Evaluating 'this.props.navigation.navigate')
的原因是因为 props.navigation
在您的 Abc
组件中不可用。如果您仔细查看代码的底部,
你有:
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
因此您创建了一个 StackNavigator
组件,但您没有注册该组件,而是在 AppRegistry
中注册了 Abc
组件,并且由于 Abc
组件不是您 StackNavigator
下的任何屏幕,它无法访问 this.props.navigation
因此您会 运行 进入异常。
距离提出最初的问题已经快 3 年了。我遇到了同样的问题并登陆了这个页面。
react-navigation 现在是 v5。我遇到的问题是因为 'navigation' 没有传递给子组件。
经过更多研究后,我使用此处的解决方案解决了问题。
我需要像这样将道具传递给子组件。
<GoToButton navigation={props.navigation} />
https://reactnavigation.org/docs/connecting-navigation-prop
希望这对像我这样的新手有所帮助。
我尝试根据 this 教程使用 react-navigation 在 React Native 中实现导航,但在启动应用程序时遇到以下错误:
undefined 不是对象(正在计算 'this.props.navigation.navigate') 渲染 index.android.js:17:12
我的index.android.js:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button
} from 'react-native';
import {
StackNavigator,
} from 'react-navigation';
export default class Abc extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Button
title="Go to List"
onPress={() =>
navigate('Liste')
}
/>
</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,
},
});
export class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to List"
onPress={() =>
navigate('Profile')
}
/>
);
}
}
export class Liste extends React.Component {
static navigationOptions = {
title: 'Liste',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Liste"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
你有什么建议吗?谢谢!
您没有 Profile
屏幕。
如果您想从 HomeScreen
转到 Liste
屏幕,请使用:
onPress={() => navigate('Liste')}
如果您想转到 Profile
屏幕,则需要制作 Profile Screen
并将其添加到 StackNavigator
,示例:
export class Profile extends React.Component {
static navigationOptions = {
title: 'Profile',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Profile"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
Profile: {screen: Profile},
});
我相信您想做的是在 AppRegistry
中注册 App
组件而不是 Abc
组件。
您 运行 进入此 undefined is not an object (Evaluating 'this.props.navigation.navigate')
的原因是因为 props.navigation
在您的 Abc
组件中不可用。如果您仔细查看代码的底部,
你有:
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
因此您创建了一个 StackNavigator
组件,但您没有注册该组件,而是在 AppRegistry
中注册了 Abc
组件,并且由于 Abc
组件不是您 StackNavigator
下的任何屏幕,它无法访问 this.props.navigation
因此您会 运行 进入异常。
距离提出最初的问题已经快 3 年了。我遇到了同样的问题并登陆了这个页面。
react-navigation 现在是 v5。我遇到的问题是因为 'navigation' 没有传递给子组件。
经过更多研究后,我使用此处的解决方案解决了问题。
我需要像这样将道具传递给子组件。
<GoToButton navigation={props.navigation} />
https://reactnavigation.org/docs/connecting-navigation-prop
希望这对像我这样的新手有所帮助。