我想第一次了解本机的反应。我试图引进一个stacknavigator但行const {navigate} = this.props.navigation;
导致错误:未定义不是一个对象(评估this.props.navigation.navigate)
undefined is not an object (evaluating 'this.props.navigation.navigate')
这里是我的代码:
import React, { Component } from "react";
import { StackNavigator } from "react-navigation";
import { AppRegistry, Text, View } from "react-native";
export default class App extends Component {
static navigationOptions = {
title: "Login",
headerStyle: {
backgroundColor: "#000000"
},
headerTitleStyle: {
color: "#fff"
}
};
constructor(props) {
super(props);
}
render() {
console.log(this.props);
const { navigate } = this.props.navigation;
return (
<View>
<Text>Hello</Text>
</View>
);
}
}
const myscreens = StackNavigator({
Home: { screen: App }
});
AppRegistry.registerComponent("app",() => myscreens);
什么我做错了,我该如何解决这个问题?
我还应该提到道具在渲染函数和构造函数中是空的。
,这里是我的package.json柜面它的事项
{
"name": "myapp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.0.0-alpha.12",
"react-native": "0.48.3",
"react-navigation": "git+https://github.com/react-community/react-navigation.git"
},
"devDependencies": {
"babel-jest": "21.0.2",
"babel-preset-react-native": "4.0.0",
"jest": "21.1.0",
"react-test-renderer": "16.0.0-alpha.12"
},
"jest": {
"preset": "react-native"
}
}
这是我的index.android.js
import React, { Component } from 'react';
import App from './components/app';
import {
AppRegistry,
View
} from 'react-native';
export default class myapp extends Component {
render() {
return (
<App />
);
}
}
AppRegistry.registerComponent('myapp',() => myapp);
我刚刚创建了一个全新的项目与刚装'反应,navigation'并没有得到错误。也许发布你的'package.json'? –
@MichaelCheng好的谢谢我刚刚添加我的package.json和我的index.android.js。希望这有助于? – John