javascript - reactJS生成无法读取未定义的属性
问题描述
我定义了以下 reactJS 状态变量:
constructor(props) {
super(props);
this.state = {
passData: this.props.location.state,
transferState: "0",
numberOfFunds: 0,
indicative: {
functionUpdate: false,
planName: '',
familyID: '',
planID: '',
loc: '',
memberID: '',
PIN: '',
mqMessage: '' ,
planMenuString: ''
},
data: {
inv_FundAccurecordFundNumber: [],
inv_FundMsgNumber: [],
inv_FundName: [],
inv_FundBalance: [],
inv_FundAllocation: [],
inv_FundPercent: [],
inv_FundMinPct: [],
inv_FundMaxPct: [],
inv_FundTrsfInAllowed: [],
inv_FundTrsfOutAllowed: []
},
currentTransfer: {
intoFundNumber: [],
intoFundName: [],
intoPercent: []
}
}
this.returnHome = this.returnHome.bind(this);
this.buttonNext = this.buttonNext.bind(this);
this.buttonCancel = this.buttonCancel.bind(this);
}
我有以下代码:
renderIntoPercent = (index, fundNumber, fundName) => {
console.log("index/fundNumber/fundName: ", index, fundNumber, fundName);
let obj = {
array: []
};
for (var l=0;l<=100;l++){
obj.array[l] = l;
}
let optionItems = obj.array.map((item) =>
<option key={item}>{item}</option>
);
return (
<div className="col-2 text-left text_14">
<select value={this.state.currentTransfer.intoFundPercent[index]}
onChange={(event) => this.handleIntoFundPercent(event, index, fundNumber, fundName)} >
{optionItems}
</select>
</div>
)
}
当我运行应用程序并第一次执行 renderIntoPercent 时,我分别传入 0、04 和 American Century Inflation-Adjusted Bond 作为 3 个参数。
当代码执行时,我查看 console.log,我看到以下错误消息:
未捕获的类型错误:无法在 transferRebalance._this.renderIntoPercent 读取未定义的属性“0”(transferRebalance.js:412)
这是我的代码的图像,因此您可以看到行号:
我正在尝试租用一个下拉菜单,其中将包含值 1 - 100 并由状态变量填充值。在更改时,我将执行 handleIntoFundPercent() 来更新状态变量。
我的问题是无法读取未定义的属性“0”。我不确定什么是“0”属性,也不确定它认为哪个变量是未定义的。
感谢您的任何帮助。
解决方案
currentTransfer
状态中的对象不包括intoFundPercent
currentTransfer: {
intoFundNumber: [],
intoFundName: [],
intoPercent: []
}
尝试使用this.state.currentTransfer.intoPercent
推荐阅读
- c++ - 将 opencv 中的“videocapture”数据插入到 Tensorflow-lite
- performance - 我如何在普罗米修斯中查阅这些查询?
- ethereum - 如何使用 waffle 和 ethers.js 测试应付/外部方法
- python - 根据时间Python聚合列表中的分类值
- mysql - mysql中的外连接不提供结果
- javascript - 如何使用 Reflect.getMetadata 检查异步/承诺
- python - 蟒蛇虫?我做错了什么?
- jmeter - 是否可以更改 JSR223 后处理器中的事务控制器响应时间?
- python - 在 linux 操作系统上安装 mtcnn 时出错。在 python 3.6.5 和最新的 pip 版本 21.0.1 上运行
- usb - 使用不同的供应商和产品 ID 使 USB 设备可见