简体   繁体   English

JSX 中的 if 语句

[英]if statement inside JSX

class App extends React.Component {

  render() {

    return (

      <div className="bg">
        {productList.map((product) => {
          return (
            <div className="mainContainer">
              <div className="billedeContainer">
                <img src={product.image} />
              </div>
              <div className="titel">Vare: {product.title}</div>
              <div className="type">Type: {product.type}</div>
              <div className="producent">Producent: {product.producer}</div>
              <div className="unit">
                {product.unitSize} {product.unit}
              </div>
              <div className="kolli">
                Kolli mængde: {product.price * product.bulkSize}
              </div>
              <div>Antal: {product.quantity}</div>

              <div className="prisContainer">
                <div className="pris">{product.price} kr,-</div>
              </div>
            </div>
          );
        })}
      </div>
    );
  }
}

I want to make an if statement on div "type", but I can't get the syntax to work properly.我想对 div“type”做一个 if 语句,但我无法让语法正常工作。 I am new at react/jsx.我是 react/jsx 的新手。

I want an if that says, whenever "type" is null in my array, then there shouldn't be an "type" appearing.我想要一个 if ,当我的数组中的“类型”为空时,就不应该出现“类型”。

试试看,当 type 为 null 时,Type div 不会被渲染:

{product.type && <div className="type">Type: {product.type}</div>}

You can use conditional statements in render.您可以在渲染中使用条件语句。

item && item.type? Show type: null

But better is to place the code in seperate function if it is too complex.但如果代码太复杂,最好将代码放在单独的函数中。

You can use a JSX expression to do that.您可以使用 JSX 表达式来做到这一点。 If the expression evaluates to null , undefined , or false , nothing at all is shown where the expression appears.如果表达式的计算结果为nullundefinedfalse ,则表达式出现的地方根本不显示任何内容。

You said your product.type may be null , so you can use the curiously-powerful && operator:您说您的product.type可能为null ,因此您可以使用功能强大的&&运算符:

{product.type && <div className="type">Type: {product.type}</div>}

The {} is the JSX expression. {}是 JSX 表达式。 The && operator works like this: It evaluates its left-hand operand and, if that value is falsy ,¹ takes the value as its result, completely ignoring the right-hand operand. &&运算符的工作方式如下:它评估其左侧操作数,如果该值为falsy ,¹ 将该值作为其结果,完全忽略右侧操​​作数。 If the left-hand operand's value is truthy , the expression evaluates the right-hand operand and takes that value as its result.如果左侧操作数的值为,则表达式计算右侧操作数并将该值作为其结果。

So if product.type is null , {product.type && <Stuff />} is {null} and nothing gets rendered.因此,如果product.typenull ,则{product.type && <Stuff />}{null}并且不会呈现任何内容。 If product.type isn't null , {product.type && <Stuff />} results in {<Stuff />} .如果product.type不为null ,则{product.type && <Stuff />}产生{<Stuff />}

If you were testing something that might be 0 or some such, you wouldn't use && because you'd end up with a 0 in the rendered output (since only null , undefined , and false are ignored).如果您正在测试可能为0或类似值的内容,则不会使用&&因为您最终会在呈现的输出中得到0 (因为只有nullundefinedfalse被忽略)。 In that case you'd use a conditional expression:在这种情况下,您将使用条件表达式:

{mightBeZero ? <Stuff /> : null}

¹ A falsy value is any value that's treated as false in a condition. ¹值是在条件中被视为false任何值。 The falsy values are 0 , null , undefined , NaN , "" , and of course, false .假值是0nullundefinedNaN"" ,当然还有false All other values are truthy .所有其他值都是真实的

<div className="type">Type: {product.type}</div>

=> {product.type ? (<div className="type">Type: {product.type}</div>) : null } => {product.type ? (<div className="type">Type: {product.type}</div>) : null } {product.type ? (<div className="type">Type: {product.type}</div>) : null }

or或者

`{product.type && (<div className="type">Type: {product.type}</div>)}`

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

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