2016-02-09 10 views
18
  • की संपत्ति 'सहारा' पढ़ा नहीं जा सकता मैं एक प्रतिक्रिया कोड
  • इस कोड को यूआई में विभिन्न पैनलों renders है।
  • जब मैं एक टैग क्लिक करें, इस समारोह sportsCornerPanel()
  • कहा जाता है, लेकिन मैं Uncaught TypeError यह कैसे
  • नीचे स्निपेट कोड उपलब्ध कराने के ठीक करने के लिए हो रही है।
  • पूरे कोड आप बेला

कोड स्निपेटUncaught TypeError: अशक्त

sportsCornerPanel() { 
     debugger; 

     console.log("sportsCornerPanel" 
     console.log("this.props.sportsPanelState.size-->" + this.props); 

     if (this.props.sportsPanelState.size === 'hidden') { 

      if (!this.props.sportsPanelState.visible) { 
       this.props.dispatch(sportsOpenPanel()); 
      } else { 
       this.props.dispatch(sportsClosePanel()); 
      } 
     } 
    } 


    render() { 


     let sportsContent, leftNavLink; 

     if (this.props.sports-layout !== 'small') { 
      console.log("SportsBox---page loads at bigger size"); 
      console.log("SportsBox---page loads at ipad size"); 
      sportsContent = <SportsBox className="sports-header"/>; 
     } else { 
      if (this.props.sportsPanelState.visible) { 
       console.log("sportsPanelState--when it becomes small--around ipad width"); 

       sportsContent = <SportsBox className="sports-nav"/>; 
       leftNavLink = <a onClick={this.sportsCornerPanel} href="javascript:;" className="header-navLink active"></a>; 
      } else { 
       if (this.props.sports.active) { 

        console.log("SportsBox"); 

        sportsContent = <SportsBox className="sports-nav"/>; 
       } else { 

        console.log("leftNavLink--when it becomes small--around ipad width"); 

        leftNavLink = <a onClick={this.sportsCornerPanel} href="javascript:;" className="header-navLink"></a>; 
       } 
      } 
     } 


output 

Uncaught TypeError: Cannot read property 'props' of null 
+1

क्यों इस टैग है 'css' और' html'? क्योंकि यह ऑनलाइन है? –

उत्तर

33

जब से तुम, वर्ग के तरीकों this घटक उदाहरण के लिए संदर्भित करता नहीं करता है में React.createClass उपयोग नहीं कर रहे हैं ताकि आप इसे मैन्युअल रूप से बांधना चाहिए। ऐसे कई तरीके हैं:

1. मैन्युअल रूप से वर्ग निर्माता में this बाँध

constructor(props) { 
    super(props); 
    this.sportsCornerPanel= this.sportsCornerPanel.bind(this); 
} 

2. तीर समारोह के साथ उपयोग ES7 संपत्ति initializers

sportsCornerPanel =() => { 
    debugger; 

    console.log("sportsCornerPanel" 
    console.log("this.props.sportsPanelState.size-->" + this.props); 

    if (this.props.sportsPanelState.size === 'hidden') { 

     if (!this.props.sportsPanelState.visible) { 
      this.props.dispatch(sportsOpenPanel()); 
     } else { 
      this.props.dispatch(sportsClosePanel()); 
     } 
    } 
} 

3. बाइंड this कॉल-साइट

render() विधि में:

let sportsContent, leftNavLink; 

    if (this.props.sports-layout !== 'small') { 
     console.log("SportsBox---page loads at bigger size"); 
     console.log("SportsBox---page loads at ipad size"); 
     sportsContent = <SportsBox className="sports-header"/>; 
    } else { 
     if (this.props.sportsPanelState.visible) { 
      console.log("sportsPanelState--when it becomes small--around ipad width"); 

      sportsContent = <SportsBox className="sports-nav"/>; 
      leftNavLink = <a onClick={this.sportsCornerPanel.bind(this)} href="javascript:;" className="header-navLink active"></a>; 
     } else { 
      if (this.props.sports.active) { 

       console.log("SportsBox"); 

       sportsContent = <SportsBox className="sports-nav"/>; 
      } else { 

       console.log("leftNavLink--when it becomes small--around ipad width"); 

       leftNavLink = <a onClick={this.sportsCornerPanel.bind(this)} href="javascript:;" className="header-navLink"></a>; 
      } 
     } 
    } 
+0

आपके उत्तर के लिए धन्यवाद ... क्या आप मुझे बता सकते हैं कि आपने कैसे डिबग किया और पता चला ... ताकि भविष्य में मैं इसे स्वयं कर सकूं ... यदि आप मुझे बताएं कि कंसोल कहां रखना है .. मैं –

+3

डाल दूंगा और समझूंगा कि 'React.createClass()' – xCrZx

+0

के बजाय 'React.Component' का उपयोग करते समय यह सामान्य समस्या है, लेकिन क्या आप मुझे बता सकते हैं कि आप कैसे डिबग करते हैं और पता लगाते हैं ... तो भविष्य में मैं इससे बच सकता हूं .. –

0

ऐसा लगता है कि आप अपनी प्रतिक्रिया घटक पर एक getDefaultProps विधि अनुपलब्ध हो सकता है में देख सकते हैं। आप कुछ इस तरह सामान्य विचार कर सकते हैं, बस त्रुटि को रद्द करना और देखना और क्या है अप करने के लिए:

getDefaultProps() { return {}; }

+0

आपके उत्तर के लिए धन्यवाद ... क्या आप इसे मेरे कोड में अपडेट कर सकते हैं ... इसकी उलझन में –

+0

तथ्य यह है कि त्रुटि यह कह रही है कि समस्या प्रतिक्रिया घटक के साथ है और जरूरी नहीं कि इसके प्रोप मुझे सोचते हैं कि * props * मुद्दा नहीं हैं - क्या आपने यह समाधान देखने के लिए परीक्षण किया है कि यह काम करता है या नहीं? –

+0

@NickZuber मेरे कोड को इतना भ्रमित करने के लिए संभव है :( –

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