0

因为我有一个非常过时的应用程序,react-native 0.59.9 + native-base 2.10.0,或者决定从一个使用经典的干净项目开始更新它:

npx react-native init AwesomeProject

此时我还安装了新的 native-base 3.2.1 并且我翻转了所有文件并更新了它们的依赖项,很多东西都不同了,尤其是那些与 react-navigation 相关的东西。

一旦解决了依赖关系,编译就会正确进行,从 Metro 捆绑器(react-native start)可以看出,但应用程序立即崩溃报告:

Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle.
 ERROR TypeError: undefined is not an object (evaluating 'Comp.displayName')
 ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
      This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
 ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
      This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.

我试图清除缓存,删除 node_modules 并重新创建它,但总是出现错误。据我了解,appRegistry 在早期阶段存在一些问题,这些是我认为发生异常情况的文件。

应用程序.json

{
  "name": "MyApp",
  "displayName": "MyApp"
}

index.js

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

//Disable the FontScaling set by the device (Component "Text").
Text.defaultProps = Text.defaultProps || {};
Text.defaultProps.allowFontScaling = false;

//Disable the FontScaling set by the device (Component "TextInput").
TextInput.defaultProps = Text.defaultProps || {};
TextInput.defaultProps.allowFontScaling = false;

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

应用程序.js

import React from 'react';
import Setup from './src/boot/setup.js';

export default class App extends React.Component {
    render() {
        return (
            <Setup/>
        );
    }
}

./src/boot/setup.js(这里我插入了之前没用过的NativeBaseProvider组件。)

import React from 'react';
import { NativeBaseProvider } from 'native-base';
import App from '../App.js';

export default class Setup extends React.Component {
  render() {
    return (
        <NativeBaseProvider>
          <App />
        </NativeBaseProvider>
    );
  }
}

./src/App.js

import React from 'react';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { createSwitchNavigator } from "@react-navigation/compat";

import Home from './components/Home/homeScreen';
import Login from './components/Login/loginScreen';
import ResetPassword from './components/Login/resetPassword';
import News from './components/News/newsScreen';
import Chat from './components/Chat/chatScreen';
import ProfileIndex from './components/Profile/profileIndex';
import SideBar from './components/sidebar/sidebar';
import NewsDetails from './components/News/newsScreenDetails';
import Privacy from './components/Privacy/privacyScreen';
import SignUpWorker from './components/SignUpWorker/signUpWorker';
import AuthLoadingScreen from './utils/authLoadingScreen';

const SignedInDrawer = createDrawerNavigator(
  {
    Home: { screen: Home },
    ProfileIndex: { screen: ProfileIndex },
    Chat: { screen: Chat },
    News: { screen: News },
    NewsDetails: { screen: NewsDetails },
    Privacy: { screen: Privacy },
  },
  {
    initialRouteName: 'Home',
    contentComponent: props => <SideBar {...props} />,
  }
);

const SignedOutDrawer = createDrawerNavigator(
  {
    Home: { screen: Home },
    Login: { screen: Login },
    ResetPassword: { screen: ResetPassword },
    SignUp: { screen: SignUp },
    News: { screen: News },
    NewsDetails: { screen: NewsDetails },
    Privacy: { screen: Privacy },
  },
  {
    initialRouteName: 'Home',
    contentComponent: props => <SideBar {...props} />,
  }
);

const SignedInStack = createNativeStackNavigator(
  {
    DrawerIn: { screen: SignedInDrawer },
  },
  {
    initialRouteName: 'DrawerIn',
    headerMode: 'none',
  }
);

const SignedOutStack = createNativeStackNavigator(
  {
    DrawerOut: { screen: SignedOutDrawer },
  },
  {
    initialRouteName: 'DrawerOut',
    headerMode: 'none',
  }
);

export default createSwitchNavigator(
    {
        AuthLoading: AuthLoadingScreen,
        SignedIn: SignedInStack,
        SignedOut: SignedOutStack,
    },
    {
        initialRouteName:'AuthLoading'
    }
);

./src/utils/authLoadingScreen.js

import { View, ActivityIndicator, StyleSheet, StatusBar, NetInfo } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import React from 'react';

export let globalUserToken;
export let globalNetworkOnline;

// this function is used to set the globalUserToken from other pages
export function setglobalUserToken(newValue: boolean) {
  globalUserToken = newValue;
}

export async function checkNetwork() {
  await NetInfo.getConnectionInfo().then(connectionInfo => {
    if (connectionInfo.type === 'none') {
      globalNetworkOnline = false;
    } else {
      globalNetworkOnline = true;
    }
  });
}

export default class AuthLoadingScreen extends React.Component {
  constructor() {
    super();
    this._bootstrapAsync();
  }

  // Fetch the token from storage then navigate to our appropriate place
  _bootstrapAsync = async () => {
    globalUserToken = await AsyncStorage.getItem('id_token');
    // This will switch to the App screen or Auth screen and this loading
    // screen will be unmounted and thrown away.
    this.props.navigation.navigate(globalUserToken ? 'SignedIn' : 'SignedOut');
  };

  render() {
    return (
        <View style={styles.container}>
          <ActivityIndicator />
          <StatusBar barStyle="default" />
        </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

问题很可能从 src/App.js 开始,因为我已经更新了 createNativeStackNavigator、createDrawerNavigator、createSwitchNavigator 的函数,这些函数以前只是来自“react-navigation”的 StackNavigator、DrawerNavigator、SwitchNavigator。

或者错误存在于 ./src/utils/authLoadingScreen.js 中,在那里检查是否存在登录令牌......也许这里也发生了一些变化?

最后,我把 package.json 留给你:

{
  "name": "MyApp",
  "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 .",
    "build:ios": "react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios",
    "build:android": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res"
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "^1.15.9",
    "@react-navigation/compat": "^5.3.20",
    "@react-navigation/drawer": "^6.1.8",
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/native-stack": "^6.2.4",
    "@types/react-native-dotenv": "^0.2.0",
    "native-base": "^3.2.1",
    "react": "17.0.2",
    "react-native": "0.66.0",
    "react-native-dotenv": "^3.2.0",
    "react-native-gesture-handler": "^1.10.3",
    "react-native-htmlview": "^0.16.0",
    "react-native-reanimated": "2.3.0-beta.2",
    "react-native-safe-area-context": "^3.3.2",
    "react-native-screens": "^3.8.0",
    "react-native-size-matters": "^0.4.0",
    "react-native-svg": "^12.1.1",
    "rn-fetch-blob": "^0.12.0",
    "styled-components": "^5.3.1",
    "styled-system": "^5.1.5",
    "tcomb-form-native": "^0.6.20"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/runtime": "^7.15.4",
    "@react-native-community/eslint-config": "^2.0.0",
    "babel-jest": "^27.2.5",
    "eslint": "^8.0.1",
    "jest": "^27.2.5",
    "metro-react-native-babel-preset": "^0.66.2",
    "react-test-renderer": "17.0.2"
  },
  "jest": {
    "preset": "react-native"
  }
}

有没有人遇到过这个问题,或者您知道我是否正确更改了新版本中不再使用的 react-navigation 组件?

4

0 回答 0