Javascript TypeError:TypeError:undefined不是对象(正在计算此.state.scheduleList';) 导出默认类ScheduleDayTest扩展React.Component{ 建造师(道具){ 超级(道具); 此.state={ 时间表:[ { id:1, 标题:“到达和登记”, 类型:'组织', 时间:下午三点, 地点:'主大厅', 消息:“此时到达”, }, { id:2, 标题:“英语节目开场白”, 键入:“英语程序”, 时间:“下午6:00-6:30”, 地点:“紫丁香”, 信息:“欢迎参加第32届年会”, }, { id:3, 标题:“阿拉伯语节目开幕词”, 键入:“阿拉伯语程序”, 时间:“下午6:00-6:30”, 地点:'Lilac BD', 信息:“欢迎参加第32届年会”, }, { id:4, 标题:"开幕式",, 类型:'组织', 时间:“晚上8:00-10:00”, 地点:'哈利舞厅', 信息:“开幕式”, }, ], 索引:this.getIndex(this.props.navigation.state.params.itemID), } } getIndex=(itemID)=>{ for(设i=0;i
我试图调用getIndex函数来初始化index状态变量,但我一直收到这样一个错误:“this.state.scheduleList”不存在,即使我声明了它。任何帮助都会很好。初始化状态时调用getIndex,因此它将是未定义的。 试试下面Javascript TypeError:TypeError:undefined不是对象(正在计算此.state.scheduleList';) 导出默认类ScheduleDayTest扩展React.Component{ 建造师(道具){ 超级(道具); 此.state={ 时间表:[ { id:1, 标题:“到达和登记”, 类型:'组织', 时间:下午三点, 地点:'主大厅', 消息:“此时到达”, }, { id:2, 标题:“英语节目开场白”, 键入:“英语程序”, 时间:“下午6:00-6:30”, 地点:“紫丁香”, 信息:“欢迎参加第32届年会”, }, { id:3, 标题:“阿拉伯语节目开幕词”, 键入:“阿拉伯语程序”, 时间:“下午6:00-6:30”, 地点:'Lilac BD', 信息:“欢迎参加第32届年会”, }, { id:4, 标题:"开幕式",, 类型:'组织', 时间:“晚上8:00-10:00”, 地点:'哈利舞厅', 信息:“开幕式”, }, ], 索引:this.getIndex(this.props.navigation.state.params.itemID), } } getIndex=(itemID)=>{ for(设i=0;i,javascript,react-native,Javascript,React Native,我试图调用getIndex函数来初始化index状态变量,但我一直收到这样一个错误:“this.state.scheduleList”不存在,即使我声明了它。任何帮助都会很好。初始化状态时调用getIndex,因此它将是未定义的。 试试下面 export default class ScheduleDayTest extends React.Component { constructor(props) { super(props); this.state = {
export default class ScheduleDayTest extends React.Component {
constructor(props) {
super(props);
this.state = {
scheduleList: [
{
id: 1,
title: 'Arrival & Registration',
type: 'Organizational',
time: '3:00 PM',
location: 'Main Lobby',
message: 'Arrival at this time',
},
{
id: 2,
title: 'English Program Opening Statement',
type: 'English Program',
time: '6:00 PM - 6:30 PM',
location: 'Lilac AC',
message: 'Welcome to the 32nd annual conference',
},
{
id: 3,
title: 'Arabic Program Opening Statement',
type: 'Arabic Program',
time: '6:00 PM - 6:30 PM',
location: 'Lilac BD',
message: 'Welcome to the 32nd annual conference',
},
{
id: 4,
title: 'Opening Ceremony',
type: 'Organizational',
time: '8:00 PM - 10:00 PM',
location: 'Harry Ballroom',
message: 'Opening Ceremony',
},
],
index: this.getIndex(this.props.navigation.state.params.itemID),
}
}
getIndex = (itemID) => {
for(let i = 0; i < this.state.scheduleList.length; i++) {
if (this.state.scheduleList[i].id === itemID) {
console.log(i);
return i;
}
}
return 0;
}
我试过了,现在当我尝试使用这个.state.scheduleList[this.state.index].message或列表中的任何项目时,我收到另一个类型错误,说undefined不是对象对不起,我的错误。它应该是
findIndex
。更新的答案
export default class ScheduleDayTest extends React.Component {
constructor(props) {
super(props);
const scheduleList = [
{
id: 1,
title: 'Arrival & Registration',
type: 'Organizational',
time: '3:00 PM',
location: 'Main Lobby',
message: 'Arrival at this time',
},
{
id: 2,
title: 'English Program Opening Statement',
type: 'English Program',
time: '6:00 PM - 6:30 PM',
location: 'Lilac AC',
message: 'Welcome to the 32nd annual conference',
},
...
];
this.state = {
scheduleList,
index: scheduleList.findIndex(item => item.id === this.props.navigation.state.params.itemID),
}
}
}