Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does React import the same package multiple times?

I have a short question:

In React we import the React Component for every component:

import React from 'react'

Does this mean if I have a screen that uses several subcomponents (which also import React each time), does the React package get imported several times?

So basically:

1 Screen with 4 subcomponents = 5 x React package loaded = 5 times the react memory used

or

1 Screen with 4 subcomponents= 1x React package loaded

Or does it only import the react package once and then access it when it's needed again?

like image 566
Fredyonge Avatar asked Oct 28 '25 02:10

Fredyonge


1 Answers

Or does it only import the react package once and then access it when it's needed again?

Basically yes. Your build tool / bundler (i.e. webpack) will take the packages you've imported throughout the app and include them in your build. It will recognize that these are the same package and import it only once.

like image 149
Seth Lutske Avatar answered Oct 29 '25 19:10

Seth Lutske