Here is my index.jsx file:
import Navbar from './Navbar/Navbar';
import Greeting from './Greeting/Greeting'
import Project from './Project/Project';
import About from './About/About';
import Contact from './Contact/Contact';
import Footer from './Footer/Footer';
export default {
Navbar,
Greeting,
Project,
About,
Contact,
Footer,
};
And my App.jsx file:
import React from 'react';
import {Navbar} from './components';
// import Navbar from './components/Navbar/Navbar';
const App = () => {
return (
<div>
<Navbar />
</div>
)
}
export default App;
When I call to import Navbar through the index.jsx file, I got this error:
Uncaught SyntaxError: The requested module */sre/components/index. isx?
App.isx:4
t=1672200538218' does not provide an export named "Navbar'
What wrong I got if I call Navbar through index file? Thank you so much!
How can I call to import many components througnt index file.
Instead of importing them and then exporting them, you could directly export them as below. But for the above solution to work you should use named exports, like export function Navbar() { instead of
export default function Navbar() {.
export { Navbar } from "./Navbar/Navbar";
export { Greeting } from "./Greeting/Greeting";
export { Project } from "./Project/Project";
export { About } from "./About/About";
export { Contact } from "./Contact/Contact";
export { Footer } from "./Footer/Footer";
But if you would rather keep your default export, you should be doing as below instead of what you have (which I wouldn't prefer, as anyway a named export is way better):
export { defautl as Navbar } from "./Navbar/Navbar";
export { defautl as Greeting } from "./Greeting/Greeting";
export { defautl as Project } from "./Project/Project";
export { defautl as About } from "./About/About";
export { defautl as Contact } from "./Contact/Contact";
export { defautl as Footer } from "./Footer/Footer";
You can learn more about export on mdn.
Related
Export and import of App seem alright. The error must be due to some mixing of names. This is my first react application. I have scoured various StackOverflow posts with the same error but I couldn't extract helpful insight from them. So, please be a bit elaborate.
Error:
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, or you might have mixed up default and named imports.
Check the render method of App.
Index.js :
import App from './App';
ReactDOM.render(<App /> , document.getElementById('root'));
App.jsx :
import React from 'react';
import { StreamChat } from 'stream-chat';
import { Chat } from 'stream-chat-react';
import Cookies from 'universal-cookie';
import { ChannelListContainer, ChannelContainer } from './components';
const App = () => {
return (
<div className="app__wrapper">
<Chat client={client} theme="team light">
<ChannelListContainer
/>
<ChannelContainer
/>
</Chat>
</div>
);
}
export default App;
index.js in ./components:
export { default as ChannelContainer } from './ChannelContainer';
export { default as ChannelListContainer } from './ChannelListContainer';
Because of you're using:
export { default as ChannelContainer } from "./ChannelContainer";
export { default as ChannelListContainer } from "./ChannelListContainer";
You need to export ChannelContainer from ./ChannelContainer like:
const ChannelListContainer = () => {
return <>ChannelListContainer</>;
};
export default ChannelListContainer;
And export ChannelListContainer from ./ChannelListContainer like:
const ChannelContainer = () => {
return <>ChannelContainer</>;
};
export default ChannelContainer;
Here's the sample based on your structure:
I’m trying to open an upload document dialogue when the user taps a button. I import the function to do this from another file (“upload-recording-button.js”) and try to use it in a button. However, I’ve gotten a number of errors such as “_uploadRecordingButton.uploadRecording is undefined.”
Based on other related Stack Overflow pages, I tried changing the button’s OnPress to this.uploadRecording, uploadRecording(this), this.uploadRecording(), and probably a few other variations. They all fail with variations of “function doesn’t exist.”
Here is part of the file with the function I’m calling:
upload-recording-button.js
import React, {Component} from 'react';
import { DocumentPicker } from 'expo';
async function uploadRecording() {
let result = await DocumentPicker.getDocumentAsync({type: '*/*'});
if (!result.cancelled) {
//upload function(result.uri);
}
}
export default uploadRecording;
Main file
import { uploadRecording } from '../components/upload-recording-button';
export default class HomeScreen extends React.Component {
static navigationOptions = {
//header: null,
title: 'Recents',
// uploadRecording(this)
headerRight: (
<Button
onPress={uploadRecording()}
title="Add"
color="#007AFF"
/>
),
};
You need to export function from upload-recording-button.js
import React, {Component} from 'react';
import { DocumentPicker } from 'expo';
export async function uploadRecording() {
let result = await DocumentPicker.getDocumentAsync({type: '*/*'});
if (!result.cancelled) {
//upload function(result.uri);
}
}
If there's only one function you're exporting from upload-recording-button.js you can use default export
export default function-name
and import it in the file where you want to use
import anyName from file-fath
I can't get this react class to export and I can't figure out why. I have the export class at the bottom and everything is extending what it should. Is it my withRouter method?
import React, {Component} from 'react';
import {HeaderAdmin} from '../headerAdmin';
import {DashBoxes} from './dashBoxes';
import {MetaData} from '../metaData';
import {withRouter} from 'react-router-dom';
class CoachDashMain extends Component {
render() {
return(
<div>
<HeaderAdmin />
<DashBoxes />
<MetaData />
</div>
);
}
}
export default withRouter(CoachDashMain);
the error I'm getting is ./components/Coach/coachDashMain' does not contain an export named 'CoachDashMain'.
the import in another file looks like:
import {CoachDashMain} from './components/Coach/coachDashMain'
You are using named imports: import { CoachDashMain } from '...', which gives the above error unless you have export class CoachDashMain ... in that file.
Since you are using export default ..., you should import it by:
import CoachDashMain from '...'; // `CoachDashMain` can be renamed to anything
When you default export the class, you don't need the curly braces while importing that class in another file.
So, you should import like:
import CoachDashMain from './components/Coach/coachDashMain';
"Element type is invalid" I still keep getting this error and don't know what to do. Tried <form> and <Form> still same. Please help:
This is my AzForm.js saved at: "./components/testComponent"
import React from 'react'
import { Provider } from 'react-redux'
import { Container, Content, Text, Form } from 'native-base';
//import { createStore } from 'redux' //NOT sure whether I need it, but I still get the same error
import { reduxForm } from 'redux-form'
const SignInForm = props => {
return (
<Form>
</Form>
);
};
export default reduxForm({
form: 'simple' // a unique identifier for this form
})(SignInForm)
and this is an extract from index.js
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Image } from 'react-native';
import { SignInForm } from "./components/AzForm";
export class HomeScreen extends Component {
render() {
return (
<SignInForm />
);
}
}
Change
export class HomeScreen
to
export default class HomeScreen`
When you export without the default, the component that is exported becomes what is known as a named export (you can have many of these per file). You would import these as import {HomeScreen} from 'HomeScreen'
The React convention is to one component per file. This is achieved by using the export default option as provided above. You would then import it using import HomeScreen from 'HomeScreen';
This component does work:
export class Template extends React.Component {
render() {
return (
<div> component </div>
);
}
};
export default Template;
If i remove last row, it doesn't work.
Uncaught TypeError: Cannot read property 'toUpperCase' of undefined
I guess, I don't understand something in es6 syntax. Isn't it have to export without sign "default"?
Exporting without default means it's a "named export". You can have multiple named exports in a single file. So if you do this,
class Template {}
class AnotherTemplate {}
export { Template, AnotherTemplate }
then you have to import these exports using their exact names. So to use these components in another file you'd have to do,
import {Template, AnotherTemplate} from './components/templates'
Alternatively if you export as the default export like this,
export default class Template {}
Then in another file you import the default export without using the {}, like this,
import Template from './components/templates'
There can only be one default export per file. In React it's a convention to export one component from a file, and to export it is as the default export.
You're free to rename the default export as you import it,
import TheTemplate from './components/templates'
And you can import default and named exports at the same time,
import Template,{AnotherTemplate} from './components/templates'
Add { } while importing and exporting:
export { ... }; |
import { ... } from './Template';
export → import { ... } from './Template'
export default → import ... from './Template'
Here is a working example:
// ExportExample.js
import React from "react";
function DefaultExport() {
return "This is the default export";
}
function Export1() {
return "Export without default 1";
}
function Export2() {
return "Export without default 2";
}
export default DefaultExport;
export { Export1, Export2 };
// App.js
import React from "react";
import DefaultExport, { Export1, Export2 } from "./ExportExample";
export default function App() {
return (
<>
<strong>
<DefaultExport />
</strong>
<br />
<Export1 />
<br />
<Export2 />
</>
);
}
⚡️Working sandbox to play around: https://codesandbox.io/s/export-import-example-react-jl839?fontsize=14&hidenavigation=1&theme=dark
// imports
// ex. importing a single named export
import { MyComponent } from "./MyComponent";
// ex. importing multiple named exports
import { MyComponent, MyComponent2 } from "./MyComponent";
// ex. giving a named import a different name by using "as":
import { MyComponent2 as MyNewComponent } from "./MyComponent";
// exports from ./MyComponent.js file
export const MyComponent = () => {}
export const MyComponent2 = () => {}
import * as MainComponents from "./MyComponent";
// use MainComponents.MyComponent and MainComponents.MyComponent2
//here
EXPORTING OBJECT:
class EmployeeService { }
export default new EmployeeService()
import EmployeeService from "../services/EmployeeService"; // default import
EXPORTING ARRAY
export const arrExport = [
['first', 'First'],
['second', 'Second'],
['third', 'Third'],
]
import {arrExport} from './Message' //named import
// if not react and javascript app then mention .js extension in the import statement.
You can export only one default component and in import can change the name without aliasing it(using as).