简体   繁体   English

传递一个 <li> 当React中有三元运算符的数据时

[英]Pass in a <li> when there is data with ternary operator in React

How can I optionally pass in a list item only if there's data in it? 仅当其中有数据时,才可以有选择地传递列表项吗? I'm storing all of my text information in info.json : 我将所有文本信息存储在info.json

{
  "info": [
    {
      "name": "Bob",
      "description": "Bob Vance of Vance Refrigeration"
    },
    {
      "name": "Kevin",
      "description": ""
    },
    {
      "name": "Jim",
      "description": ""
    }
  ]
}

I'm mapping each of these items and rendering them onto the page only if there's content inside. 我正在映射这些项目中的每一项,并且仅在其中包含内容的情况下才将它们呈现到页面上。 As you can see, Kevin and Jim doesn't have any description, so I wouldn't render it onto the list. 如您所见,Kevin和Jim没有任何描述,因此我不会将其呈现在列表上。

info(item) {
  <div className="row">
    <ul>
      <li>{item.name}</li>
      <li>{item.description == "" ? undefined : item.description}</li>
    </ul>
  </div>
} 

//this.props.office is passed down from `App.js` and contains the JSON that is shown above
render() {
  return (
    {(this.props.office).map((x) => this.info(x))}
  )
}

How can I avoid displaying the list item if item.description is an empty string? 如果item.description为空字符串,如何避免显示列表项? I tried to set it to undefined but that doesn't work. 我试图将其设置为undefined,但这是行不通的。

If you don't want to render items without description at all, you should filter it out from the array: 如果您根本不想渲染没有描述的项目,则应从数组中过滤掉它:

{(this.props.office)
   .filter((x) => x.description !== "")
   .map((x) => this.info(x))}

Furthermore I belive you have to return react element (jsx) from info method to make it work: 此外,我相信您必须从info方法返回react元素(jsx)才能使其工作:

info(item) {
  return (
    <div className="row">
      <ul>
        <li>{item.name}</li>
        <li>{item.description}</li>
      </ul>
    </div>
  )
}

You can just check for the existence of description , if it is available, call the info() method else return false . 您可以只检查description的存在,如果可用,则调用info()方法,否则返回false React won't render Boolean and undefine s. React不会渲染Booleanundefine

Hope this helps! 希望这可以帮助!

 const config = { "info": [{ "name": "Bob", "description": "Bob Vance of Vance Refrigeration" }, { "name": "Kevin", "description": "" }, { "name": "Jim", "description": "" }] } class App extends React.Component{ info(item) { return <div className="row"> <ul> <li>{item.name}</li> <li>{item.description}</li> </ul> </div> } render(){ const list = config.info.map((x) => x.description.length > 0 && this.info(x)) return <div> {list} </div> } } ReactDOM.render(<App/>, document.getElementById('app')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

Use a logical or because in react, a Boolean "element" is ignored ( demo ): 请使用逻辑or因为在react中, 布尔值“ element”将被忽略demo ):

  <div className="row">
    <ul>
      <li>{item.name}</li>

      /* if the description is empty the result is true (and will be ignored), else  the result is the JSX */
      {item.description === '' || <li>{item.description}</li>} 

    </ul>
  </div>

However, if you're not sure if the description will be an empty string (""), and can be any "empty value" (undefined, null) you can do this instead ( demo ): 但是,如果不确定描述是否为空字符串(“”),并且可以为任何“空值”(未定义,null),则可以改为( demo ):

  <div className="row">
    <ul>
      <li>{item.name}</li>

      /* if the description is "", 0, null, or undefined it will be converted to false (and will be ignored), else  the result is the JSX */
      {!!item.description && <li>{item.description}</li>} 

    </ul>
  </div>

you can just wrap the condition around the li 你可以把条件包裹在li附近

info(item) {
  <div className="row">
    <ul>
      <li>{item.name}</li>
      {item.description !== "" && <li>item.description</li>}
    </ul>
  </div>
} 

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

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