我是第一次接触react-native,我正在尝试实现react-stack-navigator
,但函数createStackNavigator
返回null。我一直在遵循一个教程,在相同的代码中没有这个问题,任何想法都将不胜感激。下面是我的错误:
下面是我的代码:
import { createStackNavigator } from "react-navigation-stack";
import { createAppContainer } from "react-navigation";
import { Home } from "../components/Home";
import { Test } from "../components/Test";
const screens = {
home: {
screen: Home
},
test: {
screen: Test
}
};
const HomeStack = createStackNavigator(screens);
export default createAppContainer(HomeStack);
这是我的App.js文件:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from "react";
import {
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button
} from "react-native";
import Navigator from "./routes/homeStack";
export class App extends Component {
constructor() {}
render() {
return <Navigator />;
}
}
const styles = StyleSheet.create({});
以下是home组件:
import React, { Component } from "react";
import {
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button
} from "react-native";
import { About } from "./About";
import { Contact } from "./Contact";
export class Home extends Component {
constructor() {
super();
}
render() {
return (
<>
<View>
<Text>This is Home Component</Text>
</View>
<Button
title="navigate to Test"
onPress={() => this.props.navigation.navigate("test")}
></Button>
</>
);
}
}
下面是测试组件:
import React, { Component } from "react";
import {
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button
} from "react-native";
export class Test extends Component {
constructor() {
super();
this.state = {
something: "This is Test Component"
};
}
render() {
return (
<>
<View>
<Text>this.state.something</Text>
</View>
<Button
title="navigate to Home"
onPress={() => this.props.navigation.navigate("home")}
></Button>
</>
);
}
}
下面是package.json的代码:
{
"name": "reviewsApp2",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"@react-native-community/masked-view": "^0.1.5",
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-gesture-handler": "^1.5.3",
"react-native-reanimated": "^1.4.0",
"react-native-safe-area-context": "^0.6.2",
"react-native-screens": "^2.0.0-alpha.23",
"react-navigation": "^4.0.10",
"react-navigation-stack": "^2.0.2"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/runtime": "^7.6.2",
"@react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.9.0",
"eslint": "^6.5.1",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
发布于 2020-01-15 12:30:08
一双新鲜的眼睛总是会产生不同的影响(欢迎使用React-Native和dev )。
您将在Home
中导入两个组件,即About
和Contact
。
import { About } from "./About";
import { Contact } from "./Contact"
根据我们所拥有的上下文,这些并不存在。删除这些导入或将它们注释掉都可以解决您的错误。
令人恼火的是,React-Native并不总是呈现最好的错误消息,但它正在积极地进行处理。也就是说,当我附加了调试器和没有附加调试器时,我有时会得到不同的错误消息(值得对此进行处理)。
另一个更改是更改App
文件的类型导出,即
export default App
掌握两种不同类型的导出/导入(即默认导出/导入与命名导出/导入)的概念将非常有益。
请参阅工作示例:https://snack.expo.io/r1wVXFheI
https://stackoverflow.com/questions/59750890
复制相似问题