close

[Solved] Invariant Violation: “main” has not been registered

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Invariant Violation: “main” has not been registered in react-native. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How To Solve Invariant Violation: “main” has not been registered Error ?

  1. How To Solve Invariant Violation: “main” has not been registered Error?

    To Solve Invariant Violation: “main” has not been registered Error The problem is that if you are using expo then you have to registerComponent differently. All the information is in the docs.

  2. Invariant Violation: “main” has not been registered

    To Solve Invariant Violation: “main” has not been registered Error The problem is that if you are using expo then you have to registerComponent differently. All the information is in the docs.

Solution 1

The problem is that if you are using expo then you have to registerComponent differently. All the information is in the docs.

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

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

registerRootComponent(App);

Solution 2

This worked for me:

  1. delete node_modules and your lockfile (package-lock.json / yarn.lock)
  2. change the expo package version in package.json to 38.0.8
  3. run yarn or npm install
  4. run expo install react-native-safe-area-context

Solution 3

Open the index.js, the content of the file should be like this:

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

If you have this error Invariant Violation: “main” has not been registered you have to replace the 'X' by 'main'.

Another example :

If you have this error Invariant Violation: “app” has not been registered you have to replace the 'X' by 'app'.

For android :

Open ./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";
  }

The 'X' of MainActivity.java and index.js must match.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment