Receiving error message in React Native I cannot figure out - reactjs

I am currently working on a project in React Native and upon trying to create the first set of screens I began receiving this Error...
"Error: 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."
I am not a beginner in React Native, but am also not an expert and this is my first time working on a RN build on my Macbook. I am not sure if there is a typo I have missed somewhere, if my path is not defined correctly, or if I am simply in need of another coffee - either way, I would love another set of eyes on it and any help is greatly appreciated. I will post the code of my Welcome Screen component and App.js below. Aside from the assets, these are the only files I am working in. I will also post a screenshot of my file structure. Thank you again!
import React from 'react';
import { View } from 'react-native';
import WelcomeScreen from './app/screens/WelcomeScreen';
export default function App() {
return (
<WelcomeScreen />
);
};
import React from 'react';
import { ImageBackgroundComponent, StyleSheet, View } from 'react-native';
function WelcomeScreen(props) {
return (
<ImageBackgroundComponent style={styles.background} source={require('../assets/background.jpg')}>
<View style={styles.loginButton}>
</View>
</ImageBackgroundComponent>
);
}
const styles = StyleSheet.create({
background: {
flex: 1,
},
loginButton: {
width: '100%',
height: 70,
}
})
export default WelcomeScreen;
File Structure

Try to use ImageBackground instead of ImageBackgroundComponent

Related

Error "Element type is invalid" React component

Many times I've seen this error, and honestly I never know how to fix it, or how i fixed it in the past ...
Uncaught Error: 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.
Here's my component implementation, I'm trying to understand what's wrong.
import React from 'react';
import { SvgFromUri } from 'react-native-svg';
import { Image } from 'react-native';
import { AppColors } from '../constants';
const imgSize = 55;
const CompanyIcon = ({ url }) => {
return (
<>
{
url && !url.includes('favicon')
? <SvgFromUri uri={url} width={imgSize} height={imgSize} onError={console.error} />
: <Image source={require('../assets/app_icon48.png')} style={
{
borderWidth: 1,
borderColor: AppColors.LIGHT_BLUE,
borderRadius: 4,
width: imgSize,
height: imgSize
}
} />
}
</>
);
}
export default CompanyIcon;
Thanks so much for any help community
The options I found research about it said something about exporting the component as default, so I did, but no success :(
Update:
Looks like it's a bug within the svg component :|
https://github.com/software-mansion/react-native-svg/issues/1742
check your exports, if you have a default export you should import like this :
import FunctionName from '../location'; // without {}
or maybe you
forgot to export your component from the file it's defined in, or you might have mixed up default and named imports

Element type is invalid . React Native

I know there are so many questions about this Error. And I have faced so many times with this error and answers over here helped me so Much to get Over that Error. But now I just wanted to use react-native-maps and in the documentation of react-native-maps they gave us this sample of code to run and see how things work. I copied this code and paste it into my App.js and ı got this problem. I tried to something to solve that but ı couldnt figure it out.
I get this error:
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 `ExpoRoot`
And here is My code :
import * as React from 'react';
import MapView from 'react-native-maps';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
export const App = () => {
return (
<View style={styles.container}>
<MapView style={styles.map} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
map: {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
},
});
As you said, this is a common error you can see while developing with React Native.
It occurs when the component name is wrong or the import path is entered incorrectly. To solve the problem, you need to check all the import statements and check if those are exported and imported correctly.
This usually happens when you export functions like the below.
export const HelloWorld = 'hello world';
Then import like below.
import HelloWorld from 'helloWorld';
I saw many people having similar mistakes. Hope you can check that out.

'Component exception' React Native

I copied my code from an old project of mine so it should be able to work. However, I think when I made the new project it accidentally updated the versions causing it to go thorough an error. This is my code:
import React, {useState} from 'react';
import { Text, View } from 'react-native';
import * as Font from 'expo-font';
import { AppLoading} from 'expo-app-loading';
import {enableScreens} from 'react-native-screens'
//import from libraries
import TabNavigation from './Navigation';
//import TabNavigation
enableScreens();
const fetchFonts = () => {
return Font.loadAsync({
'open-sans': require('./assets/fonts/OpenSans-Regular.ttf'),
'open-sans-bold' : require('./assets/fonts/OpenSans-Bold.ttf')
});
//fetch fonts from the folder so that we can style the title
};
export default function App() {
const[fontLoaded, setFontLoaded] = useState(false);
if(!fontLoaded){
return (
<AppLoading
startAsync ={fetchFonts}
onFinish={()=> setFontLoaded(true)}
/>
);
}
//this will ensure that the font will always be loaded.
return(
<TabNavigation />
);
}
The thing I'm trying to do is run this program and then return Navigation where all the navigation process works.
Here is the error:
Component exception
Element type is invalid: expected a string(for built-in components) or a class/function (for complete components) but got: undefined. You likely forgot to export your component from. the file it's defined in, or you might have mixed up a default and named imports. Check the render method of 'App'.
You don't need to do this fetchFonts method
to load the fonts. If you installed the fonts correctly you just need to put the attribute fontFamily in any style, with the name of the font as its value, for example: textStyle:{ fontFamily: 'Montserrat-Bold' }
For a better understanding you can check this link.
Change your import for app Loading
import AppLoading from 'expo-app-loading';
First, open the terminal and go to the project directory.
Second, run the following command to install the expo-app-loading module:
expo-app-loading
Third, import the module into your App.js file:
import AppLoading from 'expo-app-loading';
Tested on Expo 4.9.0

How to change default Loading Spinner for Ant Design Pro

I searched but can't seem to figure out how to change the default loading spinner that is generated with Ant Design Pro V4. I used the generator and ran npm create umi myApp. There is a default four circle spinner that I would like to replace with a customized spinner.
The default loader is located here:
#/myApp/config/config.ts
...
dynamicImport: {
loading: '#/components/PageLoading/index',
},
...
When I modified the PageLoading/index.tsx page based on Ant Design's customer spinner documentation. I kept getting this error.
Error: Element type is invalid: expected a string (for built-in components)
or a class/function (for composite components) but got: object.
Check the render method of `LoadableComponent`.
original PageLoading/index.tsx
import { PageLoading } from '#ant-design/pro-layout';
// loading components from code split
// https://umijs.org/plugin/umi-plugin-react.html#dynamicimport
export default PageLoading;
modified PageLoading/index.tsx
import { Spin } from 'antd';
import { LoadingOutlined } from '#ant-design/icons';
const antIcon = <LoadingOutlined style={{ fontSize: 24 }} spin />;
const CustomSpinner = <Spin indicator={antIcon} />
export default CustomSpinner;
What do I need to do to make it a LoadableComponent? Thanks!
Return value should be a component. Either function or class component.
This would work:
import react from 'react';
import { Spin } from 'antd';
import { LoadingOutlined } from '#ant-design/icons';
const antIcon = <LoadingOutlined style={{ fontSize: 24 }} spin />;
// Return value should be component
const CustomSpinner = () => <Spin indicator={antIcon} />
export default CustomSpinner;
A new way to doing this. You can define default spin element globally.
//top of app.tsx
import { Spin } from 'antd';
Spin.setDefaultIndicator(<div>Loading</div>);
ant design pro referance
https://ant.design/components/spin/#Static-Method

Ract FontAwesome ES6 usage

Good day
I am a novice React developer building a site for a client using ES6 standards. I have found a component for which the usage as description uses an older syntax, and I am having some trouble implementing my code. I am receiving an error message that I am not sue how to resolve.
The component in questions can be seen here:
https://www.npmjs.com/package/react-fontawesome
it instructs you to use the component as follows:
var React = require('react');
var FontAwesome = require('react-fontawesome');
React.render(<FontAwesome name='rocket' />, document.body);
To my understanding, this is an older way of writing React code. I have thus updated my code to for the ES6 standard.
I scratched in my package.json file to find out where to import the component from, so I am not sure if this is perhaps where I have gone wrong.
Below is a copy of my code using what I believe to be the correct implementation:
import React, { Component } from "react";
import FontAwesome from '#fortawesome/fontawesome-svg-core';
export class Footer extends Component {
constructor(props) {
super(props);
}
render = _ => {
return (
<div>
<FontAwesome name='rocket' />
</div>
);
}
}
When I import the component and run the code I get the following error:
React.createElement: 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 Footer.
in Footer (created by App)
in div (created by App)
If anyone would be kind enough to let me know where I might be going wring, I would be incredibly grateful.
There's a simple example on their docs page that should help out:
import ReactDOM from 'react-dom'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
const element = <FontAwesomeIcon icon={faCoffee} />
ReactDOM.render(element, document.body)

Resources