【问题标题】:React Navigation Invarint Violation反应导航不变违规
【发布时间】:2019-05-28 23:16:39
【问题描述】:

我最近开始使用 React Navigation 在屏幕之间移动。我陷入了一个错误,无法前进。我不确定这是我的项目的错误还是我遗漏了一些关键的东西。我试图从文档中重现代码,但没有成功。

这是错误:

我的 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);

这是我的 index.js:

import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';


AppRegistry.registerComponent(appName, () => App);

这是我的 MainActivity.java 并进行了所有必要的修改:

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);
          }
        };
     }
}

注意:在使用 React Navigation 之前一切正常。

【问题讨论】:

    标签: javascript android react-native react-navigation


    【解决方案1】:

    您的 App.js 文件中有错误的导入:

    改变:

    import {View, Text, Button} from "react-navigation";
    

    到:

    import {View, Text, Button} from "react-native";
    

    【讨论】:

    • 我觉得自己像个白痴,我想念它。非常感谢!
    猜你喜欢
    • 2019-04-22
    • 2020-05-29
    • 2022-10-21
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 2020-06-14
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多