简体   繁体   English

其他条件语句中的React.js条件语句存在问题

[英]Problems with React.js conditional statement within another conditional statement

The intention is to conditionally display elements if "sortBy" is equal to a particular value.. 目的是在“ sortBy”等于特定值的情况下有条件地显示元素。

I can do a single conditional statement but when I add another inside the first one it errors with 我可以做一个条件语句,但是当我在第一个条件语句中添加另一个条件语句时

Exception: Call to Node module failed with error: TypeError: "LastName" is not a function 异常:调用节点模块失败,并显示以下错误:TypeError:“ LastName”不是一个函数

Here is my code: 这是我的代码:

export const TableHeaders = (props) => {
    const { handleSubmit } = props

    const { sortBy, sortDirection} = props

    return (
        <div>
        <div className="row">
            <div className="col-md-1" style={headingCellStyle}>Client #</div>
            <div className="col-md-6" style={headingCellStyle}>
            Name / Address
            {sortBy === 'LastName' (
                <span>
                {sortDirection === 'Descending' ? (
                    <span className='glyphicon glyphicon-sort-by-attributes'></span>
                    ) : (
                    <span className='glyphicon glyphicon-sort-by-attributes-alt'></span>
                    )
                }
                </span>
            )}

            {console.log(`Sort Direction ${sortDirection}`)}
            </div>
            <div className="col-md-2" style={headingCellStyle}>Phone</div>
            <div className="col-md-1" style={headingCellStyle}>Jobs</div>
            <div className="col-md-2" style={headingCellStyle}>Actions</div>
        </div>
        </div>
    )
    }
    TableHeaders.propTypes = {
    onSubmit: PropTypes.func.isRequired,
    }

    const TableHeadersForm = reduxForm({
    form: 'SearchClients',
    })(TableHeaders)

    export default TableHeadersForm

How do I structure a double conditional statement here? 如何在此处构造双重条件语句?

I found that just the"sortDirection" conditional statement only works but adding the "sortBy" condtional statement around it fails. 我发现仅“ sortDirection”条件语句起作用,但在其周围添加“ sortBy”条件语句失败。

I'm first intending to check if "sortby" is equal to say "LastName" and if so then check "sortDirection" is either "Ascending" or "Descending" and display a glyphon accordingly.. 我首先打算检查“ sortby”是否等于“ LastName”,然后检查“ sortDirection”是“ Ascending”还是“ Descending”并相应地显示一个字形。

You missed a ? 你错过了? as well as the false case of first condition, You need to write it like this: 以及第一个条件的false情况,您需要这样编写:

{sortBy === 'LastName' ?
    <span>
    {
        sortDirection === 'Descending' ? 
            <span className='glyphicon glyphicon-sort-by-attributes'></span>
        : 
            <span className='glyphicon glyphicon-sort-by-attributes-alt'></span>
    }
    </span>
:
    null //or some element
}

If you don't want to render anything in false case you can write it like this also: 如果您不想在false情况下render任何内容,则也可以这样编写:

{sortBy === 'LastName' && <span>
    {
        sortDirection === 'Descending' ? 
            <span className='glyphicon glyphicon-sort-by-attributes'></span>
        : 
            <span className='glyphicon glyphicon-sort-by-attributes-alt'></span>
    }
    </span>
}

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

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