不变违规:“主要”尚未注册

Pan*_*rma 23 javascript react-native react-navigation

React Native 新手:我使用 Expo init 启动了一个全新的项目,然后我按照 https://reactnavigation.org/docs/hello-react-navigation 中提到的说明操作我使用 expo start 运行该项目,但出现此错误。不变违规:“主要”尚未注册。在以下情况下可能会发生这种情况:

  • Metro(本地开发服务器)从错误的文件夹运行。检查 Metro 是否正在运行,将其停止并在当前项目中重新启动。
  • 模块因错误而AppRegistry.registerComponent无法加载且未被调用。

不变 browser.js:38:14 runApplication AppRegistry.js:193:13 __callFunction MessageQueue.js:425:19 __guard$argument_0 MessageQueue.js:112:6 __guard MessageQueue.js:373:10 callFunctionReturnFlushedQueue MessageQueue.js:111:4 callFunctionReturnFlushedQueue [native code]:0 有什么建议吗?

Kis*_*ore 16

索引.js

AppRegistry.registerComponent("X", () => Root);
Run Code Online (Sandbox Code Playgroud)

如果您遇到此问题,Android请确保 和mainactivity.java包含index.js相同的“X”

mainActivity.java

@Override
protected String getMainComponentName() {
   return "X";
}
Run Code Online (Sandbox Code Playgroud)

如果您遇到此问题,iOS请确保 和AppDelegate.m包含index.js相同的“X”

AppDelegate.m

 RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"X" initialProperties:nil];
Run Code Online (Sandbox Code Playgroud)


aru*_*run 12

就我而言,当我打电话时问题就解决了

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

在index.js中。所以看起来第一个参数是根据显示的错误消息来传递的。就我而言,我的项目名称是 AwesomeProject,Metro 报告的错误是:

Invariant Violation: "awesomeproject" has not been registered.

所以我认为添加 toLowerCase() 可以解决问题,而且确实如此!

但是,当我尝试在 macos 上运行它时遇到了这个问题,npx react-native run-macos解决后,我尝试调用 iOS 版本,然后我得到了Invariant Violation: "AwesomeProject" has not been registered.

所以最后我通过注册应用程序两次来使两者都工作,如下所示:

AppRegistry.registerComponent(appName, () => App);
AppRegistry.registerComponent(appName.toLowerCase(), () => App);
Run Code Online (Sandbox Code Playgroud)

这同样适用于npx react-native run-android.


小智 10

解决方法:

AppRegistry.registerComponent('main', () => App);
Run Code Online (Sandbox Code Playgroud)

  • 哇什么?这做到了。您能详细解释一下为什么会这样吗? (3认同)

jus*_*din 9

打开index.js,文件内容应该是这样的:

import { AppRegistry, Platform } from 'react-native';
import App from './App';

AppRegistry.registerComponent('X', () => App);

if (Platform.OS === 'web') {
    const rootTag = document.getElementById('root') || document.getElementById('X');
    AppRegistry.runApplication('X', { rootTag });
}
Run Code Online (Sandbox Code Playgroud)

如果出现此错误Invariant Violation: “main” has not been registered,则必须替换'X'by 'main'

另一个例子 :

如果出现此错误Invariant Violation: “app” has not been registered,则必须替换'X'by 'app'

对于安卓:

打开 ./android/app/src/main/java/[multiple folders]/MainActivity.java

/**
   * Returns the name of the main component registered from JavaScript.
   * This is used to schedule rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "X";
  }
Run Code Online (Sandbox Code Playgroud)

'X'MainActivity.javaindex.js必须匹配。


小智 8

问题是,如果您使用的是 expo,那么您必须以不同的方式注册组件。所有信息都在文档中。https://docs.expo.io/versions/latest/sdk/register-root-component/

import { registerRootComponent } from 'expo';
import React from 'react';
import { View } from 'react-native';

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

registerRootComponent(App);

Run Code Online (Sandbox Code Playgroud)


小智 8

这对我有用:

  1. 删除 node_modules 和你的锁文件(package-lock.json / yarn.lock)
  2. 将 package.json 中的 expo 包版本更改为 38.0.8
  3. 运行 yarn 或 npm install
  4. 运行 expo install react-native-safe-area-context

  • 删除所有的node_modules文件夹,然后重新加载,然后停止输入。 (2认同)

Fai*_*taq 7

就我而言,我缺少一些必须安装的软件包。

npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
Run Code Online (Sandbox Code Playgroud)