10

I am trying to import 2 components from the components folder. Both components' class are export default .

However, I got an error message that I should use the curly braces in my import statement. However, both components above are not using named export so curly braces is not needed when importing them.

Why isn't it working? How can I make this work?

Line 4 error Main app

Login component

Chong Onn Keat
  • 520
  • 2
  • 8
  • 19

5 Answers5

18

as @casimirth stated above since those components are from different files, even though on the same folder, then you need to import them separately as below

import Login from "./components/Login"
import Question from "./components/Question"

But i think i know what you're looking for, being able to import them all on one line right?

below are couple of the ways

1 .put them all on single file and use exports, since in one file only one component can use export default

// ./components/componentfile.js
export const Login = () => {...
export const Question = () => {...

then where you're using them you can import them as

import {Login, Question} from '.components/componentfile'

if one of the file is exported with default as below

const Login = () => {...
export const Question = () => {...
export default Login;

then the using them will be as below

import Login , { Question } from './components/componentfile'

2. You wish to keep this two files separately and still import on one line

then you need to add another file in components file, prefered index.js since if you name a directory without specifying a file, index.js is one being called by default

So, you components directory will have three files,

./components
   -index.js
   -login.js
   -questions.js

then without editing anything on your login.js,questions.js import them on your index.js and export them from it as below

import Login from './login'
import Question from './question'

export {Login, Question}

then where you're using them you just import them as below

import {Login, Question} from './components' //note with index.js no need to mention //it on import
ndotie
  • 1,830
  • 17
  • 18
2

You can create an index.js file in the components folder that imports and exports all the components, then you will be able to import the components at the same line.

This question and answer might help you, and the special part is it can performs both import and export using only one single line of code.

Chong Onn Keat
  • 520
  • 2
  • 8
  • 19
1

Try importing them individually:

import Login from "./components/Login"
import Question from "./components/Question"
casimirth
  • 11
  • 2
0

To import components in a single

  1. create a index.js file inside components folder

  2. inside index.js file export all the components

    export { default as Username } from './Questions'; export { default as PageNotFound } from './Login';

Krishnadev. V
  • 345
  • 2
  • 8
  • 23
0

I prefer like this approach for routes too. It's looks neat and clean.

I'm using following versions mentioned for better clarity

react : ^18.2.0

react-router-dom : ^6.4.0

// Pages.js
import Home from './Home'
import Contact from './Contact'
import Login from './Login'
import SignUp from './Signup';

export {
    Home,
    Contact,
    Login,
    SignUp,
}
//App.js
import React from 'react'
import { BrowserRouter as Router, Routes, Route } from "react-router-dom"
import { Home, Contact, Login, SignUp } from '/pages'; //  Looks clean


function App() {  
  return (
    <div className="App">
        <Router>
          <Routes>
            <Route path='/' exact element={<Home />} />
            <Route path='/login' exact element={<Login />} />
            <Route path='/signup' exact element={<SignUp />} />
          </Routes>
        </Router>
    </div>
  );
}

export default App;
GMKHussain
  • 3,342
  • 1
  • 21
  • 19