简体   繁体   中英

React project automatically export components by filename

I'm working on a fairly large project using react/webpack with a lot of components. Currently my folder structure looks like this:

bin/
media/
src/
  components/
    login/
      Login.js
      LoginContainer.js
    registration/
    index.js
    ..
  framework/
  redux/
  ..
static/
test/

And in src/components/index.js I do the exports: export Login from ./Login/Login so that in other components I can just do import {Login, LoginContainer} from 'components' without having to search the file location.

Is there a way to not have the exports in the index.js like now? I'd like to export automatically all javascript files by their name, without having to type it every time.

You can add (all of) your Components-SubFolders to your webpack.config.resolve and just import the Filename. Or just add the components root to resolve.modulesDirectories and import the Path relative to the components-folder.

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