首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack 2代码拆分与服务器端渲染和React-Router-v4

Webpack 2代码拆分与服务器端渲染和React-Router-v4
EN

Stack Overflow用户
提问于 2017-01-20 01:30:47
回答 1查看 1.4K关注 0票数 5

使用webpack2.2.0-rc1和react routerv4,并使用此gist使代码拆分正常工作,声明如下

代码语言:javascript
复制
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,然后在客户端I System.import组件A。FInally当我访问延迟加载的路由时,我得到这个react reuse标记警告,因为客户端在加载组件A时呈现了最初从https://gist.github.com/acdlite/a68433004f9d6b4cbc83b5cc3990c194#file-app-js-L21加载的null。Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server: (client) CO 0.0.0 </h1></div><!-- react-empty: 6 - (server) CO 0.0.0 </h1> </div><div data-radium="tru

我怎样才能不出错地工作呢?

EN

回答 1

Stack Overflow用户

发布于 2017-01-20 23:52:50

我刚刚在AsyncComponent上更改了这个line,让它返回一个刹车标签

虽然代码拆分的组件没有加载yet.Then,而不是要求实际的组件呈现服务器端,但我只是抛出了另一个刹车标记,所以标记实际上是匹配的。

这远远不是理想的

代码语言:javascript
复制
export function Shell(Component) {
    return React.createClass({
        render: function () {
            return (
                <div>
                    <Bar/>
                    <Component {...this.props}/>
                </div>
            );
        }
    });
};

export const Waiting = React.createClass({
    render: function () {
        return (
            <div>
                <Bar/>
                <br/>
            </div>
        );
    }
});


// Client routes
const AsyncDash = Utils.asyncRoute(() => System.import("../components/dashboard/dashboard.tsx"));
const AsyncLogin = Utils.asyncRoute(() => System.import("../components/login/login"));

const routes = () => {
    return (<div>
            <Match exactly pattern="/" component={Shell(AsyncLogin)}/>
            <Match exactly pattern="/dashboard" component={Shell(AsyncDash)}/>
        </div>
    );
};


// Server routes
const routes = () => {
    return (<div>
            <Match exactly pattern="/" component={Waiting}/>
            <Match exactly pattern="/dashboard" component={Waiting}/>
        </div>
    );
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41748101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档