Does React import the same package multiple times?

Viewed 612

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?

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.

Related