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?
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'.
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 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.
In my case instead of exporting like this:
export default App;
...I exported like following:
export {LoginForm};
It worked completely fine.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With