Better way to import components in large react/react native projects

Image Credits: Medium/Rooney Reeves

A Component is the basic building block of React. They are independent reusable bits of code and comes in two types - Class and Functional components.

Ok, now let’s get straight to the point and discuss how to structure your project when you have a large number of components when you are working on a large project.

Generally when you are using components in a particular screen or file, we import it like so:

which will look cluttered when the codebase grows.

For better readability, we can create an index.js file in our root component directory and export all the components as named export like below:

Now, we can write our screens file like below:

And with this approach, in a single line you can import multiple components if you want to access them, instead of having multiple import statements.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Aravind Madhusudhanan Nair
Aravind Madhusudhanan Nair

Written by Aravind Madhusudhanan Nair

Writes about JS, React and React-Native development and other technology stuff.

No responses yet

Write a response