简体   繁体   English

试图在React中映射数组,但未定义

[英]Trying to map array in React, getting not defined

Below is a section of my render and then the component that it references. 下面是渲染的一部分,然后是其引用的组件。 I am getting "li not defined". 我正在“ li未定义”。 I have tried wrapping it in {} and at this point not sure what I am missing. 我尝试将其包装在{}中,但现在不确定我缺少什么。 Thanks for the help. 谢谢您的帮助。

Render section: 渲染部分:

var uls = _.chunk(_.take(this.state.results, maxICanShow), perColumn);

    return (
        <div>
        uls.map((lis) => {
            <ul className="appt_bd">
                <li className="appt_bd_header" style={{ width: colWidth + '%' }}>
                    <span className="appt_bd_header_main">{this.props.title}</span>
                    <span className="appt_bd_header_sub">Appts Set</span>
                    <span className="appt_bd_header_sub">Appts Show</span>
                </li>
                lis.map((li) => <AppointmentBoardRow key={li.userId} row={li} />)
            </ul>
        })
            </div>
    );

Here is the component it references: 这是它引用的组件:

var AppointmentBoardRow = React.createClass({
render: function () {
    var row = this.props.row;
    return (
        <li className="appt_bd_row" style={{ width: this.props.colWidth + '%' }} key={row.userId}>
            <span className="appt_bd_desc">
                <span className="appt_bd_rank">{row.rank}</span>
                <span className="appt_bd_avatar_container"><div className={row.className}><span className="initials">{row.initials}</span></div></span>
                <span className="appt_bd_user">
                    <span className="appt_bd_description_main">{row.userName}</span>
                    <span className="appt_bd_description_sub">{row.role}</span>
                </span>
            </span>
            <span className="appt_bd_data">
                <span className="appt_bd_data_main">{row.apptsSetCount}</span>
                <span className="appt_bd_data_sub">/{row.apptsSetGoal}</span>
            </span>
            <span className="appt_bd_data">
                <span className="appt_bd_data_main">{row.apptsShowCount}</span>
                <span className="appt_bd_data_sub">/{row.apptsShowGoal}</span>
            </span>
        </li>
    );
}

}); });

It looks like there are a couple of small syntax errors in your first code snippet. 您的第一个代码段中似​​乎有一些小的语法错误。 Specifically, the call to uls.map is javascript within html syntax, so should be wrapped in {}. 具体来说,对uls.map的调用是html语法中的javascript,因此应将其包装在{}中。 The same applies to lis.map . lis.map

Additionally, uls.map((lis) => { <ul>...</ul> } is a function that does not return anything. You might instead want to represent it as uls.map((lis) => ( <ul>...</ul> ) , or uls.map((lis) => { return (<ul>...</ul>) } . You might want to use the latter and set a debugger to figure out what exactly is happening to the passed in data if the problem still persists. 此外, uls.map((lis) => { <ul>...</ul> }是不返回任何内容的函数。您可能希望将其表示为uls.map((lis) => ( <ul>...</ul> )uls.map((lis) => { return (<ul>...</ul>) } ,您可能想使用后者并将调试器设置为如果问题仍然存在,请弄清楚传递的数据到底发生了什么。

I took a pass at fixing the syntax errors I mentioned above, hope this helps. 我通过修复上面提到的语法错误,希望对您有所帮助。

var uls = _.chunk(_.take(this.state.results, maxICanShow), perColumn);

return (
    <div>
        {
            uls.map((lis) => (
                <ul className="appt_bd">
                    <li className="appt_bd_header" style={{ width: colWidth + '%' }}>
                        <span className="appt_bd_header_main">{this.props.title}</span>
                        <span className="appt_bd_header_sub">Appts Set</span>
                        <span className="appt_bd_header_sub">Appts Show</span>
                    </li>
                    { lis.map((li) => <AppointmentBoardRow key={li.userId} row={li} />) }
                </ul>
            )
        }
    </div>
);

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

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