2017-01-19 14 views
5

का उपयोग webpack2.2.0-RC1 और routerv4 प्रतिक्रिया है, और इस gist का उपयोग कर काम करने के लिए कोड spliting, जिसमें कहा गया है बनाने के लिए निम्नलिखितसर्वर साइड प्रतिपादन के साथ 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) 
) 

यह वास्तव में काम करता है, लेकिन मैं सर्वर साइड प्रतिपादन का उपयोग कर रहा हूँ। तो सर्वर पर मुझे घटक ए की आवश्यकता होती है, फिर क्लाइंट I System.import घटक ए पर जब मैं आलसी लोड किए गए मार्ग तक पहुंचता हूं तो मुझे यह प्रतिक्रिया पुन: उपयोग मार्कअप चेतावनी मिलती है क्योंकि क्लाइंट ने प्रस्तुत किया है प्रारंभ में https://gist.github.com/acdlite/a68433004f9d6b4cbc83b5cc3990c194#file-app-js-L21 से घटक लोड करते समय घटक को लोड किया गया ए 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

मैं सिर्फ यह एक ब्रेक टैग
वापसी बनाने के लिए है, जबकि कोड-splitted घटक बजाय सर्वर साइड प्रस्तुत करना वास्तविक घटक की आवश्यकता होती है की yet.Then लोड नहीं है 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

संबंधित मुद्दे