How do index.js files work in React component directories?

前端 未结 3 898
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 08:14

I just started a new React project and decided to use this pattern, which basically groups files according to their respective component:

├── actions
│   ├──         


        
3条回答
  •  执念已碎
    2020-12-13 08:43

    You can also leverage it for module namespaces, e.g.

    //MyNamespace/index.js
    
    import Mod1 from './Mod1' //assumes ./Mod1.js
    import Mod2 from './Mod2' //assumes ./Mod2.js
    
    export{
      Mod1,
      Mod2
    }
    

    Then in other files you can import with a namespace:

    //SomeOtherFile.js
    
    import * as NamespaceToUse from './MyNamespace'
    
    // Then access as:
    NamespaceToUse.Mod1
    NamespaceToUse.Mod2
    

提交回复
热议问题