【问题标题】:react js : TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))react js:TypeError:object null is not iterable(无法读取属性Symbol(Symbol.iterator))
【发布时间】:2019-08-05 21:44:10
【问题描述】:
this.state.hiring.map(h => (
  <FormApp
    condType={h.type}
    condRef={h.ref}
    label={h.name}
    labelName={h.name}
    name={h.id}
    id={h.name}
    validations={h.required == true ? [this.required] : null}
    dataList={this.state[h.ref]}
    onChange={this.onChangeInput}
  />
));

我想 if (h.requered == true) { return [this.required] } else { null }

我有问题

react js : TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))

【问题讨论】:

  • 看起来validations 期望传递一个数组,而不是null。所以也许传递一个空数组而不是null
  • This.state.hiring 为空,您可以将招聘的初始状态设置为空数组。
  • 什么是this.state.hiring?你能更新代码吗?我敢打赌你从这行得到错误,而不是validations,因为它是你的 sn-p 中唯一的迭代部分

标签: javascript reactjs


【解决方案1】:

也许你可以像这样修改你的代码:

const { hiring } = this.state;
hiring instanceof Array && hiring.map(h => { // ==> Validate hiring before use it.
        if (h.requered == true) {
            return (
                <FormApp
                    condType={h.type}
                    condRef={h.ref}
                    label={h.name}
                    labelName={h.name}
                    name={h.id}
                    id={h.name}
                    validations={h.required == true ? [this.required] : null}
                    dataList={this.state[h.ref]}
                    onChange={this.onChangeInput}
                />
            );
        } else {
            return null;
        }
    });

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 2019-08-13
    • 2020-12-14
    • 2021-05-21
    • 1970-01-01
    • 2021-10-22
    • 2020-12-02
    • 2022-11-28
    • 2021-01-10
    相关资源
    最近更新 更多