elainecadman
elainecadman

Reputation: 69

Conditional Rendering Table Rows React

I have a table of many rows which I'm passing props into. If the returned props are an empty string "" I don't want to render that row

    <Table.Body>

      <Table.Row>
        <Table.Cell>Producer</Table.Cell>
        <Table.Cell>{props.producer}</Table.Cell>
      </Table.Row>

      <Table.Row>
        <Table.Cell>Country</Table.Cell>
        <Table.Cell>{props.country}</Table.Cell>
      </Table.Row>

      <Table.Row>
        <Table.Cell>Region</Table.Cell>
        <Table.Cell>{props.region}</Table.Cell>
      </Table.Row>

      <Table.Row>    
        <Table.Cell>Subregion</Table.Cell>
        <Table.Cell>{props.subregion}</Table.Cell>
      </Table.Row>

    </Table.Body>

and where it gets rendered:

render() {
return (
//  <Container>
 <Grid>

    {this.state.wines.length ? (
          <List>
            {this.state.wines.map(wine => (
              <Grid>
               <DataWine header={wine.Wine} producer={wine.Producer} country={wine.Country} region={wine.Region} subregion={wine.Subregion}/>

              </Grid>
            ))}

          </List>
        ) : (
          <h3>No Results to Display</h3>
        )}

So in this example, if the JSON returns "" for Subregion, I don't want the row that says Subregion: " " to render. Thanks in advance!

Upvotes: 1

Views: 5202

Answers (1)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

I'm not pretty sure what you're asking for. But I'm pretty sure you just need to use && operator like:

{
  props.region &&
  <Table.Row>
    <Table.Cell>Region</Table.Cell>
    <Table.Cell>{props.region}</Table.Cell>
  </Table.Row>
}

Upvotes: 4

Related Questions