在使用webpack2.2.0-rc1和react routerv4时,可以使用这个代码片段来实现代码分割。其中提到:
function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
if (!this.state.Component) {
getComponent().then(Component => {
AsyncComponent.Component = Component
this.setState({ Component })
})
}
}
render() {
const { Component } = this.state
if (Component) {
return <Component {...this.props} />
}
return null
}
}
}
const Foo = asyncComponent(() =>
System.import('./Foo').then(module => module.default)
)
它实际上是可行的,但我正在使用服务器端渲染。因此,在服务器上,我要求组件A,然后在客户端上System.import组件A。 最终,当我访问懒加载路由时,我会收到此React重用标记警告,因为客户端渲染最初从https://gist.github.com/acdlite/a68433004f9d6b4cbc83b5cc3990c194#file-app-js-L21中加载null组件A.
警告:React尝试在容器中重用标记,但校验和无效。这通常意味着您正在使用服务器渲染,并且服务器上生成的标记不是客户端期望的。React注入了新的标记来弥补这个问题,但您已经失去了许多服务器渲染的好处。相反,请找出为什么在客户端或服务器上生成的标记不同:
(客户端)CO 0.0.0 </h1></div><!-- react-empty: 6 -
(服务器)CO 0.0.0 </h1> </div><div data-radium="tru
如何避免这些错误?