Skip to content

Instantly share code, notes, and snippets.

@zachfedor
Created March 9, 2017 16:09
Show Gist options
  • Save zachfedor/2a3f0b648d438e3ed3e314457398171f to your computer and use it in GitHub Desktop.
Save zachfedor/2a3f0b648d438e3ed3e314457398171f to your computer and use it in GitHub Desktop.
react component import convention
// ./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
*/
// 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
*/
@brisayswhaat
Copy link

the second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment