2017-01-19 18 views
5

kullanma webpack2.2.0-rc1 ve routerv4 tepki ve bu gist kullanarak aşağıdaki kod Siil devletler, hangi çalışmayı yapmakSunucu Tarafı Rendering ile WebPack 2 Kod çatlıyor ve Tepki-Yönlendirici-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) 
) 

It Aslında çalışır, ancak sunucu tarafı oluşturma kullanıyorum. Bu yüzden sunucuda A bileşeni, sonra istemci I System.import bileşeni A. FInally tempolu yüklenen rotaya eriştiğimde bu istemci yeniden https://gist.github.com/acdlite/a68433004f9d6b4cbc83b5cc3990c194#file-app-js-L21 nihayet yüklendiğinde Bileşen yükleniyor A.

nasıl hatasız bu işi yapabilir 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 ?

cevap

0

Kod bölme bileşeni henüz yüklenmemişken, bir
fren etiketi döndürmek için AsyncComponent öğesinde bu line'u değiştirdim. Daha sonra, sunucu tarafını oluşturmak için gerçek bileşene gerek duymak yerine, başka bir fren etiketini atacağım Yani işaretleme aslında eşleşiyor.

Bu ideal

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

Bunun için bir çözümü rastladınız mı uzaktır? –

+2

Pek çok çözüm. Tepki-async-bileşeni, eski hack. Sadece yapma. Tepki-yüklenebilir, yeni kesmek. React-universal-component daha iyi yeni bir hack. – CESCO

İlgili konular