javascript - Index.js module imports with webpack - Stack Overflow

My code is organised as follows: where, ResourcesActionLogComponentsLayout.js import React from 

My code is organised as follows:

where,

Resources/ActionLog/Components/Layout.js

import React from 'react';

export default class Layout extends React.Component {
    render() {
        return (
            <p>Test</p>
        );
    }
}

Resources/ActionLog/Components/index.js

export * from './Layout';

Resources/ActionLog/index.js

import React from 'react';
import ReactDOM from 'react-dom';

import Layout from './Components'; // <--- ISSUE HERE.

const app = document.getElementById('app');
ReactDOM.render(
    <Layout/>,
    app
);

Why does Layout not get imported using this setup??

If i change the line to read,

import Layout from './Components/Layout';

it works fine, but otherwise Layout is always undefined! Even when if i try,

import Layout from './Components/index';

I am using webpack as my module bundler, and have achieved something similar before, I just don't see why/how this is different..

My code is organised as follows:

where,

Resources/ActionLog/Components/Layout.js

import React from 'react';

export default class Layout extends React.Component {
    render() {
        return (
            <p>Test</p>
        );
    }
}

Resources/ActionLog/Components/index.js

export * from './Layout';

Resources/ActionLog/index.js

import React from 'react';
import ReactDOM from 'react-dom';

import Layout from './Components'; // <--- ISSUE HERE.

const app = document.getElementById('app');
ReactDOM.render(
    <Layout/>,
    app
);

Why does Layout not get imported using this setup??

If i change the line to read,

import Layout from './Components/Layout';

it works fine, but otherwise Layout is always undefined! Even when if i try,

import Layout from './Components/index';

I am using webpack as my module bundler, and have achieved something similar before, I just don't see why/how this is different..

Share Improve this question asked Oct 19, 2016 at 19:21 Sean BugejaSean Bugeja 1643 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Why does Layout not get imported using this setup??

Layout.js has a default export. However, export * from './Layout.js will only export the named exports (of which there are none). In other words, Components/Layout.js doesn't have any exports at all, so nothing can be imported.

But even if it did have named exports, import Layout from './Components/index'; imports the default export, but Components/index.js doesn't have a default export.


There are a couple of ways this could be solved. The one that makes the most sense is probably to export the default export of Layout.js as named export in Components/index.js. You will presumably have multiple files each exporting a ponent. I assume Components/index.js should export a map of all these ponents in which case you have to use named exports.

The changes you have to make:

// in Components/index.js
export {default as Layout} from './Layout';


// in ActionLog/index.js
import {Layout} from './Components'; // use a named import

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744707926a4589166.html

相关推荐

  • javascript - Index.js module imports with webpack - Stack Overflow

    My code is organised as follows: where, ResourcesActionLogComponentsLayout.js import React from 

    18小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信