首页 > 解决方案 > 不变违规:“main”尚未注册。在以下情况下可能会发生这种情况: * Metro(本地开发服务器)从错误的文件夹运行

问题描述

我已将我的 index.js 更改为此,因为我试图遵循这个 turorial:

https://www.youtube.com/watch?v=6soHPnHxHxI


        import { registerRootComponent } from 'expo';
        import React from "react";
        import { AppRegistry } from "react-native";
        import App from "./App";
        import { name as appName } from "./app.json";
        import { Provider } from "react-redux";
        import { Provider as PaperProvider } from "react-native-paper"
        import store from "./store";
        import 'react-native-gesture-handler';

        const AppRedux = () => {
            <Provider {...{store}}>
                <PaperProvider>
                    <App></App>
                </PaperProvider>
            </Provider>
        }

        // registerRootComponent calls AppRegistry.registerComponent('main', () => App);
        // It also ensures that whether you load the app in the Expo client or in a native build,
        // the environment is set up appropriately
        //AppRegistry.registerComponent(appName, () => AppRedux);


        // registerRootComponent calls AppRegistry.registerComponent('main', () => App);
        // It also ensures that whether you load the app in the Expo client or in a native build,
        // the environment is set up appropriately
        registerRootComponent(AppRedux);

但我不断收到此错误:

Invariant Violation: "main" has not been registered. This can happen if: * Metro (the local dev server) is run from the wrong folder.

标签: node.jsreact-nativereduxreact-redux

解决方案


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

如果您有此错误 Invariant Violation: “main” has not been registered,您必须将“X”替换为“main”。

另一个例子 :

如果您遇到此错误 Invariant Violation: “app” has not been registered,您必须将“X”替换为“app”。


推荐阅读