简体   繁体   English

当使用 Map 返回 jsx 时,结果是 undefined

[英]When returning jsx using Map it is giving undefined as a result

arrToAdd = 
[
    { id:"firstname", className:"input-sm", type:"text", name:"firstname", title:"Enter first name", placeholder:"First name" },
    { id:"lastname", className:"input-sm", type:"text", name:"lastname", title:"Enter last name", placeholder:"Last name" }
]

inputArr = [ this.arrToAdd ];

state = {
    modal13: false,
    name: '',
    status: false,
    radio: 2,
    inputArray : this.inputArr
}

showInput = this.state.inputArray.map( (value1) => {
    value1.map( (value) => {

        return (
            <input id={value.id} className={value.className} type={value.type} name={value.name} title={value.title} placeholder={value.placeholder} />
        );
    });

});

addMoreFields = () => {

    console.log(this.showInput); // Giving Undefined

    this.setState({
        ...this.state,
         inputArray : this.inputArr.push(this.arrToAdd) 
        });

    console.log(this.showInput); // Giving Undefined
}

Merge your array, don't push otherwise it will be array of array合并您的数组,不要推送,否则它将是数组数组

this.setState({
    ...this.state,
     inputArray : [inputArray, ...this.arrToAdd] 
    });
showInput = this.state.inputArray.map( (value1) => {
        let newArr = value1.map( (value) => {
            console.log(value);
            return (
                <input id={value.id} className={value.className} type={value.type} name={value.name} title={value.title} placeholder={value.placeholder} />
            );
        });

        return newArr ;

    });

Not sure why you want to put arrToAdd into inputArray but this should be what you want不确定为什么要将arrToAdd放入inputArray但这应该是您想要的

showInput = this.state.inputArray[0].map((value1) => {
    return (
        <input id={value.id} className={value.className} type={value.type} name={value.name} title={value.title} placeholder={value.placeholder} />
    );
});

Or directly change your state assignment to: inputArray : this.arrToAdd或者直接将您的状态分配更改为: inputArray : this.arrToAdd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM