ReactJS – Component import instructions like “PRO”

Tram Ho

Hello friends. Today I will guide you to import components (in fact not just components but also other file types) in React (really not just react, others too) like “people who know the code”: v Let’s start

Certainly the front end SPA code (Single Page App) like React, Vue or Angular all have a place to “store” the components to use for the whole project such as shared-components, or common, or UI, … And when we import the components this directory will look like this

And when more components need to be imported, it looks like this or twice as long

Hmm. It looks too amateur, so there is a way

Then the solution is in the directory that needs to import multiple or together (common directory, or shared-components is an example) create an index.js file (or .jsx .ts .tsx) and import all the components into as follows

And when we need to import multiple components from elsewhere, just call one line like this oke

It looks too professional: v if you want to consult more about importing / exporting files in javascript, you can access to the doc: https://developer.mozilla.org/en-US/docs/web/javascript/reference / statements / export

Share the news now

Source : Viblo