Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kerelka's avatar
Level 15

Import on Component or Import on App.js/ts

I see on starting kit laravel, component like Head, Link are imported on the Component. I know that i can import on app.js/ts to use across all my component without using import again.

which one is better? in term of performance/clean code/readability

0 likes
3 replies
LaryAI's avatar
Level 58

When deciding whether to import components like Head and Link directly within each component or globally in app.js/app.ts, there are several factors to consider: performance, clean code, and readability.

Performance

From a performance standpoint, there is generally no significant difference between importing components globally or locally. Modern JavaScript bundlers like Webpack or Vite are quite efficient at handling imports, and they will only include the necessary code in the final bundle. However, importing components globally can slightly increase the initial load time if the components are not used on every page.

Clean Code and Readability

  1. Local Imports (Per Component):

    • Pros:
      • Explicitness: It's clear which components are being used in each file, making it easier to understand dependencies.
      • Isolation: Each component is self-contained, which can make it easier to refactor or remove components without worrying about global dependencies.
    • Cons:
      • Repetition: You have to import the same components in multiple files, which can be repetitive.
  2. Global Imports (In app.js/app.ts):

    • Pros:
      • Convenience: You only need to import components once, and they are available throughout your application.
      • Less Boilerplate: Reduces the need to repeatedly import the same components in multiple files.
    • Cons:
      • Implicit Dependencies: It can be less clear which components are being used in a specific file, potentially making the code harder to understand.
      • Potential for Unused Imports: You might end up with components loaded globally that are not used everywhere, which can lead to unnecessary code in your bundle.

Recommendation

For most projects, especially larger ones, it's generally better to import components locally within each component file. This approach promotes better modularity and makes the codebase easier to maintain and understand. However, for utility components that are used in almost every component, such as a Button or Input, you might consider global imports to reduce boilerplate.

Here's an example of how you might structure your imports:

Local Import Example

// In MyComponent.js
import React from 'react';
import { Head, Link } from 'some-library';

const MyComponent = () => {
  return (
    <div>
      <Head title="My Page" />
      <Link to="/home">Home</Link>
    </div>
  );
};

export default MyComponent;

Global Import Example

// In app.js
import React from 'react';
import { Head, Link } from 'some-library';

const App = () => {
  return (
    <div>
      <MyComponent />
    </div>
  );
};

export default App;

// In MyComponent.js
import React from 'react';

const MyComponent = () => {
  return (
    <div>
      <Head title="My Page" />
      <Link to="/home">Home</Link>
    </div>
  );
};

export default MyComponent;

Ultimately, the choice depends on your project's specific needs and your team's preferences.

RemiM's avatar

First, if you see that the starter kit comes with a specific architecture, you should take that as a blueprint for your development strategy.

Going further on the topic, in most cases, it's best to import components only where they need to be, rather than globally at the root level. The root level should typically contain only essential elements like a state provider (if you are using a frontend framework), a layout, and other utilities that all pages require. For everything else, import components only in the places where they are actually used.

1 like

Please or to participate in this conversation.