Can you use es6 import alias syntax for React Components?

旧城冷巷雨未停 提交于 2019-12-20 09:12:36

问题


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?


回答1:


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";



回答2:


Try to import this way

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

I suppose you export

export default StyledLibrary



回答3:


No idea why I am not able to alias the import;

As a work around, I ended up doing this:

import React, { PropTypes } from "react";
import * as StyledLibrary from 'component-library';

export default class Button extends React.Component {
    constructor(props){
        super(props)
    }
    render() {
        return (
            <StyledLibrary.Button {...this.props}></StyledLibrary.Button>
        )
    }
}

Thanks all




回答4:


User-Defined Components Must Be Capitalized
https://reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized

change your code to

import { Button as StyledButton } from 'component-library';
....bah...bah....bah  
<StyledButton {...this.props}></StyledButton>



回答5:


note that when you capitalized StyledLibrary and it worked

whereas, in the original question, you did not capitalize styledButton and it did not work

both of these are the expected results with React

so you didn't discover a workaround, you simply discovered the (documented) React way of doing things




回答6:


Careful with capitalisation. Best to always CamelCase.

One:

import Thing from "component";

One with alias:

import {Thing as OtherThing} from "component";

One with alias plus other defaults:

import {Thing as OtherThing}, Stuff, Fluff from "component";

More detailed example

import
{Thing as StyledButton},
{Stuff as Stuffing},
{Fluff as Fluffy},
Wool,
Cotton
from "component";


来源:https://stackoverflow.com/questions/43172750/can-you-use-es6-import-alias-syntax-for-react-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!