最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react-loadable import from multiple class exported js - Stack Overflow

programmeradmin3浏览0评论

How to import from a multiple class exported js file in react-loadable.
I am trying to import CustomButton from MyButton.js using react-loadable in Home.js. This is the only way I know to do it, is not working.

MyButton.js

import {
  CustomButton,
  BUTTON_STYLE,
  BUTTON_TYPE,
  BUTTON_SIZE,
  BUTTON_ALIGN,
  COLOR
} from './buttons';
module.exports = {
  CustomButton,
  BUTTON_STYLE,
  BUTTON_TYPE,
  BUTTON_SIZE,
  BUTTON_ALIGN,
  COLOR
}

Home.js

const AsyncButton = Loadable({
  loader: () => import('../../button/MyButton'),
  loading: Loading
});

Please help. Thanks in advance.

How to import from a multiple class exported js file in react-loadable.
I am trying to import CustomButton from MyButton.js using react-loadable in Home.js. This is the only way I know to do it, is not working.

MyButton.js

import {
  CustomButton,
  BUTTON_STYLE,
  BUTTON_TYPE,
  BUTTON_SIZE,
  BUTTON_ALIGN,
  COLOR
} from './buttons';
module.exports = {
  CustomButton,
  BUTTON_STYLE,
  BUTTON_TYPE,
  BUTTON_SIZE,
  BUTTON_ALIGN,
  COLOR
}

Home.js

const AsyncButton = Loadable({
  loader: () => import('../../button/MyButton'),
  loading: Loading
});

Please help. Thanks in advance.

Share Improve this question asked May 2, 2018 at 15:03 Midhun G SMidhun G S 9779 silver badges23 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

I was able to do it this way:

const AsyncButton = Loadable({
  loader: () => import('../../button/MyButton').then(module => module.CustomButton),
  loading: Loading
});

However, I can't get it where one variable contains all the other exports.

I Found a solution from

react-loadable documentation

Loadable({
  loader: () => import('./my-ponent'),
  render(loaded, props) {
    let Component = loaded.namedExport;
    return <Component {...props}/>;
  }
});

Its working.

I know this is answered, but I came up with a way around getting one variable to contain all named exports. See below.

    AsyncSubSites = namedComponent => Loadable({
    loader: () =>
      import( /*webpackChunkName: "SubSites"*/ "./SubSites/SubSites"),
    loading: () => <Loading/>, //loader/spinner etc.
    modules: ["SubSites"],
    render(loaded, props) {
      let Component = loaded[namedComponent];
      return <Component {...props}/>;
    }
  })

and used with react-router as...

<Route path="/:slug" exact ponent={AsyncSubSites('Membership')} />
<Route path="/:slug" exact ponent={AsyncSubSites('About')} />

and whatever other named export

发布评论

评论列表(0)

  1. 暂无评论