React Import & Export Component Pattern
Default export
const MyComponent = () => <div>Hey, MyComponent</div>;
export default MyComponent;
import MyComponent from './MyComponent';
Export
export const MyComponent = () => <div>Hey, MyComponent</div>;
import { MyComponent } from './MyComponent';
Rename import
export const MyComponent = () => <div>Hey, MyComponent</div>;
import { MyComponent as MySuperComponent } from './MyComponent';
export const App = () => <MySuperComponent />;
Group all imports and rename
export const Switch = () => <div>Hey, Switch</div>;
export const Group = () => <div>Hey, Group</div>;
import { * as MyComponent } from './MyComponent';
export const App = () => (
<>
<MyComponent.Switch />
<MyComponent.Group />
</>
);
Re-export all
export const Switch = () => <div>Hey, Switch</div>;
export const Group = () => <div>Hey, Group</div>;
export * from "./MyComponent";
import { Switch, Group } from './MyParentComponent';
export const App = () => (
<>
<Switch />
<Group />
</>
);
Re-export, then group all imports and rename
export const Switch = () => <div>Hey, Switch</div>;
export const Group = () => <div>Hey, Group</div>;
export * as MyComponent from "./MyComponent";
import { MyComponent } from './MyParentComponent';
export const App = () => (
<>
<MyComponent.Switch />
<MyComponent.Group />
</>
);
Export type
export type Gender = "Male" | "Female";
import { Gender } from './MyComponent'
Export interface
export interface IUser {
id: string;
name: string;
}
import { IUser } from './MyComponent'
Cross published at .NET Thailand
Acknowledgement: Thank you .net thailand team to review this article: dotnetthailand.github.io#84