Created
March 9, 2017 16:09
-
-
Save zachfedor/2a3f0b648d438e3ed3e314457398171f to your computer and use it in GitHub Desktop.
react component import convention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./App.js | |
import React from 'react'; | |
import ComponentOne from './componentGroup/ComponentOne'; | |
import ComponentTwo from './componentGroup/ComponentTwo'; | |
class App extends React.Component { | |
render() { | |
return ( | |
<div> | |
<ComponentOne /> | |
<ComponentTwo /> | |
</div> | |
); | |
} | |
}; | |
export default App; | |
// ./componentGroup/ComponentOne.js | |
const ComponentOne = () => { | |
// ... | |
}; | |
export default ComponentOne; | |
// ./componentGroup/ComponentTwo.js | |
const ComponentTwo = () => { | |
// ... | |
}; | |
export default ComponentTwo; | |
/* | |
Pros: | |
- minimal files | |
- one class or constant using one default export per file | |
- allows extra named exports if needed | |
Cons: | |
- if file structure changes, requires mass refactor for every `import` statement | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// App.js | |
import React from 'react'; | |
import { | |
ComponentOne, | |
ComponentTwo | |
} from './componentGroup'; | |
class App extends React.Component { | |
render() { | |
return ( | |
<div> | |
<ComponentOne /> | |
<ComponentTwo /> | |
</div> | |
); | |
} | |
}; | |
export default App; | |
// ./componentGroup/ComponentOne.js | |
const ComponentOne = () => { | |
// ... | |
}; | |
export default ComponentOne; | |
// ./componentGroup/ComponentTwo.js | |
const ComponentTwo = () => { | |
// ... | |
}; | |
export default ComponentTwo; | |
// ./componentGroup/index.js | |
export { default as ComponentOne } from './ComponentOne'; | |
export { default as ComponentTwo } from './ComponentTwo'; | |
/* | |
Pros: | |
- easier to handle grouped components | |
- easier to handle refactor after moving components | |
Cons: | |
- yet another file to keep track of | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the second.