代码之家  ›  专栏  ›  技术社区  ›  Yen Sheng

如何在三元表达式中使用null?

  •  1
  • Yen Sheng  · 技术社区  · 9 年前

    我使用react和react引导来呈现页面,使用 <ListGroup> <ListGroupItem> bsStyle='info' 当状态不是 '1' '1' .

    <ListGroup>
      {tables.map(table => (
        <ListGroupItem
          key={`table.${table.id}`}
          header=(table.name)
          bsStyle={table.status !== '1' ? ('info') : ('')}
          >
          {table.content} - 
        </ListGroupItem>
      ))}
    </ListGroup>
    

    它已正确渲染,但由于bsStyle的验证,不断收到以下警告:

    Warning: Failed prop type: Invalid prop `bsStyle` of value `` supplied to `ListGroupItem`, expected one of ["success","warning","danger","info"].
    

    有一种解决方法可以通过设置 bsStyle={null} .然而,我无法在三元表达式中工作。以下代码将使其成为字符串 bsStyle='{null}'

    <ListGroup>
      {tables.map(table => (
        <ListGroupItem
          key={`table.${table.id}`}
          header=(table.name)
          bsStyle={table.status !== '1' ? ('info') : ('{null}')}
          >
          {table.content} - 
        </ListGroupItem>
      ))}
    </ListGroup>
    

    通过的正确方式是什么 {null} 不让它成为一根绳子?

    2 回复  |  直到 9 年前
        1
  •  3
  •   Andrew Li    9 年前

    ('{null}')
    

    从字面上看 "{null}" .试试这个:

    bsStyle={table.status !== '1' ? 'info' : null}
    

    null 很明显,不要用引号括起来。这是 a JSFiddle example

        2
  •  2
  •   Davorin RuÅ¡evljan    9 年前

    如果状态与1没有不同,您可能希望对整个bsStyle属性进行操作

    <ListGroupItem
       key={`table.${table.id}`}
       header=(table.name)
       {...( table.status !== '1' ? {bsStyle:'info'} : {} )}
      >