首页 > 解决方案 > react js:TypeError:object null is not iterable(无法读取属性Symbol(Symbol.iterator))

问题描述

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(无法读取属性Symbol(Symbol.iterator))

标签: javascriptreactjs

解决方案


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

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;
        }
    });

推荐阅读