我正在嘗試使用webpack 2實現動態代碼拆分並作出反應。爲了測試我創建了代碼異步牽引組件:爲什麼這反應組件,導入System.import,不渲染?
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.
把一個的console.log(this.state .component)在Async
的渲染功能,結果如下:
那麼什麼錯嗎?看起來好像我得到了有效的反應組件,那爲什麼它會拋出一個錯誤?
應該從'react''導入React,{Component} from'react''不是'import {React,Component}嗎? – evolutionxbox
@evolutionxbox不,React是'react'包的默認導出 – vsjn3290ckjnaoij2jikndckjb