简体   繁体   中英

Should I put index.js in every component in react project?

Well, I've read https://medium.com/bootstart/you-should-be-using-folder-components-b30b7d165c39 article where author describes index.js as a cure and there is also comment in that article in which another person tells that it is redundant to make index.js in every component.

But! There are many repositories over github such

where they use index.js in every single component.

So, do we have to use them? Is it really necessary?

...it is redundant to make index.js in every component

Creating index.js helps in importing that component in different components/containers directly using:

import LayoutHeader from '../LayoutHeader';

instead of (without index.js):

import LayoutHeader from '../LayoutHeader/LayoutHeader';

Creating index.js in / for a single import element for an entire component feels redundant, but can be helpful when you want to have multiple elements in a single component (Not a very modular approach). Then you can use a single index file to export all the elements of the component.

Both practices are valid, and usually depends on programmer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM