最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论
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}
  />
));

I want to if (h.requered == true) { return [this.required] } else { null }

I have problem

react js : TypeError: object null is not iterable (cannot read property 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}
  />
));

I want to if (h.requered == true) { return [this.required] } else { null }

I have problem

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

Share Improve this question edited Mar 14, 2019 at 23:02 Hemant Parashar 3,7842 gold badges17 silver badges23 bronze badges asked Mar 14, 2019 at 21:02 Said MounaimSaid Mounaim 11 gold badge1 silver badge2 bronze badges 3
  • 1 It looks like validations expects to be passed an array, not null. So maybe pass an empty array instead of null? – Felix Kling Commented Mar 14, 2019 at 21:04
  • 1 This.state.hiring is null, you can set the initial state of hiring to an empty array instead. – vitomadio Commented Mar 14, 2019 at 21:16
  • What is this.state.hiring? Could you update the code? I bet you're getting error from this line, not validations, because it's the only iterating part in your snippet – flppv Commented Mar 15, 2019 at 1:12
Add a ment  | 

1 Answer 1

Reset to default 1

Maybe you can modify your code like this:

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;
        }
    });
Post a comment

comment list (0)

  1. No comments so far