Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to import file in other directory ReactJS

Understanding File Import in ReactJS

Importing files in ReactJS is a significant aspect of developing applications. It allows you to divide your application into manageable chunks of codes known as "components".

Consider components as individual Lego pieces. Each Lego piece can exist on its own but can also connect to other pieces to create a complete structure. Similarly, in a ReactJS application, each component is a Lego piece. We can use these components to build a full-fledged web application.

What Does it Mean to Import a File?

In simple terms, importing a file in ReactJS means bringing in a piece of code from another file. This could be a function, a class, a React component, or even just an object or an array.

Imagine you're baking a cake. You have ingredients like flour, sugar, and eggs in different containers. Each of these containers can be seen as a different file. To bake a cake (or to build your application), you need to bring all these ingredients together. That's what importing does in ReactJS.

Why Should We Import Files?

The main reason to import files is to make our code more readable and maintainable. By dividing our code into separate files or modules, we make it easier to understand and debug.

Also, it helps in reusability. If you have a function, a class, or a component that needs to be used in several places, it makes sense to write it once in a file and then import it wherever needed.

How to Import a File in ReactJS?

Let's take an example. Suppose you have a React component named 'Header' in a file named 'Header.js'. Now, you want to use this 'Header' component in your 'App.js' file. To do that, you'll need to import the 'Header' component from 'Header.js' into 'App.js'.

The syntax for importing a file in ReactJS is:

import { ComponentName } from './ComponentFile';

In our example, to import the 'Header' component from 'Header.js' into 'App.js', you'll write:

import { Header } from './Header';

Importing a File From Another Directory

The example above assumes that 'Header.js' and 'App.js' are in the same directory. But what if they're not? What if 'Header.js' is in a different directory?

Well, in that case, we'll need to specify the relative path of 'Header.js' from 'App.js' in the import statement. Suppose 'Header.js' is inside a directory named 'components' which is in the same directory as 'App.js'. Then, you'll import 'Header' like this:

import { Header } from './components/Header';

Default Exports and Imports

There's another way of exporting and importing modules in ReactJS known as default exports and imports. A default export is just a part of a file that is exported by default when the file is imported.

Let's say 'Header.js' has a default export - the 'Header' component. Here's how you'll write it:

export default Header;

And here's how you'll import it in 'App.js':

import Header from './components/Header';

Notice the difference? With default exports, you don't need the curly braces around the component name while importing.

Now, suppose 'Header.js' is not in the 'components' directory but in a directory named 'layout' inside 'components'. Then the import statement in 'App.js' will look like this:

import Header from './components/layout/Header';

Wrapping Up - The Art of Component Assembly

By now, you should be comfortable with the concept of file import in ReactJS. It's like assembling different Lego pieces to make a beautiful structure. Each component, or 'piece', can exist independently but when assembled together through importing, they form a complete web application.

Remember, a well-structured application is not just about writing code. It's about organizing it. It's about knowing where each piece goes. By understanding how to import files in ReactJS, you're mastering the art of component assembly. Happy coding!