2015-11-04 8 views
6

मैं अपने सर्वर साइड प्रतिपादन के लिए match विधि का उपयोग करता हूं और कॉलबैक में पैरामीटर हमेशा अपरिभाषित होते हैं। शायद कुछ गलत हो गया लेकिन यह एक पूरा दिन रहा है और मैं इसके चारों ओर अपना सिर नहीं ले सकता।Reactjs राउटर मैच कॉलबैक पैरामीटर हमेशा अपरिभाषित

यहां मेरा सर्वर साइड प्रतिपादन है।

// Create location from the history module. 
let location = createLocation(req.url); 

match({Routes, location}, (error, redirectLocation, renderProps) => { 

    // TODO : Verify why this is always undefined 
    console.log('ERROR :: ', error) 
    console.log('REDIRECT LOCATION :: ', redirectLocation) 
    console.log('RENDER PROPS :: ', renderProps) 

    if (redirectLocation) { 
     res.redirect(redirectLocation.pathname + redirectLocation.search) 
    } 
    // TODO : Verify why this is breaking 
    //else if (error || !renderProps) { 
    // return console.log('Error while starting server :: ', error) 
    //} 
    else { 

     Transmit.renderToString(RoutingContext, renderProps).then(({reactString, reactData}) => { 

      console.log('REACT STRING :: ', reactString); 
      console.log('REACT DATA :: ', reactData); 

      let output = (
       `<!doctype html> 
       <html lang="en-us" style="min-height:100vh; width: 100%; background-color: #eee;"> 
        <head> 
         <meta charset="utf-8"> 
         <title>react-isomorphic-pandora-app</title> 
         <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> 
         <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:400,100,700"> 
        </head> 
        <body> 
         <div id="react-root">${reactString}</div> 
        </body> 
       </html>` 
      ); 

      var webserver = process.env.NODE_ENV === "production" ? "" : "//localhost:8080" 

      output = Transmit.injectIntoMarkup(output, reactData, [`${webserver}/dist/client.js`]) 

      res.send(output) 

     }).catch((error) => { 

      res.send(error.stack).type("text/plain").code(500) 

     }) 
    } 
}) 

यहां मेरा ग्राहक पक्ष है।

import React from "react"; 
import ReactDOM from "react-dom"; 
import {Router} from "react-router"; 
import Transmit from "react-transmit"; 
import routes from "../views/Routes"; 
import {createHistory} from "history"; 

let reactRoot = window.document.getElementById("react-root"); 
let history = createHistory(); 
let location = history.createLocation(); 

const routerOption = { 
    routes: routes, 
    history: history, 
    location: location 
} 

Transmit.render(Router, routerOption, reactRoot); 

if (process.env.NODE_ENV !== "production") { 
    if (!reactRoot.firstChild || !reactRoot.firstChild.attributes || 
     !reactRoot.firstChild.attributes["data-react-checksum"]) { 
     console.error("Server-side React render was discarded. Make sure that your initial render does not contain any client-side code."); 
    } 
} 

यहां मेरे मार्ग हैं।

import React from "react" 
import {Router, Route} from "react-router" 
import MenuView from "./Menu" 
import DefaultView from "./Default" 
import AnotherView from "./Another" 

export default (
    <Router component={MenuView}> 
     <Route path="/" component={DefaultView} /> 
     <Route path="/another-view" component={AnotherView} /> 
    </Router> 
); 

किसी भी मदद की सराहना की जाएगी। धन्यवाद :)

संपादित करें --------

यहाँ मेरी MenuView घटक के अंदर कोड है।

class MenuView extends React.Component { 

    constructor(){ 

     super() 

     this.menuItems = [ 
      { 
       type: MenuItem.Types.SUBHEADER, 
       text: 'Menu sub header' 
      }, 
      { 
       route: '/', 
       text: 'Home' 
      }, 
      { 
       route: '/another-view', 
       text: 'Another view' 
      } 
     ] 
    } 

    childContextTypes = { 
     muiTheme: React.PropTypes.object 
    }; 

    getChildContext() { 
     return { 
      muiTheme: ThemeManager.getMuiTheme(DefaultTheme) 
     } 
    } 

    _getSelectedIndex =() => { 

     let currentItem; 

     for (let i = this.menuItems.length - 1; i >= 0; i--) { 
      currentItem = this.menuItems[i]; 
      if (currentItem.route && this.props.history.isActive(currentItem.route)) return i; 
     } 

    } 

    _onLeftNavChange = (e, key, payload) => { 

     this.props.history.pushState(null, payload.route); 

    } 

    render() { 

     var style = { 
      paddingTop: '92px' 
     } 

     return (
      <div> 
       <LeftNav 
        ref="leftNav" 
        menuItems={this.menuItems} 
        onChange={this._onLeftNavChange} 
        selectedIndex={this._getSelectedIndex()} 
        style={style}/> 
       <section className="content"> 
        {this.props.children} 
       </section> 
      </div> 
     ) 
    } 

} 

उत्तर

0

आपके मार्ग मुद्दे हैं। आपके पास match के बाद RoutingContext में आप शीर्ष-स्तर <Router> हैं। यह पीछे की तरफ है, आमतौर पर क्लाइंट साइड we create a RoutingContext within the <Router> component पर।

मुझे लगता है कि आप बस <Router> को केवल <Route> बनाना चाहते हैं ताकि सभी मार्गों पर आपके शीर्ष-स्तर <MenuView> घटक प्रदान किए जा सकें।

+0

ठीक है मैं संपादित '' इतना है कि यह एक ' है' । लेकिन मुझे अभी भी एक ही समस्या है। मैंने कोड को भी जोड़ा जो मेरे 'मेनू व्यू' में है। –

4

मैं तुम्हें कोलाहल 6, नहीं कोलाहल 5 का उपयोग लगता है।

मुझे एक ही समस्या थी। यह बेबेल 5 के साथ ठीक है, लेकिन बेबेल के साथ नहीं 6.

लंबे डिबगिंग के बाद, मैंने पाया कि निम्नलिखित परिवर्तन करने से मेरे लिए समस्या ठीक हो जाती है।

परिवर्तन:

match({Routes, location} 

रहे हैं:

match({Routes.default, location} 
+0

मैं वास्तव में 'बेबेल @ 5.8.23' का उपयोग कर रहा हूं और यह मुझे वह त्रुटि देता है। –

3

आप असाइनमेंट गलत destructuring ES6 गया। Destructuring assignment

match({Routes, location})match({routes, location})

या अधिक सुरक्षित तरीका

match({routes:Routes, location:location})

+0

मुझे सुरक्षित तरीका पसंद आया ('मिलान ({मार्ग: मार्ग, स्थान: स्थान}) ') और मुझे अभी भी वही त्रुटियां मिलती हैं। –

4

समस्या मार्गों फ़ाइल में exports default है की जरूरत है। यह { defaults: { // routes in here } जैसे ऑब्जेक्ट देता है जबकि क्लाइंट पर, यह { // routes in here } जैसी ऑब्जेक्ट देता है। आपको फ़ाइल को modules.exports = [...routes...]; जैसे अलग तरीके से निर्यात करने की आवश्यकता है।

4

बेबेल 5 बनाम के कारण।कोलाहल 6 और निर्यात मार्गों फाइल यह इस तरह दिखना चाहिए:

कोलाहल 5:

Router.match({ routes:routes, location: req.url}, etc ... 

कोलाहल 6:

Router.match({ routes:routes.default, location: req.url}, etc ... 
संबंधित मुद्दे