2017-01-19 12 views
5

WebPACKの2コードSplitingして反応-ルータ-V4を

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) 
) 

それを述べている、webpack2.2.0-rc1の使い方とrouterv4反応、およびコードsplitingが動作するようにするために、このgistを使用して実際には動作しますが、私はサーバーサイドレンダリングを使用しています。サーバ上でコンポーネントAが必要な場合は、クライアントIのSystem.importコンポーネントAを要求します。 Filledことがあります。遅延ロードされたルートにアクセスすると、このクライアントはhttps://gist.github.com/acdlite/a68433004f9d6b4cbc83b5cc3990c194#file-app-js-L21 A. 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

どうすればこの作業を間違いなく行うことができますか?

答えて

0

私はちょうどコード分割されたコンポーネントがロードされていないうちに、ブレーキタグ
を返すようにAsyncComponentのこのlineを変更しました。実際のコンポーネントをサーバー側にレンダリングする代わりに、別のブレーキタグマークアップは実際に一致します。

これは理想的な

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

あなたは、このためのソリューションを越えてきたから遠く離れているのですか? –

+2

多くのソリューション。リアクション非同期コンポーネント、古いハックただしないでください。反復読み込み可能、​​新しいハックリアクションユニバーサルコンポーネントは、新しいハックをさらに改善します。 – CESCO

関連する問題