gpt4 book ai didi

javascript - ReactJS - 从 map 函数调用函数,函数未定义

转载 作者:行者123 更新时间:2023-11-28 18:49:39 24 4
gpt4 key购买 nike

我正在尝试调用 getElementByKey() 函数来映射集合:

{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 不是函数,有人可以帮忙吗?

最佳答案

就目前情况而言,您的 this 是映射函数,而不是您可能怀疑的组件。

.map() 采用第二个参数,它将映射函数的上下文设置为传递的任何对象。

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

或者你可以使用.bind()来改变this的含义。

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

关于javascript - ReactJS - 从 map 函数调用函数,函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34678440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com