Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

javascript - Would like to confirm understanding of default behaviour of importing index.js with create-react-app

Trying to understand while using create react app why if a folder contains an index.js and you want to import it, then you only specify the folder name.

I was curious to know where this is configured to automatically know to look for index.js by default?

This question was previously linked to another post but that post wasn't clear enough for me to understand. From what I gather this is my understanding and I'd like to confirm if it's correct.

My understanding is that webpack which I know runs under the hood for create-react-app is configured to by default import index.js - is this correct? I guess the configuration for this is hidden away when using create-react-app.

Does this just apply to projects built with webpack then?

question from:https://stackoverflow.com/questions/65831947/would-like-to-confirm-understanding-of-default-behaviour-of-importing-index-js-w

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The configuration works like this: In the create-react-app build all code files should be under the /source directory. All static files should be under the /public directory, and they can be referenced from base url(for example: if I have a file /public/my-image.png, I can use it in my JSX with <img src = '/my-image.png.

To answer your question, this is not just with create-react-app, but with any JavaScript project. If a folder has a index.js file, you don't have to specify ./dir/index.js and instead can do ./dir, which looks for a file called index.js.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...