简体   繁体   English

JSX三元表达式对地图的行为不同

[英]JSX Ternary Expression Behaves Differently For Map

Simple question: 简单的问题:

JSX allows you to return a map function which will be comprised of a bunch of divs or images elements or whatever without having to wrap the JSX expression in one element. JSX允许您返回一个映射函数,该函数将由一堆div或images元素组成, 而不必将JSX表达式包装在一个元素中。 However if you type out a series of DIVs explicitly without the map function, it yells at you. 但是,如果您在没有map函数的情况下显式键入一系列DIV,则会对您大吼大叫。 See example below: 请参见下面的示例:

    <div style={{display:'flex', justifyContent:'space-between'}}>
                {
                    (this.props.label.exampleImages.length > 0)

                    ? this.props.label.exampleImages.map((image, i)=>{
                        return <img src={image.url}
                             alt={image}
                             key={`image-${i}`}
                             style={{marginTop:10}}/>
                    })

                    : 
                        // this throws error even though above is essentially doing the same thing
                        <div className="image-placeholder"></div>
                        <div className="image-placeholder"></div>
                        <div className="image-placeholder"></div>


                }
            </div>

Why is this? 为什么是这样? Aren't they evaluating to the exact same thing? 他们不是对完全相同的事物进行评估吗?

The difference is that in the first part of your ternary expression you're only returning one value, that is your array of images. 不同之处在于,在三元表达式的第一部分中,您仅返回一个值,即图像数组。 However, when your this.props.label.exampleImages.length > 0 statement returns false, you're returning multiple values, which doesn't makes sense, as explained here docs . 但是,当this.props.label.exampleImages.length > 0语句返回false时,您将返回多个值,这没有意义,如docs所述

You already have an enclosing div around your array, which is <div style={{display:'flex', justifyContent:'space-between'}}> . 数组周围已经有一个封闭的div,它是<div style={{display:'flex', justifyContent:'space-between'}}> What's causing you error is that you're trying to do something along the lines of: var a = arr.length > 0 ? 1 : 1 2 3 导致您出错的原因是您正在尝试执行以下操作: var a = arr.length > 0 ? 1 : 1 2 3 var a = arr.length > 0 ? 1 : 1 2 3 . var a = arr.length > 0 ? 1 : 1 2 3 Which just isn't valid javascript as you can't return multiple values on a ternary expression. 这只是无效的javascript,因为您无法在三元表达式上返回多个值。

First it's important to remember that JSX gets compiled to JavaScript and that's ultimately what gets run with JavaScript syntax rules. 首先,重要的是要记住,JSX已被编译为JavaScript,这最终就是使用JavaScript语法规则运行的。

Inside your ternary operator each portion needs to evaluate to a single value. 在您的三元运算符内部,每个部分都需要求值为单个值。 example var myVar = maxValidNumStuff < getCurrItemNum() ? 'too many' : 'good' 示例var myVar = maxValidNumStuff < getCurrItemNum() ? 'too many' : 'good' var myVar = maxValidNumStuff < getCurrItemNum() ? 'too many' : 'good'

The maxValidNumStuff < getCurrItemNum() evaluated to a boolean, and each other portion evaluates to a string. maxValidNumStuff < getCurrItemNum()评估为布尔值,其他部分评估为字符串。

The Map method per Mozilla shows that the return value for .map is an array. 每个Mozilla的Map方法显示.map的返回值是一个数组。

If you look at how JSX compiles each <div>blah</div> when you do it outside of map, you'll see that you're putting multiple expressions which upsets the single expression that the ternary operator wants. 如果您查看JSX在地图之外进行编译时每个<div>blah</div>编译方式,则会发现您放置了多个表达式,这使三元运算符想要的单个表达式不满意。

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

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