Javascript undefined不是本机对象
我不知道为什么我会这样 “undefined不是对象(正在评估) “\u this.props.navigation.navigate”)” 我是react的新手,但我已经经历了每一个可能的解决方案,但仍然犯了这个错误。我在下面分享我的代码: App.jsJavascript undefined不是本机对象,javascript,android,reactjs,react-native,react-router,Javascript,Android,Reactjs,React Native,React Router,我不知道为什么我会这样 “undefined不是对象(正在评估) “\u this.props.navigation.navigate”)” 我是react的新手,但我已经经历了每一个可能的解决方案,但仍然犯了这个错误。我在下面分享我的代码: App.js import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import Login from './app/components/Log
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Login from './app/components/Login/Login';
import Dashboard from './app/components/Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
const Application = StackNavigator({
Home: { screen: Login },
Dashboard: { screen: Dashboard },
}, {
navigationOptions: {
header: false
}
});
export default class App extends React.Component {
render() {
return (
<Application />
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
import React from 'react';
import { StyleSheet, View, Image, Text, TextInput, KeyboardAvoidingView } from 'react-native';
import LoginForm from './LoginForm';
import {StackNavigator} from 'react-navigation';
export default class Login extends React.Component {
render() {
return (
<KeyboardAvoidingView behavior='padding' style={styles.container}>
<View style={styles.logoContainer}>
<Image source={require('../../images/logo.png')}
style={styles.logo}
/>
</View>
<Text style={styles.title}>Share Emotions Instantly..</Text>
<View style={styles.formContainer}>
<LoginForm></LoginForm>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#bdc3c7',
alignItems: 'center',
justifyContent: 'center',
},
logoContainer: {
alignItems: 'center',
justifyContent: 'center'
},
logo: {
width: 70,
height: 70
}
});
import React from 'react';
import { StyleSheet, View, TextInput, TouchableOpacity, Text, Alert } from 'react-native';
import Dashboard from '../Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
export default class LoginForm extends React.Component {
constructor(props) {
super(props);
}
onButtonPress = () => {
alert('ok');
const { navigate } = this.props.navigation;
navigate('Dashboard');
}
render() {
return (
<View style={styles.container}>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="username or email"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType="next"
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
onSubmitEditing={() => this.passwordInput.focus()}>
</TextInput>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="password"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType='go'
ref={(input) => this.passwordInput = input}
secureTextEntry>
</TextInput>
<TouchableOpacity style={styles.buttonContainer}
onPress={this.onButtonPress}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
padding: 20
},
input: {
height: 40,
marginBottom: 15,
backgroundColor: 'rgba(255,255,255,0.2)',
paddingHorizontal: 10,
width: 300
},
buttonContainer: {
backgroundColor: '#888',
paddingVertical: 10
},
buttonText: {
textAlign: 'center',
color: '#fff',
fontWeight: '700'
}
});
import React from 'react';
import { StyleSheet, View } from 'react-native';
import {StackNavigator} from 'react-navigation';
export default class Dashboard extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>this is Dashboard</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
从“React”导入React;
从“react native”导入{样式表、文本、视图};
从“./app/components/Login/Login”导入登录名;
从“./app/components/Dashboard/Dashboard”导入仪表板;
从“react navigation”导入{StackNavigator};
const Application=StackNavigator({
主页:{屏幕:登录},
仪表板:{屏幕:仪表板},
}, {
导航选项:{
标题:false
}
});
导出默认类App扩展React.Component{
render(){
返回(
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
对齐项目:“居中”,
为内容辩护:“中心”,
},
});
Login.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Login from './app/components/Login/Login';
import Dashboard from './app/components/Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
const Application = StackNavigator({
Home: { screen: Login },
Dashboard: { screen: Dashboard },
}, {
navigationOptions: {
header: false
}
});
export default class App extends React.Component {
render() {
return (
<Application />
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
import React from 'react';
import { StyleSheet, View, Image, Text, TextInput, KeyboardAvoidingView } from 'react-native';
import LoginForm from './LoginForm';
import {StackNavigator} from 'react-navigation';
export default class Login extends React.Component {
render() {
return (
<KeyboardAvoidingView behavior='padding' style={styles.container}>
<View style={styles.logoContainer}>
<Image source={require('../../images/logo.png')}
style={styles.logo}
/>
</View>
<Text style={styles.title}>Share Emotions Instantly..</Text>
<View style={styles.formContainer}>
<LoginForm></LoginForm>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#bdc3c7',
alignItems: 'center',
justifyContent: 'center',
},
logoContainer: {
alignItems: 'center',
justifyContent: 'center'
},
logo: {
width: 70,
height: 70
}
});
import React from 'react';
import { StyleSheet, View, TextInput, TouchableOpacity, Text, Alert } from 'react-native';
import Dashboard from '../Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
export default class LoginForm extends React.Component {
constructor(props) {
super(props);
}
onButtonPress = () => {
alert('ok');
const { navigate } = this.props.navigation;
navigate('Dashboard');
}
render() {
return (
<View style={styles.container}>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="username or email"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType="next"
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
onSubmitEditing={() => this.passwordInput.focus()}>
</TextInput>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="password"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType='go'
ref={(input) => this.passwordInput = input}
secureTextEntry>
</TextInput>
<TouchableOpacity style={styles.buttonContainer}
onPress={this.onButtonPress}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
padding: 20
},
input: {
height: 40,
marginBottom: 15,
backgroundColor: 'rgba(255,255,255,0.2)',
paddingHorizontal: 10,
width: 300
},
buttonContainer: {
backgroundColor: '#888',
paddingVertical: 10
},
buttonText: {
textAlign: 'center',
color: '#fff',
fontWeight: '700'
}
});
import React from 'react';
import { StyleSheet, View } from 'react-native';
import {StackNavigator} from 'react-navigation';
export default class Dashboard extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>this is Dashboard</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
从“React”导入React;
从“react native”导入{样式表、视图、图像、文本、文本输入、键盘AVOIDGVIEW};
从“/LoginForm”导入LoginForm;
从“react navigation”导入{StackNavigator};
导出默认类登录扩展React.Component{
render(){
返回(
立即分享情感。。
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
背景颜色:“#bdc3c7”,
对齐项目:“居中”,
为内容辩护:“中心”,
},
标识容器:{
对齐项目:“居中”,
为内容辩护:“中心”
},
标志:{
宽度:70,
身高:70
}
});
LoginForm.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Login from './app/components/Login/Login';
import Dashboard from './app/components/Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
const Application = StackNavigator({
Home: { screen: Login },
Dashboard: { screen: Dashboard },
}, {
navigationOptions: {
header: false
}
});
export default class App extends React.Component {
render() {
return (
<Application />
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
import React from 'react';
import { StyleSheet, View, Image, Text, TextInput, KeyboardAvoidingView } from 'react-native';
import LoginForm from './LoginForm';
import {StackNavigator} from 'react-navigation';
export default class Login extends React.Component {
render() {
return (
<KeyboardAvoidingView behavior='padding' style={styles.container}>
<View style={styles.logoContainer}>
<Image source={require('../../images/logo.png')}
style={styles.logo}
/>
</View>
<Text style={styles.title}>Share Emotions Instantly..</Text>
<View style={styles.formContainer}>
<LoginForm></LoginForm>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#bdc3c7',
alignItems: 'center',
justifyContent: 'center',
},
logoContainer: {
alignItems: 'center',
justifyContent: 'center'
},
logo: {
width: 70,
height: 70
}
});
import React from 'react';
import { StyleSheet, View, TextInput, TouchableOpacity, Text, Alert } from 'react-native';
import Dashboard from '../Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
export default class LoginForm extends React.Component {
constructor(props) {
super(props);
}
onButtonPress = () => {
alert('ok');
const { navigate } = this.props.navigation;
navigate('Dashboard');
}
render() {
return (
<View style={styles.container}>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="username or email"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType="next"
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
onSubmitEditing={() => this.passwordInput.focus()}>
</TextInput>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="password"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType='go'
ref={(input) => this.passwordInput = input}
secureTextEntry>
</TextInput>
<TouchableOpacity style={styles.buttonContainer}
onPress={this.onButtonPress}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
padding: 20
},
input: {
height: 40,
marginBottom: 15,
backgroundColor: 'rgba(255,255,255,0.2)',
paddingHorizontal: 10,
width: 300
},
buttonContainer: {
backgroundColor: '#888',
paddingVertical: 10
},
buttonText: {
textAlign: 'center',
color: '#fff',
fontWeight: '700'
}
});
import React from 'react';
import { StyleSheet, View } from 'react-native';
import {StackNavigator} from 'react-navigation';
export default class Dashboard extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>this is Dashboard</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
从“React”导入React;
从“react native”导入{样式表、视图、文本输入、TouchableOpacity、文本、警报};
从“../Dashboard/Dashboard”导入仪表板;
从“react navigation”导入{StackNavigator};
导出默认类LoginForm扩展React.Component{
建造师(道具){
超级(道具);
}
onButtonPress=()=>{
警报(“正常”);
const{navigate}=this.props.navigation;
导航(“仪表板”);
}
render(){
返回(
this.passwordInput.focus()}>
this.passwordInput=input}
secureTextEntry>
登录
);
}
}
const styles=StyleSheet.create({
容器:{
填充:20
},
输入:{
身高:40,
marginBottom:15,
背景颜色:“rgba(255255,0.2)”,
水平方向:10,
宽度:300
},
按钮容器:{
背景颜色:“#888”,
填充垂直:10
},
按钮文字:{
textAlign:'中心',
颜色:“#fff”,
重量:'700'
}
});
Dashboard.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Login from './app/components/Login/Login';
import Dashboard from './app/components/Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
const Application = StackNavigator({
Home: { screen: Login },
Dashboard: { screen: Dashboard },
}, {
navigationOptions: {
header: false
}
});
export default class App extends React.Component {
render() {
return (
<Application />
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
import React from 'react';
import { StyleSheet, View, Image, Text, TextInput, KeyboardAvoidingView } from 'react-native';
import LoginForm from './LoginForm';
import {StackNavigator} from 'react-navigation';
export default class Login extends React.Component {
render() {
return (
<KeyboardAvoidingView behavior='padding' style={styles.container}>
<View style={styles.logoContainer}>
<Image source={require('../../images/logo.png')}
style={styles.logo}
/>
</View>
<Text style={styles.title}>Share Emotions Instantly..</Text>
<View style={styles.formContainer}>
<LoginForm></LoginForm>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#bdc3c7',
alignItems: 'center',
justifyContent: 'center',
},
logoContainer: {
alignItems: 'center',
justifyContent: 'center'
},
logo: {
width: 70,
height: 70
}
});
import React from 'react';
import { StyleSheet, View, TextInput, TouchableOpacity, Text, Alert } from 'react-native';
import Dashboard from '../Dashboard/Dashboard';
import {StackNavigator} from 'react-navigation';
export default class LoginForm extends React.Component {
constructor(props) {
super(props);
}
onButtonPress = () => {
alert('ok');
const { navigate } = this.props.navigation;
navigate('Dashboard');
}
render() {
return (
<View style={styles.container}>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="username or email"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType="next"
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
onSubmitEditing={() => this.passwordInput.focus()}>
</TextInput>
<TextInput underlineColorAndroid="transparent"
style={styles.input}
placeholder="password"
placeholderTextColor='rgba(255,255,255,0.5)'
returnKeyType='go'
ref={(input) => this.passwordInput = input}
secureTextEntry>
</TextInput>
<TouchableOpacity style={styles.buttonContainer}
onPress={this.onButtonPress}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
padding: 20
},
input: {
height: 40,
marginBottom: 15,
backgroundColor: 'rgba(255,255,255,0.2)',
paddingHorizontal: 10,
width: 300
},
buttonContainer: {
backgroundColor: '#888',
paddingVertical: 10
},
buttonText: {
textAlign: 'center',
color: '#fff',
fontWeight: '700'
}
});
import React from 'react';
import { StyleSheet, View } from 'react-native';
import {StackNavigator} from 'react-navigation';
export default class Dashboard extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>this is Dashboard</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
从“React”导入React;
从“react native”导入{StyleSheet,View};
从“react navigation”导入{StackNavigator};
导出默认类Dashboard扩展React.Component{
render(){
返回(
这是仪表板
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1
}
});
我知道这是一个常见的问题,但我仍然不明白为什么“导航”对象没有作为道具传递给其他组件。导航属性将被注入到为ScreenNavigator添加到route configs的所有屏幕中(在您的情况下为Home和Dashboard)。但是,对于这些屏幕的子组件,您必须传递导航属性
我看不出您在任何地方试图将导航作为道具传递到哪里?我对自己的反应还不熟悉,但我认为您需要在登录组件中创建LoginForm元素时传递属性@让我没有什么可以通过的。现在只想在单击按钮时导航到仪表板页面。如果我仍然需要这样做,你能告诉我通过哪个属性以及如何通过吗?我通过了教程,虽然没有通过,但教程仍然有效。我真的需要传递什么吗?@ChrisPossible replication of i see!!情况就是这样。谢谢你的回复和描述。你能回答我吗:我没有看到足够的优势来使用它。已经公布的答案似乎是公平的