import asyncComponent from './asyncComponent'
const AsyncButton = asyncComponent(() => import('./Buttons'))
export { ButtonPrimary } = AsyncButton
为什么上面的代码无效? Button
组件是普通组件。
我这样做export { ButtonPrimary } from './Buttons'
没关系。
Mt asyncComponent代码
import React, { Component } from 'react'
const asyncComponent = importComponent => {
class AsyncComponent extends Component {
state = {
component: null
}
async componentDidMount() {
const { default: component } = await importComponent()
this.setState({
component: component
})
}
render() {
const C = this.state.component
return C ? <C {...this.props} /> : null
}
}
return AsyncComponent
}
export default asyncComponent
答案 0 :(得分:0)
似乎
const AsyncButton = asyncComponent(() => import('./Buttons'))
export { ButtonPrimary } = AsyncButton
应该是
export const ButtonPrimary = asyncComponent(() => import('./Buttons'));
答案 1 :(得分:0)
export { ButtonPrimary } = AsyncButton
语法无效,因为export
支持here。它不会进行解构,ButtonPrimary
也不是AsyncButton
属性。
如果ButtonPrimary
是Buttons
模块导出:
export { ButtonPrimary } from './Buttons';
那应该是:
const AsyncButton = asyncComponent(async () => (await import('./Buttons')).ButtonPrimary);
export { AsyncButton as ButtonPrimary };