【发布时间】:2017-06-23 09:34:49
【问题描述】:
我正在尝试使用 webpack 2 实现动态代码拆分并做出反应。为了测试,我创建了一个异步提取代码的组件:
import React, { Component } from 'react'
export class Async extends Component {
constructor(props) {
super(props)
this.state = { component: <div>Loading</div> }
}
componentDidMount() {
System.import('../../about')
.then(component => this.setState({ component: component.About }))
.catch(error => this.setState({ component: <div>{error.message}</div> }))
}
render() {
return this.state.component
}
}
但是,当我挂载它时,它会返回以下错误:
Async.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
在Async 的渲染函数中放入一个console.log(this.state.component) 会产生以下结果:
那么这里出了什么问题?好像我得到了有效的反应组件,那为什么会抛出错误?
【问题讨论】:
-
应该
import React, { Component } from 'react'不是import { React, Component } from 'react'? -
@evolutionxbox 不,React 是“react”包的默认导出
标签: javascript reactjs webpack webpack-2 es6-modules