简体   繁体   中英

Render class conditionally in React

I'm trying to render a class conditionally. If the mapped item is blank, I'd like there to be a class that renders. Otherwise, no changes. I'm sure this very simple but I'm new at this and not sure how to identify the blank item. Is this a problem with scope? This is the code in my component:

const TableBody = (props) => {
  let classes = ''
  classes += (props.data.map === '') ? '' : 'collapse'
  return (
    <tbody>
      {props.data.map((item, index) => (
        <tr key={typy(item, 'sys.id').safeString || index}>
          {props.columns.map(column =>
            <td className={classes} role='cell' key={column.label}>{typy(item, column.path).safeObject}</td>)
          }
        </tr>
      ))}
    </tbody>
  )
}

All of the <td> elements are collapsed so the code I'm using above must not be properly detecting a blank value. Can anyone point me in the right direction here?

Per my comment, props.data appears to be an array. You are checking to see if props.data.map === '' , which will always evaluate to false . You should probably fix that statement, otherwise the class will always be 'collapse'. Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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