~/reactjs/reactjs-3-ways-import-components.md

ReactJS - 3 ways to Import components in ReactJS

2 min readJune 25, 2020by Gorav Singal#reactjs
// TL;DR

Import React components using default export with any name, named exports with curly braces, or directory index files to organize and include components in your project.

ReactJS - 3 ways to Import components in ReactJS

Introduction

In this post, we will discuss 3 different ways to import a component in ReactJS. This post is not about concept of components in general. But, this post is to show how we can include or import different components.

Example folder structure:

/src
  App.js
  App.css
  ...
  /components
    /Movie
    /MovieList
    /Footer

In this post, We are concerned with three components:

  • Movie
  • MovieList
  • Footer

We will see 3 different ways to import this three components.

Way-1 By exact name of JS file

This is the simplest of the ways and you probably knew about this. The folder structure. We will see component: Movie

/src
  App.js
  App.css
  ...
  /components
    /Movie
      /Movie.js
    /MovieList
    /Footer

Assumming you are importing Movie component in App.js.

import React, {Component} from 'react';
import './App.scss';
import Movie from './components/Movie/Movie';

class App extends Component {
    render() {
        return <React.Fragment>
            <Movie />
        </React.Fragment>
    }
}

// module.exports = App;
export default App;

Here, we are just refering Movie component to its exact filename:

Way-2 By naming our component as index.js

In your respective component folder, you need to name your js file as index.js The folder structure. We will see component: MovieList

/src
  App.js
  App.css
  ...
  /components
    /Movie
    /MovieList
      /index.js
    /Footer

Assumming you are importing MovieList component in App.js.

import React, {Component} from 'react';
import './App.scss';
import MovieList from './components/MovieList';

class App extends Component {
    render() {
        return <React.Fragment>
            <MovieList />
        </React.Fragment>
    }
}

// module.exports = App;
export default App;

Note that we did not refer full path till the component js file. Rather we have refered folder path. And index.js is implicitly included.

Way-3 By specifying your component filename in special package.json

Name your component the name you want. Say: Footer.js Create a special file under component’s respective folder: package.json with following content:

package.json

{
    "main": "Footer.js"
}

The folder structure. We will see component: Footer

/src
  App.js
  App.css
  ...
  /components
    /Movie
    /MovieList
    /Footer
      /paackage.json
      /Footer.js

Assumming you are importing Footer component in App.js.

import React, {Component} from 'react';
import './App.scss';
import Footer from './components/Footer';

class App extends Component {
    render() {
        return <React.Fragment>
            <Footer />
        </React.Fragment>
    }
}

// module.exports = App;
export default App;

Here, we are just refering Footer component upto its folder name. And, presence of package.json file make ReactJS system understand which js file to include from this folder.

This ends our three ways to include components. I hope you learnt something new.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS setState is Asynchronous setState() method in ReactJS class components…

ReactJS - How to restrict data type for different kind of data

ReactJS - How to restrict data type for different kind of data

Introduction Javascript is not a strict type language. We can use a variable to…

ReactJS - How to create ReactJS components

ReactJS - How to create ReactJS components

Introduction In this post, we will talk about basic of how to create components…

ReactJS - Basic Form Handling and Form submission

ReactJS - Basic Form Handling and Form submission

Introduction Lets take a look at how forms are being handled in ReactJS. We will…

ReactJS - How to pass method to component and how to pass arguments to method

ReactJS - How to pass method to component and how to pass arguments to method

Introduction In the ReactJS project, you are having aa parent component and a…

ReactJS - How to use conditionals in render JSX

ReactJS - How to use conditionals in render JSX

Introduction In this post, I will show several ways to use conditionals while…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…