javascript - React Navigation Invarint Violation
问题描述
I recently started to use React Navigation to go beetween screens. I'm stuck on an error and can't go foward. I'm no sure if it's an error of my project or I'm missing some crucial stuff. I'm tried to reproduce the code from the docs, but didn't work.
Here's the error:
My App.js:
import React from 'react';
import {View, Text, Button} from "react-navigation";
import { createAppContainer, createStackNavigator, StackActions, NavigationActions } from 'react-navigation';
class LoginScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
this.props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Home' })
],
}))
}}
/>
</View>
);
}
}
class HomeScreen extends React.Component {
render(){
return(
<View>
<Text>
Home Screen
</Text>
</View>
);
}
}
const AppNavigator = createStackNavigator({
Home: {
screen: HomeScreen,
},
Login: {
screen: LoginScreen,
},
}, {
initialRouteName: 'Login',
});
export default createAppContainer(AppNavigator);
This is my index.js:
import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
This is my MainActivity.java with all the necessary modifications:
package com.ggwpapplication;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "ggWPapplication";
}
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
}
Note: Before using React Navigation it was all fine.
解决方案
您的 App.js 文件中有错误的导入:
改变:
import {View, Text, Button} from "react-navigation";
至:
import {View, Text, Button} from "react-native";
推荐阅读
- python - QFileDialog 在取消时崩溃
- c# - 使用 C# SDK 在 amazon s3 中获取存储桶使用的存储空间
- r - 在ggplot中绘制多条线性回归线
- python - 芹菜定期任务不定期
- amazon-web-services - 接收有关进入停止状态的任何特定 Windows 服务的警报
- php - 围绕会计 api 构建 webapp
- php - Laravel 记住我功能
- azure-resource-manager - 部署 ARM 模板时的错误
- c# - 是否有 WINAPI 或 P/Invoke 方法来剪辑/屏蔽 C# 中的窗口?
- csv - 如何使用谷歌云存储文件中的 gs:// 链接读取 csv 中的字符串内容