2016-01-08 84 views
0

我試圖調用getElementByKey()功能,我映射的集合:ReactJS - 呼叫從地圖功能的功能,功能沒有定義

{this.state.sections.map(function(section) { 
       return (<Tab key={section.value} title={section.label}> 
         {this.getElementByKey(section.name)} 
       </Tab>); 
      })} 

這裏是我想要調用方法:

getElementByKey: function(name){ 

    switch(name) { 
     case "EditStudentBasicDetails": 
      return <EditStudentBasicDetails studentId={this.state.studentId} sectionData={this.state.studentData.activeData.basicDetails} />; 
     case "EditStudentAgentsInfo": 
      return <EditStudentAgentsInfo studentId={this.state.studentId} sectionData={this.state.studentData.activeData.agentsInfo} />; 
     case "EditStudentCaseNotes": 
      return <EditStudentCaseNotes studentId={this.state.studentId} sectionData={this.state.studentData.activeData.caseNotes}/>; 
     case "EditStudentRegistration": 
      return <EditStudentRegistration studentId={this.state.studentId} sectionData={this.state.studentData.activeData.registration} />; 
     case "EditStudentContactDetails": 
      return <EditStudentContactDetails studentId={this.state.studentId} sectionData={his.state.studentData.activeData.contactDetails} />; 
     case "EditStudentAttendance": 
      return <EditStudentAttendance studentId={this.state.studentId} sectionData={this.state.studentData.activeData.attendance} />; 
     case "EditStudentSENDetails": 
      return <EditStudentSENDetails studentId={this.state.studentId} sectionData={this.state.studentData.activeData.SENDetails} />; 
     case "EditStudentSENReviews": 
      return <EditStudentSENReviews studentId={this.state.studentId} sectionData={this.state.studentData.activeData.SENReviews} />; 

     default:{ 

     } 
    } 

}, 

我得到錯誤,this.getElementByKey不是一個函數,任何人都可以請幫忙嗎?

回答

3

因爲它是你的this是映射函數,而不是你可能會懷疑的組件。

.map()需要第二個參數,它將映射函數的上下文設置爲任何正在傳遞的對象。

.map(function() {}, this); 

或者您可以使用.bind()改變this意義。

this.state.sections.map(function() {}.bind(this));