Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does react (or maybe es6 in general) allow aliased exports?

Pretty self explanatory, can I do something like this in react/es6?

export function BaseContainer() {} as Container;

I’m programming with a editor on my phone and cant run to test. I also haven’t found anything on google regarding this so maybe this can help another on the same path

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

yes, you can do it.

Here is the basic example which may help you:

const App = () => {
  return (
    <h1>I'm from app</h1>
  )
}

const App2 = () => {
  return (
    <h1>I'm from app 2</h1>
  )
}

export {
  App as MyApp,
  App2 as MyApp2
}

In here, I’ve allias my component with MyApp Name.

OR

you can also allias while importing your component.

Here is the basic example code:

const App = () => {
  return (
    <h1>I'm from app</h1>
  )
}

const App2 = () => {
  return (
    <h1>I'm from app 2</h1>
  )
}

export {
  App,
  App2
}

and while importing you can do like this:

imoprt {App as MyApp, App2 as MyApp2} from "/path"

And for default export you do not need to worry about allias, you can export your component by any name and import it with any name. Here is the code:

// component.js
const App = () => {
  return (
    <h1>I'm from component</h1>
  )
}

export default App;

And in main App you can import this component by any name:

// main.js
import MyComponent from "./component"

const Main = () => {
  return (
    <MyComponent />
  )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading