javascript - React 导航不变性违规

标签 javascript android react-native react-navigation

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

错误如下:

enter image description here

我的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 之前一切都很好。

最佳答案

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

更改:

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

至:

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

关于javascript - React 导航不变性违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56351179/

相关文章:

javascript - 我怎样才能告诉 MathJax 只处理一个节点?

android - Firemonkey:将 JPEG 加载到任何类型的图像控件中,然后保存到文件或流中,使其更小。这可以避免吗?

java - 将对象放入 ByteBuffer 而不对其进行序列化

reactjs - 即使在 React 中选中,复选框也总是传递 false

javascript - 如何反转 react native 矢量图标按钮颜色?

react-native - Cookie 未正确存储在 ios 模拟器中

javascript - 访问 setstate 之后执行的函数中的变量

javascript - 当我们使用 jQuery 使用 .clone() 创建元素的副本时,如何保留占位符文本?

javascript - 使用 vbscript 获取页面访问时的屏幕宽度

android - NestedScrollView 内的 fragment (Viewpager)未加载