Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native Invariant Violation: Element type is invalid

Tags:

When I going to run my react native app on my iPhone Expo this error displayed in red background area.

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in.

this is the App.js inside the 'src/components/' folder

import React, { Component } from 'react';
import { View, Text } from 'react-native';


export default class App extends Component {
  render() {
    return (
    	<View>
      		<Text>Hello</Text>
      	</View>
    );
  }
}

This is the main App.js in react-native app folder.

import App from './src/components/App';

I used expo app for run this code. How can I solve this error?

like image 703
Thimeth Karanayaka Avatar asked Dec 15 '17 03:12

Thimeth Karanayaka


People also ask

What is invariant violation in react native?

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of 'App'.

How do you fix element type is invalid expected a string for built-in components or a class function for composite components but got undefined?

To solve the error "Element type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got", make sure to import named exports using curly braces and default exports without, and only use functions or classes as components.

How do you read react native errors?

How to handle errors in React Native. Error handling in vanilla JavaScript can be achieved using try, catch and finally statements. You can use these statements to handle caught exceptions in React Native components. React Native has its own mechanism to handle uncaught exceptions.


2 Answers

In my case instead of exporting like this:

export default App;

...I exported like following:

export {LoginForm};

It worked completely fine.

like image 28
Masood Avatar answered Oct 12 '22 22:10

Masood


Expo expects you to export a component from /App.js. But right now you are only importing into /App.js. Expo is not receiving any component to render. You need to export the component you imported like this:

export default App;

On side note: Use a class component only if you must.

like image 125
THpubs Avatar answered Oct 13 '22 00:10

THpubs