首页 > 解决方案 > 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)

这是我的代码的图像,因此您可以看到行号:

在此处输入图像描述

这是 console.log() 的图像: 在此处输入图像描述

我正在尝试租用一个下拉菜单,其中将包含值 1 - 100 并由状态变量填充值。在更改时,我将执行 handleIntoFundPercent() 来更新状态变量。

我的问题是无法读取未定义的属性“0”。我不确定什么是“0”属性,也不确定它认为哪个变量是未定义的。

感谢您的任何帮助。

标签: javascriptreactjs

解决方案


currentTransfer状态中的对象不包括intoFundPercent

currentTransfer: {
            intoFundNumber: [],
            intoFundName: [],
            intoPercent: []
}

尝试使用this.state.currentTransfer.intoPercent


推荐阅读