Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use es6 import alias syntax for React Components?

I'm trying to do something like the following, however it returns null:

import { Button as styledButton } from 'component-library' 

then attempting to render it as:

import React, { PropTypes } from "react"; import cx from 'classNames';  import { Button as styledButton } from 'component-library';  export default class Button extends React.Component {     constructor(props){         super(props)     }     render() {         return (                 <styledButton {...this.props}></styledButton>         )     } } 

The reason is, I need to import the Button component from a library, and also export a wrapper component with the same name but maintaining the functionality from the imported component. If I leave it at import { Button } from component library then of course, I get a multiple declaration error.

Any ideas?

like image 424
Magnum Avatar asked Apr 02 '17 19:04

Magnum


People also ask

How do I import alias into React?

To use import aliases when importing components in React, use the as keyword to rename the imported component, e.g. import {Button as MyButton} from './another-file' . The as keyword allows us to change the identifying name of the import.

How do you use ES6 to define a component in React?

Using an ES6 class to write the same component is a little different. Instead of using a method from the react library, we extend an ES6 class that the library defines, Component . constructor() is a special function in a JavaScript class. JavaScript invokes constructor() whenever an object is created via a class.

Does ReactJS use ES6?

React uses ES6, and you should be familiar with some of the new features like: Classes. Arrow Functions. Variables (let, const, var)


2 Answers

Your syntax is valid. JSX is syntax sugar for React.createElement(type) so as long as type is a valid React type, it can be used in JSX "tags". If Button is null, your import is not correct. Maybe Button is a default export from component-library. Try:

import {default as StyledButton} from "component-library"; 

The other possibility is your library is using commonjs exports i.e. module.exports = foo. In this case you can import like this:

import * as componentLibrary from "component-library"; 

Update

Since this is a popular answer, here a few more tidbits:

export default Button              -> import Button from './button'                                       const Button = require('./button').default           export const Button                -> import { Button } from './button'                                       const { Button } = require('./button')           export { Button }                  -> import { Button } from './button'                                       const { Button } = require('./button')           module.exports.Button              -> import { Button } from './button'                                       const { Button } = require('./button')  module.exports.Button = Button     -> import { Button } from './button'                                       const { Button } = require('./button')  module.exports = Button            -> import * as Button from './button'                                       const Button = require('./button')  
like image 51
chris Avatar answered Oct 06 '22 14:10

chris


Try to import this way

import {default as StyledLibrary} from 'component-library'; 

I suppose you export

export default StyledLibrary 
like image 43
Marcin Rakowski Avatar answered Oct 06 '22 16:10

Marcin Rakowski